openshotExample.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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(title_mv,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. anchor4 = openshot.FFmpegReader(title_mv)
  58. anchor4.Open()
  59. anchor_clip = video_photo_clip(video=anchor4,layer=2,scale_x=1,scale_y=1,
  60. location_x=0,location_y=0,position=0, end=30,ck=ck_anchor,audio=True)
  61. t.AddClip(anchor_clip)
  62. anchor4.Close()
  63. anchor = openshot.QtImageReader(bg)
  64. anchor.Open()
  65. anchor_clip = video_photo_clip(video=anchor,layer=3,scale_x=1,scale_y=1,
  66. location_x=0,location_y=0,position=0, end=30,ck=ck_anchor,audio=True)
  67. t.AddClip(anchor_clip)
  68. anchor.Close()
  69. # anchor3 = openshot.FFmpegReader(title_bg)
  70. # anchor3.Open()
  71. # anchor_clip3 = video_photo_clip(video=anchor3,layer=3,scale_x=0.34,scale_y=0.34,
  72. # location_x=0.33,location_y=-0.34,position=0, end=30,ck=ck_anchor,audio=True)
  73. # t.AddClip(anchor_clip3)
  74. # anchor3.Close()
  75. txt2image(text, "tmp/save_target.png",lang='zh')
  76. text_anchor = openshot.QtImageReader("tmp/save_target.png")
  77. text_anchor.Open()
  78. text_anchor_clip = video_photo_clip(video=text_anchor,layer=4,scale_x=1,scale_y=1,
  79. location_x=0.27,location_y=0.83,position=0, end=30,ck=ck_anchor,audio=True)
  80. t.AddClip(text_anchor_clip)
  81. text_anchor.Close()
  82. txt2image(title, "tmp/save_title.png",lang='zh')
  83. text_title = openshot.QtImageReader("tmp/save_title.png")
  84. text_title.Open()
  85. text_title_clip = video_photo_clip(video=text_title,layer=5,scale_x=0.5,scale_y=0.5,
  86. location_x=0.5,location_y=-0.145,position=0, end=30,ck=ck_anchor,audio=True)
  87. t.AddClip(text_title_clip)
  88. text_title.Close()
  89. w = video_writer_init("output/test3.mp4")
  90. w.Open()
  91. frames = int(t.info.fps)*int(time)
  92. for n in range(frames):
  93. # tmp = n%(int(t.info.fps)*3) +int(t.info.fps)*int(2)
  94. f=t.GetFrame(n)
  95. w.WriteFrame(f)
  96. t.Close()
  97. w.Close()
  98. if __name__ == '__main__':
  99. # text_to_short_vedio("input/bg/客家_BG.png","input/title/右上角桐花.MOV","測試content","標題測試",3)
  100. # text_to_short_vedio("input/bg/客家_BG.png","input/movie_main/C0047.MP4","測試content","標題測試",300)
  101. text_to_short_vedio("input/movie_main/C0047.MP4","input/bg/客家_BG.png","測試content","標題測試",30)