main1.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. import openshot
  2. import re
  3. from PIL import Image,ImageDraw,ImageFont
  4. import pandas as pd
  5. import os
  6. import cv2
  7. import numpy as np
  8. # import moviepy.editor as mp
  9. import time
  10. import pysrt
  11. import shutil
  12. import rpyc
  13. import random
  14. import string
  15. import requests
  16. from bs4 import BeautifulSoup
  17. import zipfile
  18. import csv
  19. from datetime import datetime
  20. import rarfile
  21. import gspread
  22. from oauth2client.service_account import ServiceAccountCredentials
  23. import fire
  24. def cKey(r,g,b,fuzz):
  25. col=openshot.Color()
  26. col.red=openshot.Keyframe(r)
  27. col.green=openshot.Keyframe(g)
  28. col.blue=openshot.Keyframe(b)
  29. return openshot.ChromaKey(col, openshot.Keyframe(fuzz))
  30. def video_writer_init(path):
  31. w = openshot.FFmpegWriter(path)
  32. w.SetAudioOptions(True, "aac", 44100, 2, openshot.LAYOUT_STEREO, 3000000)
  33. w.SetVideoOptions(True, "libx264", openshot.Fraction(30000, 1000), 1280, 720,
  34. openshot.Fraction(1, 1), False, False, 3000000)
  35. return w
  36. def video_photo_clip(video=None,layer=None, position=None, end=None
  37. ,scale_x=1,scale_y=1,location_x=0,location_y=0,ck=None,audio=True):
  38. clip = openshot.Clip(video)
  39. clip.Layer(layer)
  40. clip.Position(position)
  41. clip.End(end)
  42. clip.scale_x=openshot.Keyframe(scale_x)
  43. clip.scale_y=openshot.Keyframe(scale_y)
  44. clip.location_x=openshot.Keyframe(location_x)
  45. clip.location_y=openshot.Keyframe(location_y)
  46. if ck!=None:
  47. clip.AddEffect(ck)
  48. if audio==True:
  49. clip.has_audio=openshot.Keyframe(1)
  50. else:
  51. clip.has_audio=openshot.Keyframe(0)
  52. return clip
  53. def trim_punctuation(s):
  54. pat_block = u'[^\u4e00-\u9fff0-9a-zA-Z]+'
  55. pattern = u'([0-9]+{0}[0-9]+)|{0}'.format(pat_block)
  56. res = re.sub(pattern, lambda x: x.group(1) if x.group(1) else u" " ,s)
  57. return res
  58. def randomString(stringLength=10):
  59. letters = string.ascii_lowercase
  60. return ''.join(random.choice(letters) for i in range(stringLength))
  61. def mp3_to_anchor(fname):
  62. conn = rpyc.classic.connect("192.168.192.221",18812)
  63. fr=open(fname,'rb')
  64. ropen = conn.builtins.open
  65. randname=randomString(10)
  66. finalname=randomString(10)
  67. fw=ropen('/tmp/'+randname+'.mp4','wb')
  68. fw.write(fr.read())
  69. fw.close()
  70. ros = conn.modules.os
  71. ros.system('/root/to_video/p9.sh '+randname+".mp4 "+finalname+".mp4")
  72. return 'http://192.168.192.221/video/'+finalname+'.mp4'
  73. # conn.execute('import os')
  74. def download_mp4(url):
  75. headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'}
  76. with open('input_self/AI_girl/ai_spokesgirl.mp4','wb') as f:
  77. r = requests.get(url, headers=headers, stream=True)
  78. if r.status_code == 404:
  79. return False
  80. for chunk in r.iter_content(chunk_size=1024):
  81. if chunk:
  82. f.write(chunk)
  83. return True
  84. 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"):
  85. files=os.listdir("input/")
  86. print(files)
  87. if len(files)==0:
  88. print("input資料夾沒有檔案,請放入")
  89. return
  90. elif len(files)>1:
  91. print("檔案只能有一個,且為壓縮檔")
  92. return
  93. if files[0][-4:]==".zip":
  94. input_zip="input/"+files[0]
  95. with zipfile.ZipFile(input_zip, 'r') as zf:
  96. for fn in zf.namelist():
  97. right_fn = fn.encode('cp437').decode('big5') # 將檔名正確編碼
  98. check_p=right_fn.split("/")
  99. if right_fn[-1]=="/" :
  100. os.mkdir("input/"+right_fn[:-1])
  101. pwd_use=right_fn
  102. break
  103. if len(check_p)==2:
  104. os.mkdir("input/"+check_p[0])
  105. pwd_use=check_p[0]+"/"
  106. break
  107. read_csv_use = 0
  108. with zipfile.ZipFile(input_zip, 'r') as zf:
  109. for fn in zf.namelist():
  110. right_fn = fn.encode('cp437').decode('big5') # 將檔名正確編碼
  111. check_p=right_fn.split("/")
  112. if right_fn[-1]=="/" :
  113. continue
  114. if right_fn[-4:]==".csv" or right_fn[-5:]==".xlsx":
  115. read_csv_use = right_fn
  116. with open("input/"+right_fn, 'wb') as output_file: # 建立並開啟新檔案
  117. with zf.open(fn, 'r') as origin_file: # 開啟原檔案
  118. shutil.copyfileobj(origin_file, output_file) # 將原檔案內容複製到新檔案
  119. elif files[0][-4:]==".rar":
  120. rar_ = rarfile.RarFile("input/"+files[0])
  121. names = rar_.namelist()
  122. for name in names:
  123. if name[-4:]==".csv" or name[-5:]==".xlsx":
  124. read_csv_use = name
  125. pwd_use=name.split("/")[0]+"/"
  126. rar_.extract(name,"input/")
  127. rar_.close()
  128. else:
  129. print("目前不支援此壓縮格式或不為壓縮檔")
  130. return
  131. # 去背參數
  132. # ck=cKey(0,254,0,270)
  133. ck=cKey(0,255,0,320) #綠
  134. # ck=cKey(255,0,255,320) #紫
  135. ck_anchor=None
  136. #時間
  137. time_= 0
  138. try:
  139. if read_csv_use[-4:]==".csv":
  140. csv_use=pd.read_csv("input/"+read_csv_use)
  141. elif read_csv_use[-5:]==".xlsx":
  142. csv_use = pd.read_excel("input/"+read_csv_use)
  143. except:
  144. print("excel不存在或excel格式不支援或損毀")
  145. return
  146. csv_use=csv_use.dropna(how='all')
  147. csv_use.reset_index(inplace=True)
  148. # print(csv_use)
  149. t1 = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  150. t1.Open()
  151. frames = 0
  152. for i in range(len(csv_use)):
  153. # for i in range(3):
  154. pwd_=str(csv_use.loc[i,['音檔']].values[0])
  155. locals()['anchor_music_t'+str(i)] = openshot.FFmpegReader("input/"+pwd_use+pwd_)
  156. locals()['anchor_music_t'+str(i)].Open()
  157. locals()['anchor_music_t'+str(i)+'clip'] = video_photo_clip(video=locals()['anchor_music_t'+str(i)],layer=3,scale_x=0,scale_y=0,
  158. location_x=0,location_y=0,position=time_, end=locals()['anchor_music_t'+str(i)].info.duration,ck=ck_anchor,audio=True)
  159. t1.AddClip(locals()['anchor_music_t'+str(i)+'clip'])
  160. locals()['anchor_music_t'+str(i)].Close()
  161. time_+=locals()['anchor_music_t'+str(i)].info.duration
  162. w = video_writer_init("input_self/AI_girl/%s.mp4"%('anchor_music_t'))
  163. w.Open()
  164. frames = int(t1.info.fps)*int(time_)
  165. for n in range(frames):
  166. f=t1.GetFrame(n)
  167. w.WriteFrame(f)
  168. w.Close()
  169. t1.Close()
  170. fname=mp3_to_anchor("input_self/AI_girl/%s.mp4"%('anchor_music_t'))
  171. # print(fname)
  172. time.sleep(300)
  173. while True:
  174. result = download_mp4(fname)
  175. if result:
  176. break
  177. print('等待...')
  178. time.sleep(60)
  179. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  180. t.Open()
  181. ck=cKey(0,255,0,320)
  182. ck_anchor=None
  183. #時間
  184. time_= 0
  185. try:
  186. if read_csv_use[-4:]==".csv":
  187. csv_use=pd.read_csv("input/"+read_csv_use)
  188. elif read_csv_use[-5:]==".xlsx":
  189. csv_use = pd.read_excel("input/"+read_csv_use)
  190. except:
  191. print("excel不存在或excel格式不支援或損毀")
  192. return
  193. csv_use=csv_use.dropna(how='all')
  194. csv_use.reset_index(inplace=True)
  195. anchor_op = openshot.FFmpegReader(op)
  196. anchor_op.Open()
  197. anchor_clip_op = video_photo_clip(video=anchor_op,layer=2,scale_x=1,scale_y=1,
  198. location_x=0,location_y=0,position=time_, end=anchor_op.info.duration,ck=ck_anchor,audio=True)
  199. t.AddClip(anchor_clip_op)
  200. anchor_op.Close()
  201. time_+=anchor_op.info.duration
  202. start_time = time_
  203. for i in range(len(csv_use)):
  204. # for i in range(3):
  205. pwd_=str(csv_use.loc[i,['音檔']].values[0])
  206. layer_choose = 4
  207. location_x_choose=-0.04
  208. location_y_choose=-0.04
  209. locals()['anchor_music'+str(i)] = openshot.FFmpegReader("input/"+pwd_use+pwd_)
  210. locals()['anchor_music'+str(i)].Open()
  211. locals()['anchor_music'+str(i)+'clip'] = video_photo_clip(video=locals()['anchor_music'+str(i)],layer=3,scale_x=0,scale_y=0,
  212. location_x=0,location_y=0,position=time_, end=locals()['anchor_music'+str(i)].info.duration,ck=ck_anchor,audio=True)
  213. t.AddClip(locals()['anchor_music'+str(i)+'clip'])
  214. locals()['anchor_music'+str(i)].Close()
  215. if str(csv_use.loc[i,['是否要場景']].values[0])=="是":
  216. # scale_x_use = 0.59
  217. # scale_y_use = 0.59
  218. # scale_x_use = 0.95
  219. # scale_y_use = 0.82
  220. scale_x_use = 0.8
  221. scale_y_use = 0.7
  222. location_x_choose=0
  223. location_y_choose=-0.039
  224. else:
  225. scale_x_use = 1
  226. scale_y_use = 1
  227. layer_choose = 8
  228. location_x_choose=0
  229. location_y_choose=0
  230. locals()['anchor_black'+str(i)] = openshot.QtImageReader("input_self/black.jpg")
  231. locals()['anchor_black'+str(i)].Open()
  232. locals()['anchor_black'+str(i)+'clip'] = video_photo_clip(video=locals()['anchor_black'+str(i)],layer=7,scale_x=scale_x_use,scale_y=scale_y_use,
  233. location_x=0,location_y=0,position=time_, end=locals()['anchor_music'+str(i)].info.duration,ck=ck_anchor,audio=False)
  234. t.AddClip(locals()['anchor_black'+str(i)+'clip'])
  235. locals()['anchor_black'+str(i)].Close()
  236. choose=str(csv_use.loc[i,['素材']].values[0]).split(".")[-1]
  237. pwd_p1=str(csv_use.loc[i,['素材']].values[0])
  238. if choose == 'mp4':
  239. print("input/"+pwd_use+pwd_p1)
  240. locals()['anchor'+str(i)] = openshot.FFmpegReader("input/"+pwd_use+pwd_p1)
  241. locals()['anchor'+str(i)].Open()
  242. locals()['anchor'+str(i)+'clip'] = video_photo_clip(video=locals()['anchor'+str(i)],layer=layer_choose,scale_x=scale_x_use,scale_y=scale_y_use,
  243. location_x=location_x_choose,location_y=location_y_choose,position=time_, end=locals()['anchor_music'+str(i)].info.duration,ck=ck_anchor,audio=False)
  244. t.AddClip(locals()['anchor'+str(i)+'clip'])
  245. locals()['anchor'+str(i)].Close()
  246. elif choose == 'jpg' or choose == 'png':
  247. print("input/"+pwd_use+pwd_p1)
  248. locals()['anchor'+str(i)] = openshot.QtImageReader("input/"+pwd_use+pwd_p1)
  249. locals()['anchor'+str(i)].Open()
  250. locals()['anchor'+str(i)+'clip'] = video_photo_clip(video=locals()['anchor'+str(i)],layer=layer_choose,scale_x=scale_x_use,scale_y=scale_y_use,
  251. location_x=location_x_choose,location_y=location_y_choose,position=time_, end=locals()['anchor_music'+str(i)].info.duration,ck=ck_anchor,audio=False)
  252. t.AddClip(locals()['anchor'+str(i)+'clip'])
  253. locals()['anchor'+str(i)].Close()
  254. time_+=locals()['anchor_music'+str(i)].info.duration
  255. locals()['anchor_bg'] = openshot.FFmpegReader(bg)
  256. locals()['anchor_bg'].Open()
  257. locals()['anchor_clip_bg'] = video_photo_clip(video=locals()['anchor_bg'],layer=5,scale_x=1,scale_y=1,
  258. location_x=0,location_y=0,position=start_time, end=time_-start_time,ck=ck,audio=False)
  259. t.AddClip(locals()['anchor_clip_bg'])
  260. locals()['anchor_bg'].Close()
  261. # locals()['anchor_ad'] = openshot.FFmpegReader('input_self/puuet主播_long.mp4')
  262. locals()['anchor_ad'] = openshot.FFmpegReader('input_self/AI_girl/ai_spokesgirl.mp4')
  263. locals()['anchor_ad'].Open()
  264. locals()['anchor_clip_ad'] = video_photo_clip(video=locals()['anchor_ad'],layer=6,scale_x=0.8,scale_y=0.8,
  265. location_x=0.38,location_y=0.35,position=start_time , end=time_-start_time,ck=ck,audio=False)
  266. t.AddClip(locals()['anchor_clip_ad'])
  267. locals()['anchor_ad'].Close()
  268. anchor_ed = openshot.FFmpegReader(ed)
  269. anchor_ed.Open()
  270. anchor_clip_ed = video_photo_clip(video=anchor_ed,layer=2,scale_x=1,scale_y=1,
  271. location_x=0,location_y=0,position=time_, end=anchor_ed.info.duration,ck=ck_anchor,audio=True)
  272. time_+=anchor_ed.info.duration
  273. t.AddClip(anchor_clip_ed)
  274. anchor_ed.Close()
  275. w = video_writer_init("output/no_captions/test.mp4")
  276. w.Open()
  277. frames = int(t.info.fps)*int(time_)
  278. for n in range(frames):
  279. f=t.GetFrame(n)
  280. w.WriteFrame(f)
  281. t.Close()
  282. w.Close()
  283. shutil.rmtree('input')
  284. os.mkdir('input')
  285. shutil.rmtree('input_self/AI_girl')
  286. os.mkdir('input_self/AI_girl')
  287. if save_type=="excel":
  288. vedio_to_csv()
  289. elif save_type=="sheet":
  290. vedio_to_sheet()
  291. return True
  292. #文字轉圖片
  293. def txt2image(content, save_target,lang='zh',size=26,fon="input_self/font/DFT_B7.ttc"):
  294. unicode_text = trim_punctuation(content)
  295. font = ''
  296. if lang=='zh':
  297. font = ImageFont.truetype(font=fon, size=size)
  298. else :
  299. font = ImageFont.truetype(font="input_self/font/arial.ttf", size=size)
  300. W, H = (1280,500)
  301. canvas = Image.new('RGB', (W, H), "#00FF00")
  302. draw = ImageDraw.Draw(canvas)
  303. text= content
  304. if "\n" in text:
  305. w, h = draw.textsize(text.split("\n")[0],font = font)
  306. #draw.text(((W-w)/2,0), text[0:18],'black', font)
  307. text_border(draw,(W-w)/2,0,text.split("\n")[0],font,'black','white')
  308. w, h = draw.textsize(text.split("\n")[1],font = font)
  309. #draw.text(((W-w)/2,h+2), text[18:],'black', font)
  310. text_border(draw,(W-w)/2,h+2,text.split("\n")[1],font,'black','white')
  311. else:
  312. w, h = draw.textsize(content,font = font)
  313. #draw.text(((W-w)/2,0), text,'black', font)
  314. text_border(draw,(W-w)/2,0,text,font,'black','white')
  315. canvas.save(save_target, "PNG")
  316. def text_border(draw,x,y,text,font,shadowcolor,fillcolor):
  317. draw.text((x-1, y), text, font=font, fill=shadowcolor)
  318. draw.text((x+1, y), text, font=font, fill=shadowcolor)
  319. draw.text((x, y-1), text, font=font, fill=shadowcolor)
  320. draw.text((x, y+1), text, font=font, fill=shadowcolor)
  321. draw.text((x-1, y+1), text, font=font, fill=shadowcolor)
  322. draw.text((x+1, y-1), text, font=font, fill=shadowcolor)
  323. draw.text((x-1, y-1), text, font=font, fill=shadowcolor)
  324. draw.text((x+1, y+1), text, font=font, fill=shadowcolor)
  325. # thicker border
  326. draw.text((x-2, y-2), text, font=font, fill=shadowcolor)
  327. draw.text((x+2, y-2), text, font=font, fill=shadowcolor)
  328. draw.text((x-2, y+2), text, font=font, fill=shadowcolor)
  329. draw.text((x+2, y+2), text, font=font, fill=shadowcolor)
  330. # now draw the text over it
  331. draw.text((x, y), text, font=font, fill=fillcolor)
  332. def srt_to_csv(srt_file,sound_file="output/csv_produce/test.csv"):
  333. subs = pysrt.open(srt_file)
  334. # csv_file = srt_file.split('.')[0] + ".csv"
  335. with open(sound_file, 'w', newline='',encoding="big5") as csvfile:
  336. # 建立 CSV 檔寫入器
  337. writer = csv.writer(csvfile)
  338. writer.writerow(["序號","開始時間","結束時間","字幕內容"])
  339. for context in subs:
  340. writer.writerow([context.index, context.start,context.end, context.text])
  341. return
  342. def csv_to_text(csv_file,text_font):
  343. text_form = []
  344. with open(csv_file, newline='',encoding="big5") as csvfile:
  345. # 讀取 CSV 檔案內容
  346. rows = csv.reader(csvfile)
  347. # 以迴圈輸出每一列
  348. for row in rows:
  349. start = datetime.strptime(row[1], "%H:%M:%S,%f")
  350. end = datetime.strptime(row[2], "%H:%M:%S,%f") - datetime.strptime(row[1], "%H:%M:%S,%f")
  351. end_timeStamp=end.seconds+0.000001*end.microseconds
  352. start_timeStamp=start.minute*60+start.second+ 0.000001*start.microsecond
  353. text_form.append({'text':row[3],'start':start_timeStamp,'end':end_timeStamp,'size':36,'font':text_font})
  354. return text_form
  355. def text_to_short_vedio(mp4_file ,sound_file,output_filename,text_font):
  356. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  357. t.Open()
  358. # 去背參數
  359. ck = cKey(0, 254, 0, 270)
  360. ck_anchor = cKey(0, 255, 0, 320)
  361. anchor = openshot.FFmpegReader(mp4_file)
  362. anchor.Open()
  363. anchor_clip = video_photo_clip(video=anchor,layer=2,scale_x=1,scale_y=1,
  364. location_x=0,location_y=0,position=0, end=anchor.info.duration,audio=True)
  365. t.AddClip(anchor_clip)
  366. anchor.Close()
  367. number = 0
  368. sound_srt_file = ""
  369. #音檔自動產生srt(逐字稿)
  370. if ".srt" in sound_file:
  371. sound_srt_file = sound_file
  372. elif not sound_file is None:
  373. cmd = "autosub -S zh-TW -D zh-TW " + sound_file
  374. os.system(cmd)
  375. sound_srt_file = sound_file.split('.')[0] + ".srt"
  376. csv_file = srt_to_csv(sound_srt_file)
  377. text_form = csv_to_text(csv_file,text_font)
  378. print(sound_srt_file)
  379. #開啟srt檔
  380. try:
  381. number = 0
  382. for text_tmp in text_form:
  383. file_name = "input_self/tmp/save_target_" + str(number) + ".png"
  384. txt2image(text_tmp['text'], file_name,lang='zh',size = text_tmp['size'],fon = text_tmp['font'])
  385. exec('text_anchor_{} = openshot.QtImageReader("input_self/tmp/save_target_{}.png")'.format(number,number))
  386. exec('text_anchor_{}.Open()'.format(number))
  387. exec('text_anchor_{}.Open()'.format(number))
  388. exec('text_anchor_clip_{} = video_photo_clip(video=text_anchor_{},layer=4,scale_x=1,scale_y=1,\
  389. location_x=0,location_y=0.78,position=text_tmp["start"], end=text_tmp["end"],ck=ck_anchor,audio=True)'.format(number,number))
  390. exec('t.AddClip(text_anchor_clip_{})'.format(number))
  391. exec('text_anchor_{}.Close()'.format(number))
  392. number = number+1
  393. except:
  394. print("無法開啟srt檔案(字幕產生失敗)")
  395. w = video_writer_init(output_filename)
  396. w.Open()
  397. frames = int(t.info.fps)*int(anchor.info.duration)
  398. for n in range(frames):
  399. f=t.GetFrame(n)
  400. w.WriteFrame(f)
  401. t.Close()
  402. w.Close()
  403. # 刪除暫存檔案
  404. # shutil.rmtree('input_self/tmp')
  405. # os.mkdir('input_self/tmp')
  406. # shutil.rmtree('input_self/tmp1')
  407. # os.mkdir('input_self/tmp1')
  408. def auth_gss_client(path, scopes):
  409. credentials = ServiceAccountCredentials.from_json_keyfile_name(path, scopes)
  410. return gspread.authorize(credentials)
  411. auth_json_path = 'noted-tesla-348011-74f70c9caeda.json' #由剛剛建立出的憑證,放置相同目錄以供引入
  412. gss_scopes = ['https://spreadsheets.google.com/feeds'] #我們想要取用的範圍
  413. gss_client = auth_gss_client(auth_json_path, gss_scopes) #呼叫我們的函式
  414. #從剛剛建立的sheet,把網址中 https://docs.google.com/spreadsheets/d/〔key〕/edit 的 〔key〕的值代入
  415. spreadsheet_key_path = '1LU5O8-oAotIFGPI9STPbElO0NHGA6eynuv9sYz81aOw'
  416. wks = gss_client.open_by_key(spreadsheet_key_path).sheet1
  417. def srt_to_sheet(srt_file):
  418. subs = pysrt.open(srt_file)
  419. wks.clear()
  420. index = 0
  421. for context in subs:
  422. index = context.index
  423. cell_list = wks.range('A1:C'+str(index))
  424. number = 0
  425. for context in subs:
  426. #print(context.start.minutes*60+context.start.seconds+ 0.001*context.start.milliseconds)
  427. index = context.index
  428. end = context.end
  429. start = context.start
  430. #print('A'+str(index))
  431. cell_list[number].value = str(start)
  432. cell_list[number+1].value = str(end)
  433. cell_list[number+2].value = str(context.text)
  434. number = number+3
  435. wks.update_cells(cell_list)
  436. def vedio_to_csv(sound_file = "output/no_captions/test.mp4"):
  437. cmd = "autosub -S zh-TW -D zh-TW " + sound_file
  438. os.system(cmd)
  439. sound_srt_file = sound_file.split('.')[0] + ".srt"
  440. srt_to_csv(sound_srt_file)
  441. def vedio_to_sheet():
  442. cmd = "autosub -S zh-TW -D zh-TW " + sound_file
  443. os.system(cmd)
  444. sound_srt_file = sound_file.split('.')[0] + ".srt"
  445. srt_to_sheet(sound_srt_file)
  446. if __name__ == '__main__':
  447. # fire.Fire()
  448. # text_to_short_vedio_create(
  449. # op="input_self/LOGO_OP_4.mp4",
  450. # ed="input_self/LOGO_ED.mp4",
  451. # bg="input_self/xchoozmo_long.mp4",
  452. # input_zip="input/input_data.zip",
  453. # save_type="excel")
  454. fire.Fire(text_to_short_vedio_create)
  455. # fire.Fire(text_to_short_vedio_create(
  456. # op="input_self/LOGO_OP_4.mp4",
  457. # ed="input_self/LOGO_ED.mp4",
  458. # bg="input_self/xchoozmo_long.mp4",
  459. # input_zip="input/input_data.zip",
  460. # save_type="excel"))
  461. # vedio_to_csv("output/no_captions/test.mp4")