Jared преди 2 години
родител
ревизия
7b19b1d282
променени са 4 файла, в които са добавени 189 реда и са изтрити 20 реда
  1. 5 2
      AI_anchor2/Dockerfile
  2. 78 11
      AI_anchor2/main1.py
  3. 82 6
      AI_anchor2/main2.py
  4. 24 1
      AI_anchor2/readme.md

+ 5 - 2
AI_anchor2/Dockerfile

@@ -37,5 +37,8 @@ RUN pip3 install opencv-contrib-python
 RUN pip3 install autosub
 RUN pip3 install autosub3
 RUN pip3 install openpyxl
-RUN apt-get install unrar -y
-RUN pip3 install rarfile
+RUN sudo apt install unrar
+RUN pip3 install rarfile
+RUN pip3 install fire
+RUN pip3 install oauth2client
+RUN pip3 install gspread

+ 78 - 11
AI_anchor2/main1.py

@@ -18,6 +18,10 @@ import zipfile
 import csv
 from datetime import datetime
 import rarfile
+import gspread
+from oauth2client.service_account import ServiceAccountCredentials 
+import fire
+
 def cKey(r,g,b,fuzz):
     col=openshot.Color()
     col.red=openshot.Keyframe(r)
@@ -97,7 +101,7 @@ def download_mp4(url):
    
  
 
-def text_to_short_vedio_create(read_csv_use="導盲犬協會.csv",pwd_use="導盲犬協會影片素材2/",op="input_self/LOGO_OP_4.mp4",ed="input_self/LOGO_ED.mp4",bg="input_self/xchoozmo_long.mp4",input_zip="input/input_data.zip"):
+def text_to_short_vedio_create(op="input_self/LOGO_OP_4.mp4",ed="input_self/LOGO_ED.mp4",bg="input_self/xchoozmo_long.mp4",input_zip="input/input_data.zip",save_type="excel"):
     
 
     files=os.listdir("input/")
@@ -280,8 +284,12 @@ def text_to_short_vedio_create(read_csv_use="導盲犬協會.csv",pwd_use="導
         locals()['anchor_music'+str(i)].Close()
 
         if str(csv_use.loc[i,['是否要場景']].values[0])=="是":
-            scale_x_use = 0.59
-            scale_y_use = 0.59
+            # scale_x_use = 0.59
+            # scale_y_use = 0.59
+            scale_x_use = 0.8
+            scale_y_use = 0.82
+            location_x_choose=0
+            location_y_choose=-0.039
 
         else:
             scale_x_use = 1
@@ -332,6 +340,10 @@ def text_to_short_vedio_create(read_csv_use="導盲犬協會.csv",pwd_use="導
     t.AddClip(locals()['anchor_clip_bg'])
     locals()['anchor_bg'].Close()
 
+
+
+
+    # locals()['anchor_ad'] = openshot.FFmpegReader('input_self/puuet主播_long.mp4')
     locals()['anchor_ad'] = openshot.FFmpegReader('input_self/AI_girl/ai_spokesgirl.mp4')
     locals()['anchor_ad'].Open()
     locals()['anchor_clip_ad'] = video_photo_clip(video=locals()['anchor_ad'],layer=6,scale_x=0.8,scale_y=0.8,
@@ -387,6 +399,11 @@ def text_to_short_vedio_create(read_csv_use="導盲犬協會.csv",pwd_use="導
     os.mkdir('input')
     shutil.rmtree('input_self/AI_girl')
     os.mkdir('input_self/AI_girl')
+    if save_type=="excel":
+        vedio_to_csv()
+    elif save_type=="sheet":
+        vedio_to_sheet()
+
     return True
 #文字轉圖片
 def txt2image(content, save_target,lang='zh',size=26,fon="input_self/font/DFT_B7.ttc"):
@@ -434,12 +451,12 @@ 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):
+def srt_to_csv(srt_file,sound_file="output/csv_produce/test.csv"):
     subs = pysrt.open(srt_file)
 
     
     # csv_file = srt_file.split('.')[0] + ".csv"
-    with open("output/csv_produce/test.csv", 'w', newline='',encoding="big5") as csvfile:
+    with open(sound_file, 'w', newline='',encoding="big5") as csvfile:
         # 建立 CSV 檔寫入器
         writer = csv.writer(csvfile)
         writer.writerow(["序號","開始時間","結束時間","字幕內容"])
@@ -528,16 +545,66 @@ def text_to_short_vedio(mp4_file ,sound_file,output_filename,text_font):
     # os.mkdir('input_self/tmp')
     # shutil.rmtree('input_self/tmp1')
     # os.mkdir('input_self/tmp1')
-def vedio_to_csv(sound_file):
+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 vedio_to_csv(sound_file = "output/no_captions/test.mp4"):
+    cmd = "autosub -S zh-TW -D zh-TW " + sound_file
+    os.system(cmd)
+    sound_srt_file = sound_file.split('.')[0] + ".srt"
+    srt_to_csv(sound_srt_file,sound_file)
+
+def vedio_to_sheet():
     cmd = "autosub -S zh-TW -D zh-TW " + sound_file
     os.system(cmd)
     sound_srt_file = sound_file.split('.')[0] + ".srt"
-    srt_to_csv(sound_srt_file)
+    srt_to_sheet(sound_srt_file)
 
 if __name__ == '__main__':
-    # down=text_to_short_vedio_create()
-    # if down:
-    #     vedio_to_csv("output/no_captions/test.mp4")
-    vedio_to_csv("output/no_captions/test.mp4")
+    # fire.Fire()
+    fire.Fire(text_to_short_vedio_create(
+        op="input_self/LOGO_OP_4.mp4",
+        ed="input_self/LOGO_ED.mp4",
+        bg="input_self/xchoozmo_long.mp4",
+        input_zip="input/input_data.zip",
+        save_type="excel"))
+
+    # vedio_to_csv("output/no_captions/test.mp4")
 	
     

+ 82 - 6
AI_anchor2/main2.py

@@ -17,6 +17,25 @@ from bs4 import BeautifulSoup
 import zipfile
 import csv
 from datetime import datetime
+import gspread
+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 cKey(r,g,b,fuzz):
     col=openshot.Color()
     col.red=openshot.Keyframe(r)
@@ -170,11 +189,15 @@ def text_to_short_vedio(mp4_file ,sound_file,output_filename,text_font):
     #     os.system(cmd)
     #     sound_srt_file = sound_file.split('.')[0] + ".srt"
     # csv_file = srt_to_csv(sound_srt_file)
-    csv_file = "output/csv_produce/test.csv"
 
-    text_form = csv_to_text(csv_file,text_font)
- 
 
+    # csv_file = "output/csv_produce/test.csv"
+
+    # text_form = csv_to_text(csv_file,text_font)
+    
+
+    text_form = sheet_to_text(text_font)
+    # print(text_form)
     # print(sound_srt_file)
     #開啟srt檔
     try:
@@ -209,11 +232,64 @@ def text_to_short_vedio(mp4_file ,sound_file,output_filename,text_font):
     os.mkdir('input_self/tmp')
     # shutil.rmtree('input_self/tmp1')
     # os.mkdir('input_self/tmp1')
+def csv_to_text2(csv_file,text_font):
+    text_form = []
+    csv_use=pd.read_csv("input/主播檔.csv")
+    b=""
+    for i in range(len(csv_use)):
+    # for i in range(3):
+        b+=str(csv_use.loc[i,['字幕']].values[0])
+    print(b)
+    print(len(b))
+    out = re.sub(r'[^\w\s]','',b)
+    print(out)
+    print(len(out))
+    with open(csv_file, newline='',encoding="big5") as csvfile:
 
+        # 讀取 CSV 檔案內容
+        rows = csv.reader(csvfile)
+
+        j=0
+        # 以迴圈輸出每一列
+        a = ""
+        for row in rows:
+
+            if j==0:
+                j = 1
+                continue
+            
+            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})
+            a+=row[3]
+        print(a)
+        print(len(a))
+        
+    return text_form
+
+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})
+    return text_form
 
 if __name__ == '__main__':
-	# text_to_short_vedio_create()
-	text_to_short_vedio(mp4_file = "output/no_captions/test.mp4",sound_file ='output/no_captions/test.mp4',
-        output_filename="output/finally_output/demo.mp4",text_font ="input_self/font/DFT_R7.ttc")
+
+    fire.Fire(text_to_short_vedio(
+        mp4_file = "output/no_captions/test.mp4",
+        sound_file ='output/no_captions/test.mp4',
+        output_filename="output/finally_output/demo.mp4",
+        text_font ="input_self/font/DFT_R7.ttc"))
+
+
+    # csv_to_text2("output/csv_produce/test.csv","input_self/font/DFT_R7.ttc")
 
     

+ 24 - 1
AI_anchor2/readme.md

@@ -8,4 +8,27 @@
 		docker run -it -v C:/Users/oak/Desktop/AI_anchor:/app ai_anchor
 		
 2.使用說明:(檔案名稱沒有限制,但格式請參考圖1~圖12說明和sample_explain.csv說明)
-	
+
+
+3.可調整參數:
+	main1.py
+		1.
+		輸入
+		python3 main1.py --op="input_self/LOGO_OP_4.mp4" --ed="input_self/LOGO_ED.mp4" --bg="input_self/xchoozmo_long.mp4" --input_zip="input/input_data.zip" --save_type="excel"
+		2.
+		op:所引用開頭影片檔案位置
+		ed:所引用結尾影片檔案位置
+		bg:所引用背景影片檔案位置
+		input_zip:所引用壓縮包檔案位置
+		save_type:可選["excel","sheet"] :
+				  "excel"為存成excel檔案
+				  "sheet"為存在google sheet
+	main2.py
+		1.
+		輸入
+		python3 main2.py --mp4_file = "output/no_captions/test.mp4" --sound_file ='output/no_captions/test.mp4' --output_filename="output/finally_output/demo.mp4" --text_font ="input_self/font/DFT_R7.ttc"
+		2.
+		mp4_file:所引用無字幕影片檔案畫面位置
+		sound_file:所引用無字幕影片檔案聲音位置
+		output_filename:完整版影片輸出位置
+		text_font :所引用字形檔案位置