openshot_video_generator.py 38 KB

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