openshot_video_generator.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. from os import listdir
  2. from os.path import isfile, isdir, join
  3. import openshot
  4. import threading
  5. import zhtts
  6. import os
  7. import urllib
  8. from typing import List
  9. import requests
  10. from pydantic import BaseModel
  11. from bs4 import BeautifulSoup
  12. from PIL import Image,ImageDraw,ImageFont
  13. import pyttsx3
  14. import rpyc
  15. import random
  16. import re
  17. import time
  18. import math
  19. import dataset
  20. from datetime import datetime
  21. from gtts import gTTS
  22. import ffmpy
  23. from difflib import SequenceMatcher
  24. import difflib
  25. from autosub import DEFAULT_CONCURRENCY
  26. from autosub import DEFAULT_SUBTITLE_FORMAT
  27. from pytranscriber.control.ctr_main import Ctr_Main
  28. from pytranscriber.control.ctr_autosub import Ctr_Autosub
  29. import multiprocessing
  30. from itertools import groupby
  31. from operator import itemgetter
  32. dir_sound = 'mp3_track/'
  33. dir_photo = 'photo/'
  34. dir_text = 'text_file/'
  35. dir_video = 'video_material/'
  36. dir_title = 'title/'
  37. dir_subtitle = 'subtitle/'
  38. dir_anchor = 'anchor_raw/'
  39. tmp_video_dir = 'tmp_video/'
  40. video_sub_folder = 'ai_anchor_video/'
  41. dir_list = [dir_sound,dir_photo,dir_text,dir_video,dir_title,dir_subtitle,dir_anchor,tmp_video_dir]
  42. def notify_group(msg):
  43. glist=['7vilzohcyQMPLfAMRloUawiTV4vtusZhxv8Czo7AJX8','WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD','1dbtJHbWVbrooXmQqc4r8OyRWDryjD4TMJ6DiDsdgsX','HOB1kVNgIb81tTB4Ort1BfhVp9GFo6NlToMQg88vEhh']
  44. for gid in glist:
  45. headers = {
  46. "Authorization": "Bearer " + gid,
  47. "Content-Type": "application/x-www-form-urlencoded"
  48. }
  49. params = {"message": msg}
  50. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
  51. def cKey(r,g,b,fuzz):
  52. col=openshot.Color()
  53. col.red=openshot.Keyframe(r)
  54. col.green=openshot.Keyframe(g)
  55. col.blue=openshot.Keyframe(b)
  56. return openshot.ChromaKey(col, openshot.Keyframe(fuzz))
  57. def video_photo_clip(vid=None,layer=None, position=None, end=None
  58. ,scale_x=1,scale_y=1,location_x=0,location_y=0,ck=None,audio=True):
  59. clip = openshot.Clip(vid)
  60. clip.Layer(layer)
  61. clip.Position(position)
  62. clip.End(end)
  63. clip.scale_x=openshot.Keyframe(scale_x)
  64. clip.scale_y=openshot.Keyframe(scale_y)
  65. clip.location_x=openshot.Keyframe(location_x)
  66. clip.location_y=openshot.Keyframe(location_y)
  67. if ck!=None:
  68. clip.AddEffect(ck)
  69. if audio==True:
  70. clip.has_audio=openshot.Keyframe(1)
  71. else:
  72. clip.has_audio=openshot.Keyframe(0)
  73. return clip
  74. def listener_progress(string, percent):
  75. True
  76. def myunichchar(unicode_char):
  77. mb_string = unicode_char.encode('big5')
  78. try:
  79. unicode_char = unichr(ord(mb_string[0]) << 8 | ord(mb_string[1]))
  80. except NameError:
  81. unicode_char = chr(mb_string[0] << 8 | mb_string[1])
  82. return unicode_char
  83. def get_url_type(url):
  84. req = urllib.request.Request(url, method='HEAD', headers={'User-Agent': 'Mozilla/5.0'})
  85. r = urllib.request.urlopen(req)
  86. contentType = r.getheader('Content-Type')
  87. return contentType
  88. def make_dir(name_hash):
  89. for direct in dir_list:
  90. if not os.path.isdir(direct):
  91. os.mkdir(direct)
  92. try:
  93. os.mkdir(dir_photo+name_hash)
  94. except FileExistsError:
  95. print("~~~~~~Warning~~~~~~~~~Directory " , dir_photo+name_hash , " already exists")
  96. try:
  97. os.mkdir(dir_text+name_hash)
  98. except FileExistsError:
  99. print("~~~~~~Warning~~~~~~~~~Directory " , dir_text+name_hash , " already exists")
  100. try:
  101. os.mkdir(dir_sound+name_hash)
  102. except FileExistsError:
  103. print("~~~~~~Warning~~~~~~~~~Directory " , dir_sound+name_hash , " already exists")
  104. try:
  105. os.mkdir(dir_anchor+name_hash)
  106. except FileExistsError:
  107. print("~~~~~~Warning~~~~~~~~~Directory " , dir_anchor+name_hash , " already exists")
  108. try:
  109. os.mkdir(dir_subtitle+name_hash)
  110. except FileExistsError:
  111. print("~~~~~~Warning~~~~~~~~~Directory " , dir_subtitle+name_hash , " already exists")
  112. def file_prepare(name, name_hash,text_content,image_urls,multiLang,lang='zh'):
  113. make_dir(name_hash)
  114. img_num = 1
  115. for imgu in image_urls:
  116. if get_url_type(imgu) =='video/mp4':
  117. r=requests.get(imgu)
  118. f=open(dir_photo+name_hash+"/"+str(img_num)+".mp4",'wb')
  119. for chunk in r.iter_content(chunk_size=255):
  120. if chunk:
  121. f.write(chunk)
  122. f.close()
  123. else:
  124. im = Image.open(requests.get(imgu, stream=True).raw)
  125. im= im.convert("RGB")
  126. im.save(dir_photo+name_hash+"/"+str(img_num)+".jpg")
  127. img_num+=1
  128. #save text
  129. txt_idx=0
  130. for txt in text_content:
  131. text_file = open(dir_text+name_hash+"/"+str(txt_idx)+".txt", "w")
  132. text_file.write(txt)
  133. text_file.close()
  134. txt_idx+=1
  135. print("text file made")
  136. #make mp3
  137. txt_idx = 0
  138. for txt in text_content:
  139. if lang!='zh' or multiLang==1:
  140. if lang!='zh':
  141. tts = gTTS(txt)
  142. tts.save(dir_sound+name_hash+"/"+str(txt_idx)+"raw.mp3")
  143. else:
  144. tts = gTTS(txt,lang='zh-tw')
  145. tts.save(dir_sound+name_hash+"/"+str(txt_idx)+"raw.mp3")
  146. #speed up
  147. ff = ffmpy.FFmpeg(inputs={dir_sound+name_hash+"/"+str(txt_idx)+"raw.mp3": None}
  148. , outputs={dir_sound+name_hash+"/"+str(txt_idx)+".mp3": ["-filter:a", "atempo=1.2"]})
  149. ff.run()
  150. os.remove(dir_sound+name_hash+"/"+str(txt_idx)+"raw.mp3")
  151. else:
  152. print('use zhtts')
  153. tts = zhtts.TTS()
  154. tts.text2wav(txt,dir_sound+name_hash+"/"+str(txt_idx)+".mp3")
  155. txt_idx+=1
  156. print("mp3 file made")
  157. #make title as image
  158. txt2image_title(name, dir_title+name_hash+".png",lang)
  159. def file_prepare_long(name, name_hash,text_content,image_urls,multiLang,lang='zh'):
  160. make_dir(name_hash)
  161. img_num = 1
  162. for imgu in image_urls:
  163. if get_url_type(imgu) =='video/mp4':
  164. r=requests.get(imgu)
  165. f=open(dir_photo+name_hash+"/"+str(img_num)+".mp4",'wb')
  166. for chunk in r.iter_content(chunk_size=255):
  167. if chunk:
  168. f.write(chunk)
  169. f.close()
  170. else:
  171. im = Image.open(requests.get(imgu, stream=True).raw)
  172. im= im.convert("RGB")
  173. im.save(dir_photo+name_hash+"/"+str(img_num)+".jpg")
  174. img_num+=1
  175. #make mp3
  176. txt_idx = 0
  177. for txt in text_content:
  178. txt = txt.replace
  179. if lang!='zh' or multiLang==1:
  180. if lang!='zh':
  181. tts = gTTS(txt)
  182. tts.save(dir_sound+name_hash+"/"+str(txt_idx)+"raw.mp3")
  183. else:
  184. tts = gTTS(txt,lang='zh-tw')
  185. tts.save(dir_sound+name_hash+"/"+str(txt_idx)+"raw.mp3")
  186. #speed up
  187. ff = ffmpy.FFmpeg(inputs={dir_sound+name_hash+"/"+str(txt_idx)+"raw.mp3": None}
  188. , outputs={dir_sound+name_hash+"/"+str(txt_idx)+".mp3": ["-filter:a", "atempo=1.2"]})
  189. ff.run()
  190. os.remove(dir_sound+name_hash+"/"+str(txt_idx)+"raw.mp3")
  191. else:
  192. print('use zhtts')
  193. tts = zhtts.TTS()
  194. tts.text2wav(txt,dir_sound+name_hash+"/"+str(txt_idx)+".mp3")
  195. txt_idx+=1
  196. print("mp3 file made")
  197. #make title as image
  198. txt2image_title(name, dir_title+name_hash+".png",lang)
  199. def txt2image(content, save_target,lang='zh'):
  200. unicode_text = trim_punctuation(content)
  201. font = ''
  202. if lang=='zh':
  203. font = ImageFont.truetype(font="font/DFT_B7.ttc", size=38)
  204. else :
  205. font = ImageFont.truetype(font="font/arial.ttf", size=38)
  206. text_width, text_height = font.getsize(unicode_text)
  207. canvas = Image.new('RGBA', (700, 500), (255, 0, 0, 0) )
  208. draw = ImageDraw.Draw(canvas)
  209. text= unicode_text
  210. draw.text((5,5), text, (255, 255, 0), font)
  211. canvas.save(save_target, "PNG")
  212. def txt2image_title(content, save_target, lang='zh'):
  213. unicode_text = trim_punctuation(content)
  214. font = ''
  215. if lang=='zh':
  216. font = ImageFont.truetype(font="font/DFT_B7.ttc", size=22)
  217. else :
  218. font = ImageFont.truetype(font="font/arial.ttf", size=22)
  219. text_width, text_height = font.getsize(unicode_text)
  220. canvas = Image.new('RGBA', (510, 500), (255, 0, 0, 0) )
  221. draw = ImageDraw.Draw(canvas)
  222. text= unicode_text
  223. draw.text((5,5), text, (17, 41, 167), font)
  224. canvas.save(save_target, "PNG")
  225. def call_anchor(fileName,avatar):
  226. conn = rpyc.classic.connect("192.168.1.105",18812)
  227. ros = conn.modules.os
  228. rsys = conn.modules.sys
  229. fr=open(dir_sound+fileName+".mp3",'rb')# voice
  230. #warning!!! file my be replaced by other process
  231. fw=conn.builtins.open('/tmp/output.mp3','wb')
  232. while True:
  233. b=fr.read(1024)
  234. if b:
  235. fw.write(b)
  236. else:
  237. break
  238. fr.close()
  239. fw.close()
  240. val=random.randint(1000000,9999999)
  241. ros.chdir('/home/jared/to_video')
  242. ros.system('./p'+str(avatar)+'.sh '+str(val)+' &')
  243. while True:
  244. print('waiting...')
  245. if ros.path.exists('/tmp/results/'+str(val)):
  246. break
  247. time.sleep(5)
  248. print('waiting...')
  249. fr=conn.builtins.open('/tmp/results/'+str(val)+'.mp4','rb')
  250. fw=open(dir_anchor+fileName+".mp4",'wb')
  251. while True:
  252. b=fr.read(1024)
  253. if b:
  254. fw.write(b)
  255. else:
  256. break
  257. fr.close()
  258. fw.close()
  259. def syllable_count(word):
  260. word = word.lower()
  261. count = 0
  262. vowels = "aeiouy"
  263. if word[0] in vowels:
  264. count += 1
  265. for index in range(1, len(word)):
  266. if word[index] in vowels and word[index - 1] not in vowels:
  267. count += 1
  268. if word.endswith("e"):
  269. count -= 1
  270. if count == 0:
  271. count += 1
  272. return count
  273. def split_sentence(in_str, maxLen):
  274. re.findall(r'[\u4e00-\u9fff]+', in_str)
  275. zh_idx = []
  276. eng_idx= []
  277. for i in range(len(in_str)):
  278. if in_str[i] > u'\u4e00' and in_str[i] < u'\u9fff':
  279. zh_idx.append(i)
  280. else:
  281. eng_idx.append(i)
  282. space_index = [m.start() for m in re.finditer(' ', in_str)]
  283. for idx in space_index:
  284. eng_idx.remove(idx)
  285. eng_range_list = []
  286. for k, g in groupby(enumerate(eng_idx), lambda ix : ix[0] - ix[1]):
  287. eng_range = list(map(itemgetter(1), g))
  288. eng_range_list.append(eng_range)
  289. total_syllable = 0
  290. for i in range(len(eng_range_list)):
  291. total_syllable += (syllable_count(in_str[eng_range_list[i][0]:eng_range_list[i][-1]+1])+0.5)
  292. for i in range(len(zh_idx)):
  293. total_syllable+=1
  294. #final chchchchchc[en][en][en]
  295. #[en] is a vocabulary dict with occurence of image
  296. zh_eng_idx_list = []
  297. i = 0
  298. while i < len(in_str):
  299. if in_str[i]==' ':
  300. i+=1
  301. if i in zh_idx:
  302. zh_eng_idx_list.append(i)
  303. i+=1
  304. if i in eng_idx:
  305. for ls in eng_range_list:
  306. if i in ls:
  307. zh_eng_idx_list.append(ls)
  308. i = ls[-1]+1
  309. break
  310. zh_eng_dict_list = [{'content':'','time_ratio':0}]
  311. idx = 0
  312. current_len = 0
  313. sen_idx = 0
  314. while idx < len(zh_eng_idx_list):
  315. str_from_idx = ''
  316. sylla_cnt = 1
  317. if type(zh_eng_idx_list[idx])==type([]):
  318. str_from_idx = in_str[zh_eng_idx_list[idx][0]:zh_eng_idx_list[idx][-1]+1]+' '
  319. sylla_cnt = syllable_count(str_from_idx)
  320. else:
  321. str_from_idx = in_str[zh_eng_idx_list[idx]]
  322. if len(zh_eng_dict_list[sen_idx]['content'])+sylla_cnt>=maxLen:
  323. zh_eng_dict_list[sen_idx]['time_ratio'] = current_len/total_syllable
  324. zh_eng_dict_list.append({'content':'','time_ratio':0})
  325. sen_idx+=1
  326. current_len = 0
  327. else:
  328. current_len += sylla_cnt
  329. zh_eng_dict_list[sen_idx]['content'] += str_from_idx
  330. idx+=1
  331. total_ratio = 0
  332. for obj in zh_eng_dict_list:
  333. total_ratio+=obj['time_ratio']
  334. zh_eng_dict_list[-1]['time_ratio'] = 1-total_ratio
  335. return zh_eng_dict_list
  336. def parse_script(file_path,gt_list):
  337. with open(file_path, 'r',encoding="utf-8") as f:
  338. raw_lines = [line.strip() for line in f]
  339. lines = adjustSub_by_text_similarity(gt_list,raw_lines)
  340. #make dict
  341. dict_list = []
  342. for idx in range(len(lines)):
  343. script={}
  344. script['content'] = lines[idx]
  345. time_raw = raw_lines[idx * 4 +1 ].split(' --> ')
  346. start = time_raw[0].split(':')
  347. stop = time_raw[1].split(':')
  348. script['start'] = float(start[0])*3600 + float(start[1])*60 + float(start[2].replace(',','.'))
  349. script['stop'] = float(stop[0])*3600 + float(stop[1])*60 + float(stop[2].replace(',','.'))
  350. dict_list.append(script)
  351. #merge duplicated sentences
  352. script_not_dup_list = []
  353. for idx in range(len(dict_list)):
  354. dup_list = []
  355. for idx_inner in range(len(dict_list)):
  356. if dict_list[idx_inner]['content']==dict_list[idx]['content']:
  357. dup_list.append(idx_inner)
  358. for dup_idx in dup_list:
  359. if dup_idx == min(dup_list):
  360. dict_list[dup_idx]['type'] = 'lead_sentence'
  361. else:
  362. dict_list[dup_idx]['type'] = 'duplicated'
  363. dict_list[dup_list[0]]['stop'] = dict_list[dup_list[-1]]['stop']
  364. if dict_list[idx]['type'] == 'lead_sentence':
  365. script_not_dup_list.append(dict_list[idx])
  366. #avoid subtitle overlapping ? Timeline overlapping not found currently
  367. #cut by max length----> eng seperated problem {eng_idx}
  368. #ENG counts, zh counts, space counts
  369. new_idx = 0
  370. splitted_dict = []
  371. for dic in script_not_dup_list:
  372. dic_idx = 0
  373. accumulated_duration = 0
  374. duration = dic['stop']-dic['start']
  375. print(duration)
  376. for sub_dic in split_sentence(dic['content'],13):
  377. new_dic = {}
  378. new_dic['index'] = new_idx
  379. new_idx+=1
  380. ind_duration = duration * sub_dic['time_ratio']
  381. new_dic['start'] = dic['start'] + accumulated_duration
  382. accumulated_duration += ind_duration
  383. new_dic['content'] = sub_dic['content']
  384. new_dic['duration'] = ind_duration*0.7
  385. splitted_dict.append(new_dic)
  386. return splitted_dict
  387. def adjustSub_by_text_similarity(gts,gens_raw):
  388. gens = []
  389. for idx in range(int((len(gens_raw)+1)/4)):
  390. gens.append(gens_raw[idx*4+2])
  391. combine2 = [''.join([i,j]) for i,j in zip(gts, gts[1:])]
  392. combine3 = [''.join([i,j,k]) for i,j,k in zip(gts, gts[1:], gts[2:])]
  393. alls = gts + combine2 + combine3
  394. adjusted = [None]*len(gens)
  395. duplicated_list = []
  396. for idx in range(len(gens)):
  397. match_text = difflib.get_close_matches(gens[idx], alls, cutoff=0.1)
  398. if match_text[0] in duplicated_list:
  399. for mt in match_text:
  400. if mt == adjusted[idx-1] or mt not in duplicated_list:
  401. adjusted[idx] = mt
  402. break
  403. else:
  404. adjusted[idx] = match_text[0]
  405. duplicated_list.append(match_text[0])
  406. return adjusted
  407. def trim_punctuation(s):
  408. pat_block = u'[^\u4e00-\u9fff0-9a-zA-Z]+';
  409. pattern = u'([0-9]+{0}[0-9]+)|{0}'.format(pat_block)
  410. res = re.sub(pattern, lambda x: x.group(1) if x.group(1) else u" " ,s)
  411. return res
  412. def splitter(s):
  413. for sent in re.findall(u'[^!?,。\!\?]+[!? 。\!\?]?', s, flags=re.U):
  414. yield sent
  415. def split_by_pun(s):
  416. res = list(splitter(s))
  417. return res
  418. def generate_subtitle_image_from_dict(name_hash, sub_dict):
  419. for script in sub_dict:
  420. sv_path = dir_subtitle + name_hash + '/' + str(script['index'])+'.png'
  421. sub = script['content']
  422. txt2image(sub,sv_path)
  423. def generate_subtitle_image(name_hash,text_content):
  424. img_list = [None]*len(text_content)
  425. for idx in range(len(text_content)):
  426. img_list[idx]=[]
  427. senList = split_by_pun(text_content[idx])
  428. for inner_idx in range(len(senList)):
  429. sv_path = dir_subtitle + name_hash +'/'+str(idx)+ str(inner_idx) +'.png'
  430. sub = senList[inner_idx]
  431. txt2image(sub,sv_path)
  432. img_list[idx]+=[{"count":len(sub),"path":sv_path}]
  433. return img_list
  434. def generate_subtitle_image_ENG(name_hash,text_content):
  435. img_list = [None]*len(text_content)
  436. for idx in range(len(text_content)):
  437. sv_path = dir_subtitle + name_hash +'/'+str(idx)+'.png'
  438. sub = text_content[idx]
  439. txt2image(sub, sv_path,lang='eng')
  440. img_list[idx] = sv_path
  441. return img_list
  442. def video_writer_init(path):
  443. w = openshot.FFmpegWriter(path)
  444. w.SetAudioOptions(True, "aac", 44100, 2, openshot.LAYOUT_STEREO, 3000000)
  445. w.SetVideoOptions(True, "libx264", openshot.Fraction(30000, 1000), 1280, 720,
  446. openshot.Fraction(1, 1), False, False, 3000000)
  447. return w
  448. def video_gen(name_hash,name,text_content, image_urls,multiLang,avatar):
  449. file_prepare_long(name, name_hash, text_content,image_urls,multiLang)
  450. for fname in range(len(text_content)):
  451. call_anchor(name_hash+"/"+str(fname),avatar)
  452. print('called............................................')
  453. ck=cKey(0,254,0,270)
  454. ck_anchor=cKey(0,255,1,320)
  455. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  456. t.Open()
  457. main_timer = 0
  458. LOGO_OP = openshot.FFmpegReader(dir_video+"LOGO_OP_4.mp4")
  459. LOGO_OP.Open() # Open the reader
  460. head_duration = LOGO_OP.info.duration
  461. LOGO_OP_clip = video_photo_clip(vid=LOGO_OP,layer=4,position=0,end=head_duration
  462. ,location_y=-0.03,scale_x=0.8,scale_y=0.704)
  463. t.AddClip(LOGO_OP_clip)
  464. bg_head = openshot.FFmpegReader(dir_video+"complete_head_aispokesgirl.mp4")
  465. bg_head.Open()
  466. bg_head_clip = video_photo_clip(vid=bg_head,layer=2,position=0,end=LOGO_OP.info.duration,ck=ck)
  467. t.AddClip(bg_head_clip)
  468. main_timer += head_duration
  469. bg_head.Close()
  470. LOGO_OP.Close()
  471. anchor = openshot.FFmpegReader(dir_anchor+name_hash+"/0.mp4")
  472. anchor.Open()
  473. #anchor_clip = video_photo_clip(vid=anchor,layer=4,scale_x=0.65,scale_y=0.65,
  474. # location_x=0.35,location_y=0.25,position=main_timer, end=anchor.info.duration,ck=ck_anchor,audio=False)
  475. #t.AddClip(anchor_clip)
  476. speech = openshot.FFmpegReader(dir_sound+name_hash+"/0.mp3")
  477. speech.Open()
  478. speech_clip = openshot.Clip(speech)
  479. speech_clip.Position(main_timer)
  480. speech_clip.End(anchor.info.duration)
  481. t.AddClip(speech_clip)
  482. main_timer += anchor.info.duration
  483. anchor.Close()
  484. speech.Close()
  485. LOGO_ED = openshot.FFmpegReader(dir_video+"LOGO_ED.avi")
  486. LOGO_ED.Open()
  487. LOGO_ED_clip = video_photo_clip(vid=LOGO_ED,layer=4,position=main_timer,end=LOGO_ED.info.duration
  488. ,location_x=0.005,location_y=-0.031, scale_x=0.8,scale_y=0.6825)
  489. t.AddClip(LOGO_ED_clip)
  490. main_timer += LOGO_ED.info.duration
  491. LOGO_ED.Close()
  492. bg = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  493. bg.Open()
  494. bg_times = math.floor(main_timer/bg.info.duration)
  495. left_time = (main_timer) % bg.info.duration
  496. bg_clip_list = [None] * bg_times
  497. bg_list = [None] * bg_times
  498. bg.Close()
  499. bg_timer = head_duration
  500. for idx in range(bg_times):
  501. bg_list[idx] = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  502. bg_list[idx].Open()
  503. bg_clip_list[idx] = video_photo_clip(bg_list[idx],layer=2,position=bg_timer,end=bg_list[idx].info.duration,ck=ck)
  504. t.AddClip(bg_clip_list[idx])
  505. bg_timer += bg_list[idx].info.duration
  506. bg_list[idx].Close()
  507. bg_left = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  508. bg_left.Open()
  509. bg_left_clip = video_photo_clip(bg_left,layer=2,position=bg_timer,end=left_time,ck=ck)
  510. t.AddClip(bg_left_clip)
  511. bg_left.Close()
  512. title = openshot.QtImageReader(dir_title+name_hash+".png")
  513. title.Open() # Open the reader
  514. title_clip = video_photo_clip(vid=title, layer=4,location_x=-0.047, location_y=0.801,position=0,end=head_duration+main_timer)
  515. t.AddClip(title_clip)
  516. w = video_writer_init(tmp_video_dir+name_hash+"raw.mp4")
  517. w.Open()
  518. frames = int(t.info.fps)*int(main_timer)
  519. for n in range(frames):
  520. f=t.GetFrame(n)
  521. w.WriteFrame(f)
  522. t.Close()
  523. w.Close()
  524. print(name+"RAW DONE : www.choozmo.com:8168/"+tmp_video_dir+name_hash+"raw.mp4")
  525. #start adding sub
  526. #add sub
  527. Ctr_Autosub.init()
  528. Ctr_Autosub.generate_subtitles(tmp_video_dir+name_hash+"raw.mp4",'zh',listener_progress,output=tmp_video_dir+name_hash+"script.txt",concurrency=DEFAULT_CONCURRENCY,subtitle_file_format=DEFAULT_SUBTITLE_FORMAT)
  529. sub_dict = parse_script(tmp_video_dir+name_hash+"script.txt",split_by_pun(text_content[0]))
  530. for subd in sub_dict:
  531. print(subd)
  532. generate_subtitle_image_from_dict(name_hash, sub_dict)
  533. #sv_path = dir_subtitle + name_hash + '/' + str(script['index'])+'.png'
  534. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  535. t.Open()
  536. raw = openshot.FFmpegReader(tmp_video_dir+name_hash+"raw.mp4")
  537. raw.Open()
  538. raw_clip = video_photo_clip(vid=raw,layer=2,position=0, end=raw.info.duration)
  539. t.AddClip(raw_clip)
  540. sub_img_list = [None] * len(sub_dict)
  541. sub_clip_list = [None] * len(sub_dict)
  542. for sub_obj in sub_dict:
  543. idx = int(sub_obj['index'])
  544. sub_img_list[idx] = openshot.QtImageReader(dir_subtitle + name_hash + '/' + str(idx)+'.png')
  545. sub_img_list[idx].Open()
  546. sub_clip_list[idx] = video_photo_clip(vid=sub_img_list[idx], layer=6,location_x=0.069, location_y=0.89,position=sub_obj['start'],end=math.ceil(sub_obj['duration']))
  547. t.AddClip(sub_clip_list[idx])
  548. sub_img_list[idx].Close()
  549. anchor = openshot.FFmpegReader(dir_anchor+name_hash+"/0.mp4")
  550. anchor.Open()
  551. anchor_clip = video_photo_clip(vid=anchor,layer=4,scale_x=0.65,scale_y=0.65,
  552. location_x=0.35,location_y=0.25,position=head_duration, end=anchor.info.duration,ck=ck_anchor,audio=False)
  553. t.AddClip(anchor_clip)
  554. w = video_writer_init(tmp_video_dir+name_hash+".mp4")
  555. w.Open()
  556. frames = int(t.info.fps)*int(main_timer)
  557. for n in range(frames):
  558. f=t.GetFrame(n)
  559. w.WriteFrame(f)
  560. t.Close()
  561. w.Close()
  562. os.remove(tmp_video_dir+name_hash+"raw.mp4")
  563. os.remove(tmp_video_dir+name_hash+"script.txt")
  564. print(name+"ALL DONE : www.choozmo.com:8168/"+video_sub_folder+name_hash+"raw.mp4")
  565. def anchor_video_v2(name_hash,name,text_content, image_urls,multiLang,avatar):
  566. print(os.getcwd())
  567. print('sub image made')
  568. print(multiLang)
  569. file_prepare(name, name_hash, text_content,image_urls,multiLang)
  570. sub_list=generate_subtitle_image(name_hash,text_content)
  571. for fname in range(len(text_content)):
  572. call_anchor(name_hash+"/"+str(fname),avatar)
  573. print('step finish')
  574. print('called............................................')
  575. ck=cKey(0,254,0,270)
  576. ck_anchor=cKey(0,255,1,320)
  577. duration = 0
  578. #average layer level is 3
  579. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  580. t.Open()
  581. main_timer = 0
  582. LOGO_OP = openshot.FFmpegReader(dir_video+"LOGO_OP_4.mp4")
  583. LOGO_OP.Open() # Open the reader
  584. LOGO_OP_clip = video_photo_clip(vid=LOGO_OP,layer=4,position=0,end=LOGO_OP.info.duration
  585. ,location_y=-0.03,scale_x=0.8,scale_y=0.704)
  586. t.AddClip(LOGO_OP_clip)
  587. bg_head = openshot.FFmpegReader(dir_video+"complete_head_aispokesgirl.mp4")
  588. bg_head.Open()
  589. bg_head_clip = video_photo_clip(vid=bg_head,layer=2,position=0,end=LOGO_OP.info.duration,ck=ck)
  590. t.AddClip(bg_head_clip)
  591. main_timer += LOGO_OP.info.duration
  592. head_duration = LOGO_OP.info.duration
  593. bg_head.Close()
  594. LOGO_OP.Close()
  595. clip_duration=0
  596. photo_clip_list = [None]*len(text_content)
  597. img_list = [None]*len(text_content)
  598. anchor_clip_list = [None] * len(text_content)
  599. anchor_list = [None] * len(text_content)
  600. audio_clip_list = [None] * len(text_content)
  601. audio_list = [None] * len(text_content)
  602. sub_clip_list = [None] * len(text_content)
  603. sub_img_list = [None] * len(text_content)
  604. idx = 0
  605. for p in listdir(dir_photo+name_hash):
  606. anchor_list[idx] = openshot.FFmpegReader(dir_anchor+name_hash+"/"+str(idx)+".mp4")
  607. clip_duration = anchor_list[idx].info.duration
  608. anchor_list[idx].Open()
  609. anchor_clip_list[idx] = video_photo_clip(vid=anchor_list[idx],layer=4,scale_x=0.65,scale_y=0.65,
  610. location_x=0.35,location_y=0.25,position=main_timer, end=clip_duration,ck=ck_anchor,audio=False)
  611. t.AddClip(anchor_clip_list[idx])
  612. img_list[idx] = openshot.FFmpegReader(dir_photo+name_hash+'/'+p)
  613. img_list[idx].Open()
  614. photo_clip_list[idx] = video_photo_clip(vid=img_list[idx],layer=3
  615. ,scale_x=0.81,scale_y=0.68,location_y=-0.03,position=main_timer,end=clip_duration,audio=False)
  616. t.AddClip(photo_clip_list[idx])
  617. img_list[idx].Close()
  618. audio_list[idx] = openshot.FFmpegReader(dir_sound+name_hash+"/"+str(idx)+".mp3")
  619. audio_list[idx].Open()
  620. audio_clip_list[idx] = openshot.Clip(audio_list[idx])
  621. audio_clip_list[idx].Position(main_timer)
  622. audio_clip_list[idx].End(clip_duration)
  623. t.AddClip(audio_clip_list[idx])
  624. img_list[idx].Close()
  625. anchor_list[idx].Close()
  626. audio_list[idx].Close()
  627. sub_img_list[idx] = [None] * len(sub_list[idx])
  628. sub_clip_list[idx] = [None] * len(sub_list[idx])
  629. sub_timer = 0
  630. for sub_idx in range(len(sub_list[idx])):
  631. sub_img_list[idx][sub_idx] = openshot.QtImageReader(sub_list[idx][sub_idx]['path'])
  632. sub_img_list[idx][sub_idx].Open()
  633. sub_duration = 0.205*sub_list[idx][sub_idx]['count']
  634. sub_clip_list[idx][sub_idx] = video_photo_clip(vid=sub_img_list[idx][sub_idx], layer=6,location_x=0.069, location_y=0.89,position=main_timer+sub_timer,end=sub_duration)
  635. t.AddClip(sub_clip_list[idx][sub_idx])
  636. sub_img_list[idx][sub_idx].Close()
  637. sub_timer += sub_duration
  638. print(sub_list[idx][sub_idx]['path'])
  639. main_timer += clip_duration
  640. idx+=1
  641. LOGO_ED = openshot.FFmpegReader(dir_video+"LOGO_ED.avi")
  642. LOGO_ED.Open()
  643. LOGO_ED_clip = video_photo_clip(vid=LOGO_ED,layer=4,position=main_timer,end=LOGO_ED.info.duration+2
  644. ,location_x=0.005,location_y=-0.031
  645. ,scale_x=0.8,scale_y=0.6825)
  646. t.AddClip(LOGO_ED_clip)
  647. ED_duration = LOGO_ED.info.duration
  648. LOGO_ED.Close()
  649. bg = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  650. bg.Open()
  651. bg_times = math.floor(main_timer+ED_duration/bg.info.duration)
  652. left_time = (main_timer+ED_duration) % bg.info.duration
  653. bg_clip_list = [None] * bg_times
  654. bg_list = [None] * bg_times
  655. bg.Close()
  656. bg_timer = head_duration
  657. for idx in range(bg_times):
  658. bg_list[idx] = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  659. bg_list[idx].Open()
  660. bg_clip_list[idx] = video_photo_clip(bg_list[idx],layer=2,position=bg_timer
  661. ,end=bg_list[idx].info.duration,ck=ck)
  662. t.AddClip(bg_clip_list[idx])
  663. bg_timer += bg_list[idx].info.duration
  664. bg_list[idx].Close()
  665. bg_left = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  666. bg_left.Open()
  667. bg_left_clip = video_photo_clip(bg_left,layer=2,position=bg_timer,end=left_time,ck=ck)
  668. t.AddClip(bg_left_clip)
  669. bg_left.Close()
  670. title = openshot.QtImageReader(dir_title+name_hash+".png")
  671. title.Open() # Open the reader
  672. title_clip = video_photo_clip(vid=title, layer=4,location_x=-0.047, location_y=0.801,position=0,end=head_duration+main_timer)
  673. t.AddClip(title_clip)
  674. ####start building
  675. w = openshot.FFmpegWriter(tmp_video_dir+name_hash+".mp4")
  676. w.SetAudioOptions(True, "aac", 44100, 2, openshot.LAYOUT_STEREO, 3000000)
  677. w.SetVideoOptions(True, "libx264", openshot.Fraction(30000, 1000), 1280, 720,
  678. openshot.Fraction(1, 1), False, False, 3000000)
  679. w.Open()
  680. #may change duration into t.info.duration
  681. frames = int(t.info.fps)*int(head_duration+main_timer+ED_duration)
  682. for n in range(frames):
  683. f=t.GetFrame(n)
  684. w.WriteFrame(f)
  685. #notify_group(name+"的影片已經產生完成囉! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
  686. t.Close()
  687. w.Close()
  688. print("video at : www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
  689. def anchor_video_eng(name_hash,name,text_content, image_urls,sub_titles,avatar):
  690. file_prepare(name, name_hash, text_content,image_urls,'eng')
  691. sub_list=generate_subtitle_image_ENG(name_hash,sub_titles)
  692. for fname in range(len(text_content)):
  693. call_anchor(name_hash+"/"+str(fname),avatar)
  694. print('step finish')
  695. print('called............................................')
  696. ck=cKey(0,254,0,270)
  697. ck_anchor=cKey(0,255,1,320)
  698. duration = 0
  699. #average layer level is 3
  700. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  701. t.Open()
  702. main_timer = 0
  703. #add logo
  704. LOGO_OP = openshot.FFmpegReader(dir_video+"LOGO_OP_4.mp4")
  705. LOGO_OP.Open() # Open the reader
  706. LOGO_OP_clip = video_photo_clip(vid=LOGO_OP,layer=4,position=0,end=LOGO_OP.info.duration
  707. ,location_y=-0.03,scale_x=0.8,scale_y=0.704)
  708. t.AddClip(LOGO_OP_clip)
  709. #add background video (head is different)
  710. bg_head = openshot.FFmpegReader(dir_video+"complete_head_aispokesgirl.mp4")
  711. bg_head.Open()
  712. bg_head_clip = video_photo_clip(vid=bg_head,layer=2,position=0,end=LOGO_OP.info.duration,ck=ck)
  713. t.AddClip(bg_head_clip)
  714. main_timer += LOGO_OP.info.duration
  715. head_duration = LOGO_OP.info.duration
  716. bg_head.Close()
  717. LOGO_OP.Close()
  718. #prepare empty list
  719. clip_duration=0
  720. photo_clip_list = [None]*len(text_content)
  721. img_list = [None]*len(text_content)
  722. anchor_clip_list = [None] * len(text_content)
  723. anchor_list = [None] * len(text_content)
  724. audio_clip_list = [None] * len(text_content)
  725. audio_list = [None] * len(text_content)
  726. sub_clip_list = [None] * len(text_content)
  727. #openshot image holder
  728. sub_img_list = [None] * len(text_content)
  729. idx = 0
  730. for p in listdir(dir_photo+name_hash):
  731. anchor_list[idx] = openshot.FFmpegReader(dir_anchor+name_hash+"/"+str(idx)+".mp4")
  732. clip_duration = anchor_list[idx].info.duration
  733. anchor_list[idx].Open()
  734. anchor_clip_list[idx] = video_photo_clip(vid=anchor_list[idx],layer=4,scale_x=0.65,scale_y=0.65,
  735. location_x=0.35,location_y=0.25,position=main_timer, end=clip_duration,ck=ck_anchor,audio=False)
  736. t.AddClip(anchor_clip_list[idx])
  737. #insert image
  738. img_list[idx] = openshot.FFmpegReader(dir_photo+name_hash+'/'+p)
  739. img_list[idx].Open()
  740. photo_clip_list[idx] = video_photo_clip(vid=img_list[idx],layer=3
  741. ,scale_x=0.81,scale_y=0.68,location_y=-0.03,position=main_timer,end=clip_duration,audio=False)
  742. t.AddClip(photo_clip_list[idx])
  743. img_list[idx].Close()
  744. #insert audio (speech)
  745. audio_list[idx] = openshot.FFmpegReader(dir_sound+name_hash+"/"+str(idx)+".mp3")
  746. audio_list[idx].Open()
  747. audio_clip_list[idx] = openshot.Clip(audio_list[idx])
  748. audio_clip_list[idx].Position(main_timer)
  749. audio_clip_list[idx].End(clip_duration)
  750. t.AddClip(audio_clip_list[idx])
  751. #insert subtitle
  752. sub_img_list[idx] = openshot.QtImageReader(sub_list[idx])
  753. sub_img_list[idx].Open()
  754. sub_clip_list[idx] = video_photo_clip(vid=sub_img_list[idx], layer=6,location_x=0.069, location_y=0.89,position=main_timer,end=clip_duration)
  755. t.AddClip(sub_clip_list[idx])
  756. img_list[idx].Close()
  757. anchor_list[idx].Close()
  758. audio_list[idx].Close()
  759. sub_img_list[idx].Close()
  760. main_timer += clip_duration
  761. idx+=1
  762. LOGO_ED = openshot.FFmpegReader(dir_video+"ED_ENG.mp4")
  763. LOGO_ED.Open()
  764. LOGO_ED_clip = video_photo_clip(vid=LOGO_ED,layer=4,position=main_timer,end=LOGO_ED.info.duration+2
  765. ,location_x=0.005,location_y=-0.031
  766. ,scale_x=0.8,scale_y=0.6825)
  767. t.AddClip(LOGO_ED_clip)
  768. ED_duration = LOGO_ED.info.duration
  769. LOGO_ED.Close()
  770. bg = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  771. bg.Open()
  772. bg_times = math.floor(main_timer+ED_duration/bg.info.duration)
  773. left_time = (main_timer+ED_duration) % bg.info.duration
  774. bg_clip_list = [None] * bg_times
  775. bg_list = [None] * bg_times
  776. bg.Close()
  777. bg_timer = head_duration
  778. for idx in range(bg_times):
  779. bg_list[idx] = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  780. bg_list[idx].Open()
  781. bg_clip_list[idx] = video_photo_clip(bg_list[idx],layer=2,position=bg_timer
  782. ,end=bg_list[idx].info.duration,ck=ck)
  783. t.AddClip(bg_clip_list[idx])
  784. bg_timer += bg_list[idx].info.duration
  785. bg_list[idx].Close()
  786. bg_left = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  787. bg_left.Open()
  788. bg_left_clip = video_photo_clip(bg_left,layer=2,position=bg_timer,end=left_time,ck=ck)
  789. t.AddClip(bg_left_clip)
  790. bg_left.Close()
  791. title = openshot.QtImageReader(dir_title+name_hash+".png")
  792. title.Open() # Open the reader
  793. title_clip = video_photo_clip(vid=title, layer=4,location_x=-0.047, location_y=0.801,position=0,end=head_duration+main_timer)
  794. t.AddClip(title_clip)
  795. ####start building
  796. w = openshot.FFmpegWriter(tmp_video_dir+name_hash+".mp4")
  797. w.SetAudioOptions(True, "aac", 44100, 2, openshot.LAYOUT_STEREO, 3000000)
  798. w.SetVideoOptions(True, "libx264", openshot.Fraction(30000, 1000), 1280, 720,
  799. openshot.Fraction(1, 1), False, False, 3000000)
  800. w.Open()
  801. #may change duration into t.info.duration
  802. frames = int(t.info.fps)*int(head_duration+main_timer+ED_duration)
  803. for n in range(frames):
  804. f=t.GetFrame(n)
  805. w.WriteFrame(f)
  806. #notify_group(name+"(ENG)的影片已經產生完成囉! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
  807. t.Close()
  808. w.Close()
  809. print("video at : www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
  810. #line notifs
  811. import pyttsx3
  812. def make_speech(text):
  813. engine = pyttsx3.init()
  814. #voices = engine.getProperty('voices')
  815. engine.setProperty('voice', 'Mandarin')
  816. engine.save_to_file(text, '/app/speech.mp3')
  817. engine.runAndWait()
  818. class video_service(rpyc.Service):
  819. def exposed_call_video(self,name_hash,name,text_content, image_urls,multiLang,avatar):
  820. print('ML:'+str(multiLang))
  821. anchor_video_v2(name_hash,name,text_content, image_urls,multiLang,avatar)
  822. def exposed_call_video_eng(self,name_hash,name,text_content, image_urls,sub_titles,avatar):
  823. anchor_video_eng(name_hash,name,text_content, image_urls,sub_titles,avatar)
  824. def exposed_call_video_gen(self,name_hash,name,text_content, image_urls,multiLang,avatar):
  825. print('ML:'+str(multiLang))#this is long video version,
  826. video_gen(name_hash,name,text_content, image_urls,multiLang,avatar)
  827. def exposed_make_speech(self,text):
  828. make_speech(text)
  829. from rpyc.utils.server import ThreadedServer
  830. t = ThreadedServer(video_service, port=8858)
  831. print('service started')
  832. t.start()