Mia 2 سال پیش
والد
کامیت
d46aeccafe
1فایلهای تغییر یافته به همراه29 افزوده شده و 34 حذف شده
  1. 29 34
      openshot_word.py

+ 29 - 34
openshot_word.py

@@ -1,4 +1,5 @@
 import openshot
+import os
 import re
 from PIL import Image,ImageDraw,ImageFont
 
@@ -41,65 +42,58 @@ def trim_punctuation(s):
     res = re.sub(pattern, lambda x: x.group(1) if x.group(1) else u" " ,s)
     return res
 
-def txt2image(content, save_target,lang='zh',size=38):
+def txt2image(content, save_target,lang='zh',size=26,fon="font/DFT_B7.ttc"):
     unicode_text = trim_punctuation(content)
     font = ''
     if lang=='zh':
-        font = ImageFont.truetype(font="font/DFT_B7.ttc", size=size)
+        font = ImageFont.truetype(font=fon, size=size)
     else :
         font = ImageFont.truetype(font="font/arial.ttf", size=size)
     text_width, text_height = font.getsize(unicode_text)
-    canvas = Image.new('RGBA', (700, 500), (255, 255, 255, 0) )
+    W, H = (1280,500)
+    canvas = Image.new('RGBA', (W, H), (255, 255, 255, 0) )
     draw = ImageDraw.Draw(canvas)
+    w, h = draw.textsize(content,font = font)
     text= unicode_text
-    draw.text((5,5), text,'black', font)
+    draw.text(((W-w)/2,0), text,'black', font)
     canvas.save(save_target, "PNG")
 
-def text_to_short_vedio(bg,title_bg,text,title,time):
+def text_to_short_vedio(bg,text_form,vedio_time):
     t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
     t.Open()
     
     # 去背參數
-    ck=cKey(0,254,0,270)
-    ck_anchor=cKey(0,255,0,320)
+    ck = cKey(0, 254, 0, 270)
+    ck_anchor = cKey(0, 255, 0, 320)
 
-    anchor = openshot.QtImageReader(bg)
+    anchor = openshot.FFmpegReader(bg)
     anchor.Open()
     anchor_clip = video_photo_clip(video=anchor,layer=2,scale_x=1,scale_y=1,
-            location_x=0,location_y=0,position=0, end=5,ck=ck_anchor,audio=True)
+            location_x=0,location_y=0,position=0, end=vedio_time,ck=ck_anchor,audio=True)
     t.AddClip(anchor_clip)
     anchor.Close()
 
-    anchor3 = openshot.FFmpegReader(title_bg)
-    anchor3.Open()
-    anchor_clip3 = video_photo_clip(video=anchor3,layer=3,scale_x=0.34,scale_y=0.34,
-            location_x=0.33,location_y=-0.34,position=0, end=5,ck=ck_anchor,audio=True)
-    t.AddClip(anchor_clip3)
-    anchor3.Close()
-
-    txt2image(text, "tmp/save_target.png",lang='zh',size = 26)
-    text_anchor = openshot.QtImageReader("tmp/save_target.png")
-    text_anchor.Open()
-    text_anchor_clip = video_photo_clip(video=text_anchor,layer=4,scale_x=1,scale_y=1,
-            location_x=0.27,location_y=0.84,position=0, end=5,ck=ck_anchor,audio=True)
-    t.AddClip(text_anchor_clip)
-    text_anchor.Close()
-
-    txt2image(title, "tmp/save_title.png",lang='zh')
-    text_title = openshot.QtImageReader("tmp/save_title.png")
-    text_title.Open()
-    text_title_clip = video_photo_clip(video=text_title,layer=5,scale_x=0.5,scale_y=0.5,
-            location_x=0.5,location_y=-0.145,position=0, end=5,ck=ck_anchor,audio=True)
-    t.AddClip(text_title_clip)
-    text_title.Close()
+    for text_tmp in text_form:
+        print(text_tmp['text'])
+        time = float(text_tmp['end']) - float(text_tmp['start'])
+        file_name = "tmp/save_target_" + text_tmp['text'] + ".png"
+        txt2image(text_tmp['text'], file_name,lang='zh',size = text_tmp['size'])
+        exec('text_anchor_{} = openshot.QtImageReader("tmp/save_target_{}.png")'.format(text_tmp['text'],text_tmp['text']))
+        exec('text_anchor_{}.Open()'.format(text_tmp['text']))
+        exec('text_anchor_{}.Open()'.format(text_tmp['text']))
+        exec('text_anchor_clip_{} = video_photo_clip(video=text_anchor_{},layer=4,scale_x=1,scale_y=1,\
+                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']))
+        exec('t.AddClip(text_anchor_clip_{})'.format(text_tmp['text']))
+        exec('text_anchor_{}.Close()'.format(text_tmp['text']))
+        exec('os.remove("tmp/save_target_{}.png")'.format(text_tmp['text']))
+        
 
 
     w = video_writer_init("output/test2.mp4")
     w.Open()
 
-    frames = int(t.info.fps)*int(time)
+    frames = int(t.info.fps)*int(vedio_time)
     for n in range(frames):
-        # tmp = n%(int(t.info.fps)*3) +int(t.info.fps)*int(2)
         f=t.GetFrame(n)
         w.WriteFrame(f)
 
@@ -108,4 +102,5 @@ def text_to_short_vedio(bg,title_bg,text,title,time):
 
 
 if __name__ == '__main__':
-    text_to_short_vedio("input/bg/客家_BG.png","input/title/右上角桐花.MOV","長度測試長度測試長度測試","標題測試",3)
+    text_form = [{'text':"texttexttext",'start':0,'end':3,'size':26},{'text':"test22222222",'start':4,'end':6,'size':26}]
+    text_to_short_vedio("input/bg/dog.mp4",text_form,6)