openshotExample.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # coding:utf-8
  2. import openshot
  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'):
  40. unicode_text = trim_punctuation(content)
  41. font = ''
  42. if lang=='zh':
  43. font = ImageFont.truetype(font="font/DFT_B7.ttc", size=38)
  44. else :
  45. font = ImageFont.truetype(font="font/arial.ttf", size=38)
  46. text_width, text_height = font.getsize(unicode_text)
  47. canvas = Image.new('RGBA', (700, 500), (255, 255, 255, 0) )
  48. draw = ImageDraw.Draw(canvas)
  49. text= unicode_text
  50. draw.text((5,5), text,'black', font)
  51. canvas.save(save_target, "PNG")
  52. def text_to_short_vedio(bg,title_bg,text,title,time):
  53. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  54. t.Open()
  55. ck=cKey(0,254,0,270)
  56. ck_anchor=cKey(0,255,0,320)
  57. anchor = openshot.FFmpegReader(bg) # 影片
  58. anchor.Open()
  59. anchor_clip = video_photo_clip(video=anchor, layer=2, scale_x=0.59, scale_y=0.59,location_x=-0.04, location_y=-0.04, position=0, end=5, ck=ck_anchor, audio=True)
  60. t.AddClip(anchor_clip)
  61. anchor.Close()
  62. anchor3 = openshot.FFmpegReader(title_bg) # 樣板
  63. anchor3.Open()
  64. anchor_clip3 = video_photo_clip(video=anchor3, layer=1, scale_x=1, scale_y=1,
  65. location_x=0, location_y=0, position=0, end=5, ck=ck_anchor, audio=True)
  66. t.AddClip(anchor_clip3)
  67. anchor3.Close()
  68. txt2image(text, "tmp/save_target.png",lang='zh')
  69. text_anchor = openshot.QtImageReader("tmp/save_target.png")
  70. text_anchor.Open()
  71. text_anchor_clip = video_photo_clip(video=text_anchor,layer=4,scale_x=1,scale_y=1,
  72. location_x=0.27,location_y=0.83,position=0, end=5,ck=ck_anchor,audio=True)
  73. t.AddClip(text_anchor_clip)
  74. text_anchor.Close()
  75. txt2image(title, "tmp/save_title.png",lang='zh')
  76. text_title = openshot.QtImageReader("tmp/save_title.png")
  77. text_title.Open()
  78. text_title_clip = video_photo_clip(video=text_title,layer=5,scale_x=0.5,scale_y=0.5,
  79. location_x=0.5,location_y=-0.145,position=0, end=5,ck=ck_anchor,audio=True)
  80. t.AddClip(text_title_clip)
  81. text_title.Close()
  82. w = video_writer_init("/app/output/test_op.mp4")
  83. w.Open()
  84. # frames = int(t.info.fps)*int(time)
  85. frames = t.info.fps.ToInt()*int(time)
  86. print('結果一',frames)
  87. for n in range(frames):
  88. # tmp = n%(int(t.info.fps)*3) +int(t.info.fps)*int(2)
  89. f=t.GetFrame(n)
  90. w.WriteFrame(f)
  91. t.Close()
  92. w.Close()
  93. if __name__ == '__main__':
  94. text_to_short_vedio("/app/input/movie_main/C0050.mp4","/app/examples/主播示意1.mp4","測試content","標題測試",14)