openshot_video_generator.py 40 KB

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