Mia 2 tahun lalu
induk
melakukan
e7d6bc8b9b
1 mengubah file dengan 66 tambahan dan 12 penghapusan
  1. 66 12
      openshot_word.py

+ 66 - 12
openshot_word.py

@@ -8,6 +8,7 @@ from datetime import datetime
 from PIL import Image,ImageDraw,ImageFont
 import gspread
 import pandas as pd
+import csv
 from oauth2client.service_account import ServiceAccountCredentials 
 
 def auth_gss_client(path, scopes):
@@ -57,7 +58,7 @@ def sheet_to_text(text_font):
         end_timeStamp=end.seconds+0.000001*end.microseconds
         start_timeStamp=start.minute*60+start.second+ 0.000001*start.microsecond
         text_form.append({'text':context[2],'start':start_timeStamp,'end':end_timeStamp,'size':36,'font':text_font})
-        print({'text':context[2],'start':start_timeStamp,'end':end_timeStamp,'size':36,'font':text_font})
+        #print({'text':context[2],'start':start_timeStamp,'end':end_timeStamp,'size':36,'font':text_font})
 
 def cKey(r,g,b,fuzz):
     col=openshot.Color()
@@ -144,8 +145,35 @@ def text_border(draw,x,y,text,font,shadowcolor,fillcolor):
     # now draw the text over it
     draw.text((x, y), text, font=font, fill=fillcolor)
 
+def srt_to_csv(srt_file):
+    subs = pysrt.open(srt_file)
+    csv_file = srt_file.split('.')[0] + ".csv"
+    with open(csv_file, 'w', newline='') as csvfile:
+        # 建立 CSV 檔寫入器
+        writer = csv.writer(csvfile)
+        for context in subs:
+            writer.writerow([context.index, context.start,context.end, context.text])
+    return csv_file
+
+def csv_to_text(csv_file,text_font):
+    text_form = []
+    with open(csv_file, newline='') as csvfile:
+
+        # 讀取 CSV 檔案內容
+        rows = csv.reader(csvfile)
+
+        # 以迴圈輸出每一列
+        for row in rows:
+            start = datetime.strptime(row[1], "%H:%M:%S,%f")
+            end = datetime.strptime(row[2], "%H:%M:%S,%f") - datetime.strptime(row[1], "%H:%M:%S,%f")
+            end_timeStamp=end.seconds+0.000001*end.microseconds
+            start_timeStamp=start.minute*60+start.second+ 0.000001*start.microsecond
+            text_form.append({'text':row[3],'start':start_timeStamp,'end':end_timeStamp,'size':36,'font':text_font})
+    
+    return text_form
+
 def text_to_short_vedio(mp4_file = "input/example/test3.mp4",sound_file = None
-                        ,vedio_time = 30,output_filename="output/demo.mp4",text_font = "font/DFT_B7.ttc"):
+                        ,vedio_time = 30,output_filename="output/demo.mp4",text_font = "font/DFT_B7.ttc",mode = "csv",correct = False):
     t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
     t.Open()
     
@@ -162,19 +190,43 @@ def text_to_short_vedio(mp4_file = "input/example/test3.mp4",sound_file = None
     number = 0
 
     sound_srt_file = ""
+    text_form = []
+
+    if ".csv" in sound_file :
+        correct = True
+        mode = "csv"
+
     #音檔自動產生srt(逐字稿)
-    if ".srt" in sound_file:
-        sound_srt_file = sound_file
-    elif not sound_file is None:
+    if not correct: # 未修正過的、需要產生SRT檔
         cmd = "autosub -S zh-TW -D zh-TW " + sound_file
         os.system(cmd)
         sound_srt_file = sound_file.split('.')[0] + ".srt"
-    
 
-    #開啟srt檔
+        if mode == "google" :
+            srt_to_sheet(sound_srt_file)
+            text_form = sheet_to_text(text_font)
+        elif mode == "csv" :
+            csv_file = srt_to_csv(sound_srt_file)
+            text_form = csv_to_text(csv_file,text_font)
+
+    else:
+        if ".srt" in sound_file:
+            sound_srt_file = sound_file
+            if mode == "google" :
+                srt_to_sheet(sound_srt_file)
+                text_form = sheet_to_text(text_font)
+            elif mode == "csv" :
+                csv_file = srt_to_csv(sound_srt_file)
+                text_form = csv_to_text(csv_file,text_font)
+        else:
+            if mode == "google" :
+                text_form = sheet_to_text(text_font)
+            elif mode == "csv" :
+                csv_file = sound_file
+                text_form = csv_to_text(csv_file,text_font)
+
+    #產生字幕
     try:
-        srt_to_sheet(sound_file)
-        text_form = sheet_to_text(text_font)
         number = 0
         for text_tmp in text_form:
             file_name = "tmp/save_target_" + str(number) + ".png"
@@ -208,8 +260,10 @@ def text_to_short_vedio(mp4_file = "input/example/test3.mp4",sound_file = None
 
 if __name__ == '__main__':
     
-    text_to_short_vedio(mp4_file = "input/example/導盲犬_投影片2.mp4",sound_file ='input/example/導盲犬_投影片2.srt',vedio_time =110,text_font ="font/DFT_R7.ttc")
-    #srt_to_sheet('input/example/導盲犬_投影片2.srt')
-    #sheet_to_text("font/DFT_R7.ttc")
+    #text_to_short_vedio(mp4_file = "input/example/導盲犬_投影片2.mp4",
+        #sound_file ='input/example/導盲犬_投影片2.srt',vedio_time =110,text_font ="font/DFT_R7.ttc")
+    file = srt_to_csv('input/example/導盲犬_投影片2.srt')
+    text = csv_to_text(file,"font/DFT_R7.ttc")
+    print(text)