openshot_word.py 3.9 KB

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