Mia 2 rokov pred
rodič
commit
1433a4b9ef
1 zmenil súbory, kde vykonal 60 pridanie a 12 odobranie
  1. 60 12
      openshot_word.py

+ 60 - 12
openshot_word.py

@@ -4,7 +4,60 @@ import re
 import time
 import pysrt
 import shutil
+from datetime import datetime
 from PIL import Image,ImageDraw,ImageFont
+import gspread
+import pandas as pd
+from oauth2client.service_account import ServiceAccountCredentials 
+
+def auth_gss_client(path, scopes):
+    credentials = ServiceAccountCredentials.from_json_keyfile_name(path, scopes)
+    return gspread.authorize(credentials)
+
+
+auth_json_path = 'noted-tesla-348011-74f70c9caeda.json' #由剛剛建立出的憑證,放置相同目錄以供引入
+gss_scopes = ['https://spreadsheets.google.com/feeds'] #我們想要取用的範圍
+gss_client = auth_gss_client(auth_json_path, gss_scopes) #呼叫我們的函式
+
+#從剛剛建立的sheet,把網址中 https://docs.google.com/spreadsheets/d/〔key〕/edit 的 〔key〕的值代入 
+spreadsheet_key_path = '1LU5O8-oAotIFGPI9STPbElO0NHGA6eynuv9sYz81aOw'
+wks = gss_client.open_by_key(spreadsheet_key_path).sheet1
+
+def srt_to_sheet(srt_file):
+    subs = pysrt.open(srt_file)
+    wks.clear()
+
+    index = 0
+    for context in subs:
+        index = context.index
+
+    cell_list = wks.range('A1:C'+str(index))
+    
+    number = 0
+    for context in subs:
+        #print(context.start.minutes*60+context.start.seconds+ 0.001*context.start.milliseconds)
+        index = context.index
+        end = context.end
+        start = context.start
+        #print('A'+str(index))
+        cell_list[number].value = str(start)
+        cell_list[number+1].value = str(end)
+        cell_list[number+2].value = str(context.text)
+        number = number+3
+    
+    wks.update_cells(cell_list)
+
+def sheet_to_text(text_font):
+    ck_anchor = cKey(0, 255, 0, 320)
+    text_form = []
+    for context in wks.get_all_values():
+        #print(context.start.minutes*60+context.start.seconds+ 0.001*context.start.milliseconds)
+        start = datetime.strptime(context[0], "%H:%M:%S,%f")
+        end = datetime.strptime(context[1], "%H:%M:%S,%f") - datetime.strptime(context[0], "%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':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()
@@ -120,15 +173,9 @@ def text_to_short_vedio(mp4_file = "input/example/test3.mp4",sound_file = None
 
     #開啟srt檔
     try:
-        subs = pysrt.open(sound_srt_file)
-        text_form = []
-        for context in subs:
-            #print(context.start.minutes*60+context.start.seconds+ 0.001*context.start.milliseconds)
-            end = context.end-context.start
-            end_timeStamp=(end.minutes*60+end.seconds+ 0.001*end.milliseconds)
-            start_timeStamp=(context.start.minutes*60+context.start.seconds+ 0.001*context.start.milliseconds)
-            text_form.append({'text':context.text,'start':start_timeStamp,'end':end_timeStamp,'size':36,'font':text_font})
-
+        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"
             txt2image(text_tmp['text'], file_name,lang='zh',size = text_tmp['size'],fon = text_tmp['font'])
@@ -136,7 +183,7 @@ def text_to_short_vedio(mp4_file = "input/example/test3.mp4",sound_file = None
             exec('text_anchor_{}.Open()'.format(number))
             exec('text_anchor_{}.Open()'.format(number))
             exec('text_anchor_clip_{} = video_photo_clip(video=text_anchor_{},layer=4,scale_x=1,scale_y=1,\
-                    location_x=0,location_y=0.67,position=text_tmp["start"], end=text_tmp["end"],ck=ck_anchor,audio=True)'.format(number,number))
+                        location_x=0,location_y=0.67,position=text_tmp["start"], end=text_tmp["end"],ck=ck_anchor,audio=True)'.format(number,number))
             exec('t.AddClip(text_anchor_clip_{})'.format(number))
             exec('text_anchor_{}.Close()'.format(number))
             number = number+1
@@ -161,7 +208,8 @@ def text_to_short_vedio(mp4_file = "input/example/test3.mp4",sound_file = None
 
 if __name__ == '__main__':
     
-    text_to_short_vedio(mp4_file = "input/導盲犬影片.mp4",sound_file ='input/導盲犬影片.srt',vedio_time =284,text_font ="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")
+    #srt_to_sheet('input/example/導盲犬_投影片2.srt')
+    #sheet_to_text("font/DFT_R7.ttc")