openshotExample.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import openshot
  2. import re
  3. from PIL import Image,ImageDraw,ImageFont
  4. def cKey(r,g,b,fuzz):
  5. col=openshot.Color()
  6. col.red=openshot.Keyframe(r)
  7. col.green=openshot.Keyframe(g)
  8. col.blue=openshot.Keyframe(b)
  9. return openshot.ChromaKey(col, openshot.Keyframe(fuzz))
  10. def video_writer_init(path):
  11. w = openshot.FFmpegWriter(path)
  12. w.SetAudioOptions(True, "aac", 44100, 2, openshot.LAYOUT_STEREO, 3000000)
  13. w.SetVideoOptions(True, "libx264", openshot.Fraction(30000, 1000), 1280, 720,
  14. openshot.Fraction(1, 1), False, False, 3000000)
  15. return w
  16. def video_photo_clip(video=None,layer=None, position=None, end=None
  17. ,scale_x=1,scale_y=1,location_x=0,location_y=0,ck=None,audio=True):
  18. clip = openshot.Clip(video)
  19. clip.Layer(layer)
  20. clip.Position(position)
  21. clip.End(end)
  22. clip.scale_x=openshot.Keyframe(scale_x)
  23. clip.scale_y=openshot.Keyframe(scale_y)
  24. clip.location_x=openshot.Keyframe(location_x)
  25. clip.location_y=openshot.Keyframe(location_y)
  26. if ck!=None:
  27. clip.AddEffect(ck)
  28. if audio==True:
  29. clip.has_audio=openshot.Keyframe(1)
  30. else:
  31. clip.has_audio=openshot.Keyframe(0)
  32. return clip
  33. def trim_punctuation(s):
  34. pat_block = u'[^\u4e00-\u9fff0-9a-zA-Z]+'
  35. pattern = u'([0-9]+{0}[0-9]+)|{0}'.format(pat_block)
  36. res = re.sub(pattern, lambda x: x.group(1) if x.group(1) else u" " ,s)
  37. return res
  38. def txt2image(content, save_target,lang='zh'):
  39. unicode_text = trim_punctuation(content)
  40. font = ''
  41. if lang=='zh':
  42. font = ImageFont.truetype(font="font/DFT_B7.ttc", size=38)
  43. else :
  44. font = ImageFont.truetype(font="font/arial.ttf", size=38)
  45. text_width, text_height = font.getsize(unicode_text)
  46. canvas = Image.new('RGBA', (700, 500), (255, 255, 255, 0) )
  47. draw = ImageDraw.Draw(canvas)
  48. text= unicode_text
  49. draw.text((5,5), text,'black', font)
  50. canvas.save(save_target, "PNG")
  51. def text_to_short_vedio(bg,title_bg,text,title,time):
  52. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  53. t.Open()
  54. # 去背參數
  55. ck=cKey(0,254,0,270)
  56. ck_anchor=cKey(0,255,0,320)
  57. anchor = openshot.QtImageReader(bg)
  58. anchor.Open()
  59. anchor_clip = video_photo_clip(video=anchor,layer=2,scale_x=1,scale_y=1,
  60. location_x=0,location_y=0,position=0, end=5,ck=ck_anchor,audio=True)
  61. t.AddClip(anchor_clip)
  62. anchor.Close()
  63. anchor3 = openshot.FFmpegReader(title_bg)
  64. anchor3.Open()
  65. anchor_clip3 = video_photo_clip(video=anchor3,layer=3,scale_x=0.34,scale_y=0.34,
  66. location_x=0.33,location_y=-0.34,position=0, end=5,ck=ck_anchor,audio=True)
  67. t.AddClip(anchor_clip3)
  68. anchor3.Close()
  69. txt2image(text, "tmp/save_target.png",lang='zh')
  70. text_anchor = openshot.QtImageReader("tmp/save_target.png")
  71. text_anchor.Open()
  72. text_anchor_clip = video_photo_clip(video=text_anchor,layer=4,scale_x=1,scale_y=1,
  73. location_x=0.27,location_y=0.83,position=0, end=5,ck=ck_anchor,audio=True)
  74. t.AddClip(text_anchor_clip)
  75. text_anchor.Close()
  76. txt2image(title, "tmp/save_title.png",lang='zh')
  77. text_title = openshot.QtImageReader("tmp/save_title.png")
  78. text_title.Open()
  79. text_title_clip = video_photo_clip(video=text_title,layer=5,scale_x=0.5,scale_y=0.5,
  80. location_x=0.5,location_y=-0.145,position=0, end=5,ck=ck_anchor,audio=True)
  81. t.AddClip(text_title_clip)
  82. text_title.Close()
  83. w = video_writer_init("output/test2.mp4")
  84. w.Open()
  85. frames = int(t.info.fps)*int(time)
  86. for n in range(frames):
  87. # tmp = n%(int(t.info.fps)*3) +int(t.info.fps)*int(2)
  88. f=t.GetFrame(n)
  89. w.WriteFrame(f)
  90. t.Close()
  91. w.Close()
  92. if __name__ == '__main__':
  93. text_to_short_vedio("input/bg/客家_BG.png","input/title/右上角桐花.MOV","測試content","標題測試",3)