openshot_video_generator.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. dir_sound = 'mp3_track/'
  31. dir_photo = 'photo/'
  32. dir_text = 'text_file/'
  33. dir_video = 'video_material/'
  34. dir_title = 'title/'
  35. dir_subtitle = 'subtitle/'
  36. dir_anchor = 'anchor_raw/'
  37. tmp_video_dir = 'tmp_video/'
  38. video_sub_folder = 'ai_anchor_video/'
  39. dir_list = [dir_sound,dir_photo,dir_text,dir_video,dir_title,dir_subtitle,dir_anchor,tmp_video_dir]
  40. def notify_group(msg):
  41. glist=['7vilzohcyQMPLfAMRloUawiTV4vtusZhxv8Czo7AJX8','WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD','1dbtJHbWVbrooXmQqc4r8OyRWDryjD4TMJ6DiDsdgsX','HOB1kVNgIb81tTB4Ort1BfhVp9GFo6NlToMQg88vEhh']
  42. for gid in glist:
  43. headers = {
  44. "Authorization": "Bearer " + gid,
  45. "Content-Type": "application/x-www-form-urlencoded"
  46. }
  47. params = {"message": msg}
  48. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
  49. def cKey(r,g,b,fuzz):
  50. col=openshot.Color()
  51. col.red=openshot.Keyframe(r)
  52. col.green=openshot.Keyframe(g)
  53. col.blue=openshot.Keyframe(b)
  54. return openshot.ChromaKey(col, openshot.Keyframe(fuzz))
  55. def video_photo_clip(vid=None,layer=None, position=None, end=None
  56. ,scale_x=1,scale_y=1,location_x=0,location_y=0,ck=None,audio=True):
  57. clip = openshot.Clip(vid)
  58. clip.Layer(layer)
  59. clip.Position(position)
  60. clip.End(end)
  61. clip.scale_x=openshot.Keyframe(scale_x)
  62. clip.scale_y=openshot.Keyframe(scale_y)
  63. clip.location_x=openshot.Keyframe(location_x)
  64. clip.location_y=openshot.Keyframe(location_y)
  65. if ck!=None:
  66. clip.AddEffect(ck)
  67. if audio==True:
  68. clip.has_audio=openshot.Keyframe(1)
  69. else:
  70. clip.has_audio=openshot.Keyframe(0)
  71. return clip
  72. def listener_progress(string, percent):
  73. True
  74. def myunichchar(unicode_char):
  75. mb_string = unicode_char.encode('big5')
  76. try:
  77. unicode_char = unichr(ord(mb_string[0]) << 8 | ord(mb_string[1]))
  78. except NameError:
  79. unicode_char = chr(mb_string[0] << 8 | mb_string[1])
  80. return unicode_char
  81. def get_url_type(url):
  82. req = urllib.request.Request(url, method='HEAD', headers={'User-Agent': 'Mozilla/5.0'})
  83. r = urllib.request.urlopen(req)
  84. contentType = r.getheader('Content-Type')
  85. return contentType
  86. def make_dir(name_hash):
  87. for direct in dir_list:
  88. if not os.path.isdir(direct):
  89. os.mkdir(direct)
  90. try:
  91. os.mkdir(dir_photo+name_hash)
  92. except FileExistsError:
  93. print("~~~~~~Warning~~~~~~~~~Directory " , dir_photo+name_hash , " already exists")
  94. try:
  95. os.mkdir(dir_text+name_hash)
  96. except FileExistsError:
  97. print("~~~~~~Warning~~~~~~~~~Directory " , dir_text+name_hash , " already exists")
  98. try:
  99. os.mkdir(dir_sound+name_hash)
  100. except FileExistsError:
  101. print("~~~~~~Warning~~~~~~~~~Directory " , dir_sound+name_hash , " already exists")
  102. try:
  103. os.mkdir(dir_anchor+name_hash)
  104. except FileExistsError:
  105. print("~~~~~~Warning~~~~~~~~~Directory " , dir_anchor+name_hash , " already exists")
  106. try:
  107. os.mkdir(dir_subtitle+name_hash)
  108. except FileExistsError:
  109. print("~~~~~~Warning~~~~~~~~~Directory " , dir_subtitle+name_hash , " already exists")
  110. def file_prepare(name, name_hash,text_content,image_urls,multiLang,lang='zh'):
  111. make_dir(name_hash)
  112. img_num = 1
  113. for imgu in image_urls:
  114. if get_url_type(imgu) =='video/mp4':
  115. r=requests.get(imgu)
  116. f=open(dir_photo+name_hash+"/"+str(img_num)+".mp4",'wb')
  117. for chunk in r.iter_content(chunk_size=255):
  118. if chunk:
  119. f.write(chunk)
  120. f.close()
  121. else:
  122. im = Image.open(requests.get(imgu, stream=True).raw)
  123. im= im.convert("RGB")
  124. im.save(dir_photo+name_hash+"/"+str(img_num)+".jpg")
  125. img_num+=1
  126. #save text
  127. txt_idx=0
  128. for txt in text_content:
  129. text_file = open(dir_text+name_hash+"/"+str(txt_idx)+".txt", "w")
  130. text_file.write(txt)
  131. text_file.close()
  132. txt_idx+=1
  133. print("text file made")
  134. #make mp3
  135. txt_idx = 0
  136. for txt in text_content:
  137. if lang!='zh' or multiLang==1:
  138. if lang!='zh':
  139. tts = gTTS(txt)
  140. tts.save(dir_sound+name_hash+"/"+str(txt_idx)+"raw.mp3")
  141. else:
  142. tts = gTTS(txt,lang='zh-tw')
  143. tts.save(dir_sound+name_hash+"/"+str(txt_idx)+"raw.mp3")
  144. #speed up
  145. ff = ffmpy.FFmpeg(inputs={dir_sound+name_hash+"/"+str(txt_idx)+"raw.mp3": None}
  146. , outputs={dir_sound+name_hash+"/"+str(txt_idx)+".mp3": ["-filter:a", "atempo=1.2"]})
  147. ff.run()
  148. os.remove(dir_sound+name_hash+"/"+str(txt_idx)+"raw.mp3")
  149. else:
  150. print('use zhtts')
  151. tts = zhtts.TTS()
  152. tts.text2wav(txt,dir_sound+name_hash+"/"+str(txt_idx)+".mp3")
  153. txt_idx+=1
  154. print("mp3 file made")
  155. #make title as image
  156. txt2image_title(name, dir_title+name_hash+".png",lang)
  157. def txt2image(content, save_target,lang='zh'):
  158. unicode_text = trim_punctuation(content)
  159. font = ''
  160. if lang=='zh':
  161. font = ImageFont.truetype(font="font/DFT_B7.ttc", size=38)
  162. else :
  163. font = ImageFont.truetype(font="font/arial.ttf", size=38)
  164. text_width, text_height = font.getsize(unicode_text)
  165. canvas = Image.new('RGBA', (700, 500), (255, 0, 0, 0) )
  166. draw = ImageDraw.Draw(canvas)
  167. text= unicode_text
  168. draw.text((5,5), text, (255, 255, 0), font)
  169. canvas.save(save_target, "PNG")
  170. def txt2image_title(content, save_target, lang='zh'):
  171. unicode_text = trim_punctuation(content)
  172. font = ''
  173. if lang=='zh':
  174. font = ImageFont.truetype(font="font/DFT_B7.ttc", size=22)
  175. else :
  176. font = ImageFont.truetype(font="font/arial.ttf", size=22)
  177. text_width, text_height = font.getsize(unicode_text)
  178. canvas = Image.new('RGBA', (510, 500), (255, 0, 0, 0) )
  179. draw = ImageDraw.Draw(canvas)
  180. text= unicode_text
  181. draw.text((5,5), text, (17, 41, 167), font)
  182. canvas.save(save_target, "PNG")
  183. def call_anchor(fileName,avatar):
  184. conn = rpyc.classic.connect("192.168.1.105",18812)
  185. ros = conn.modules.os
  186. rsys = conn.modules.sys
  187. fr=open(dir_sound+fileName+".mp3",'rb')# voice
  188. #warning!!! file my be replaced by other process
  189. fw=conn.builtins.open('/tmp/output.mp3','wb')
  190. while True:
  191. b=fr.read(1024)
  192. if b:
  193. fw.write(b)
  194. else:
  195. break
  196. fr.close()
  197. fw.close()
  198. val=random.randint(1000000,9999999)
  199. ros.chdir('/home/jared/to_video')
  200. ros.system('./p'+str(avatar)+'.sh '+str(val)+' &')
  201. while True:
  202. print('waiting...')
  203. if ros.path.exists('/tmp/results/'+str(val)):
  204. break
  205. time.sleep(5)
  206. print('waiting...')
  207. fr=conn.builtins.open('/tmp/results/'+str(val)+'.mp4','rb')
  208. fw=open(dir_anchor+fileName+".mp4",'wb')
  209. while True:
  210. b=fr.read(1024)
  211. if b:
  212. fw.write(b)
  213. else:
  214. break
  215. fr.close()
  216. fw.close()
  217. def parse_script(file_path):
  218. with open(file_path, 'r') as f:
  219. lines = [line.strip() for line in f]
  220. dict_list = []
  221. for idx in range(int((len(lines)+1)/4)):
  222. script={}
  223. script['index'] = idx
  224. time_raw = lines[idx * 4 + 1]
  225. script['content'] = lines[idx * 4 + 2]
  226. start = time_raw.split(' --> ')[0].split(':')
  227. stop = time_raw.split(' --> ')[1].split(':')
  228. start[2] = start[2].replace(',','.')
  229. stop[2] = stop[2].replace(',','.')
  230. start_sec = float(start[0])*3600 + float(start[1])*60 + float(start[2])
  231. stop_sec = float(stop[0])*3600 + float(stop[1])*60 + float(stop[2])
  232. duration = start_sec-stop_sec
  233. script['start'] = start_sec
  234. script['stop'] = stop_sec
  235. script['duration'] = abs(duration)
  236. dict_list.append(script)
  237. return dict_list
  238. def trim_punctuation(s):
  239. pat_block = u'[^\u4e00-\u9fff0-9a-zA-Z]+';
  240. pattern = u'([0-9]+{0}[0-9]+)|{0}'.format(pat_block)
  241. res = re.sub(pattern, lambda x: x.group(1) if x.group(1) else u" " ,s)
  242. return res
  243. def splitter(s):
  244. for sent in re.findall(u'[^!?,。\!\?]+[!?。\!\?]?', s, flags=re.U):
  245. yield sent
  246. def split_by_pun(s):
  247. res = list(splitter(s))
  248. return res
  249. def generate_subtitle_image_from_dict(name_hash, sub_dict):
  250. for script in sub_dict:
  251. sv_path = dir_subtitle + name_hash + '/' + str(script['index'])+'.png'
  252. sub = script['content']
  253. txt2image(sub,sv_path)
  254. def generate_subtitle_image(name_hash,text_content):
  255. img_list = [None]*len(text_content)
  256. for idx in range(len(text_content)):
  257. img_list[idx]=[]
  258. senList = split_by_pun(text_content[idx])
  259. for inner_idx in range(len(senList)):
  260. sv_path = dir_subtitle + name_hash +'/'+str(idx)+ str(inner_idx) +'.png'
  261. sub = senList[inner_idx]
  262. txt2image(sub,sv_path)
  263. img_list[idx]+=[{"count":len(sub),"path":sv_path}]
  264. return img_list
  265. def generate_subtitle_image_ENG(name_hash,text_content):
  266. img_list = [None]*len(text_content)
  267. for idx in range(len(text_content)):
  268. sv_path = dir_subtitle + name_hash +'/'+str(idx)+'.png'
  269. sub = text_content[idx]
  270. txt2image(sub, sv_path,lang='eng')
  271. img_list[idx] = sv_path
  272. return img_list
  273. def video_writer_init(path):
  274. w = openshot.FFmpegWriter(path)
  275. w.SetAudioOptions(True, "aac", 44100, 2, openshot.LAYOUT_STEREO, 3000000)
  276. w.SetVideoOptions(True, "libx264", openshot.Fraction(30000, 1000), 1280, 720,
  277. openshot.Fraction(1, 1), False, False, 3000000)
  278. return w
  279. def video_gen(name_hash,name,text_content, image_urls,multiLang,avatar):
  280. file_prepare(name, name_hash, text_content,image_urls,multiLang)
  281. for fname in range(len(text_content)):
  282. call_anchor(name_hash+"/"+str(fname),avatar)
  283. print('called............................................')
  284. ck=cKey(0,254,0,270)
  285. ck_anchor=cKey(0,255,1,320)
  286. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  287. t.Open()
  288. main_timer = 0
  289. LOGO_OP = openshot.FFmpegReader(dir_video+"LOGO_OP_4.mp4")
  290. LOGO_OP.Open() # Open the reader
  291. head_duration = LOGO_OP.info.duration
  292. LOGO_OP_clip = video_photo_clip(vid=LOGO_OP,layer=4,position=0,end=head_duration
  293. ,location_y=-0.03,scale_x=0.8,scale_y=0.704)
  294. t.AddClip(LOGO_OP_clip)
  295. bg_head = openshot.FFmpegReader(dir_video+"complete_head_aispokesgirl.mp4")
  296. bg_head.Open()
  297. bg_head_clip = video_photo_clip(vid=bg_head,layer=2,position=0,end=LOGO_OP.info.duration,ck=ck)
  298. t.AddClip(bg_head_clip)
  299. main_timer += head_duration
  300. bg_head.Close()
  301. LOGO_OP.Close()
  302. anchor = openshot.FFmpegReader(dir_anchor+name_hash+"/0.mp4")
  303. anchor.Open()
  304. #anchor_clip = video_photo_clip(vid=anchor,layer=4,scale_x=0.65,scale_y=0.65,
  305. # location_x=0.35,location_y=0.25,position=main_timer, end=anchor.info.duration,ck=ck_anchor,audio=False)
  306. #t.AddClip(anchor_clip)
  307. speech = openshot.FFmpegReader(dir_sound+name_hash+"/0.mp3")
  308. speech.Open()
  309. speech_clip = openshot.Clip(speech)
  310. speech_clip.Position(main_timer)
  311. speech_clip.End(anchor.info.duration)
  312. t.AddClip(speech_clip)
  313. main_timer += anchor.info.duration
  314. anchor.Close()
  315. speech.Close()
  316. LOGO_ED = openshot.FFmpegReader(dir_video+"LOGO_ED.avi")
  317. LOGO_ED.Open()
  318. LOGO_ED_clip = video_photo_clip(vid=LOGO_ED,layer=4,position=main_timer,end=LOGO_ED.info.duration
  319. ,location_x=0.005,location_y=-0.031, scale_x=0.8,scale_y=0.6825)
  320. t.AddClip(LOGO_ED_clip)
  321. main_timer += LOGO_ED.info.duration
  322. LOGO_ED.Close()
  323. bg = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  324. bg.Open()
  325. bg_times = math.floor(main_timer/bg.info.duration)
  326. left_time = (main_timer) % bg.info.duration
  327. bg_clip_list = [None] * bg_times
  328. bg_list = [None] * bg_times
  329. bg.Close()
  330. bg_timer = head_duration
  331. for idx in range(bg_times):
  332. bg_list[idx] = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  333. bg_list[idx].Open()
  334. bg_clip_list[idx] = video_photo_clip(bg_list[idx],layer=2,position=bg_timer,end=bg_list[idx].info.duration,ck=ck)
  335. t.AddClip(bg_clip_list[idx])
  336. bg_timer += bg_list[idx].info.duration
  337. bg_list[idx].Close()
  338. bg_left = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  339. bg_left.Open()
  340. bg_left_clip = video_photo_clip(bg_left,layer=2,position=bg_timer,end=left_time,ck=ck)
  341. t.AddClip(bg_left_clip)
  342. bg_left.Close()
  343. title = openshot.QtImageReader(dir_title+name_hash+".png")
  344. title.Open() # Open the reader
  345. title_clip = video_photo_clip(vid=title, layer=4,location_x=-0.047, location_y=0.801,position=0,end=head_duration+main_timer)
  346. t.AddClip(title_clip)
  347. w = video_writer_init(tmp_video_dir+name_hash+"raw.mp4")
  348. w.Open()
  349. frames = int(t.info.fps)*int(main_timer)
  350. for n in range(frames):
  351. f=t.GetFrame(n)
  352. w.WriteFrame(f)
  353. t.Close()
  354. w.Close()
  355. print(name+"RAW DONE : www.choozmo.com:8168/"+video_sub_folder+name_hash+"raw.mp4")
  356. #start adding sub
  357. #add sub
  358. Ctr_Autosub.init()
  359. 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)
  360. sub_dict = parse_script(tmp_video_dir+name_hash+"script.txt")
  361. generate_subtitle_image_from_dict(name_hash, sub_dict)
  362. #sv_path = dir_subtitle + name_hash + '/' + str(script['index'])+'.png'
  363. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  364. t.Open()
  365. raw = openshot.FFmpegReader(tmp_video_dir+name_hash+"raw.mp4")
  366. raw.Open()
  367. raw_clip = video_photo_clip(vid=raw,layer=2,position=0, end=raw.info.duration)
  368. t.AddClip(raw_clip)
  369. sub_img_list = [None] * len(sub_dict)
  370. sub_clip_list = [None] * len(sub_dict)
  371. for sub_obj in sub_dict:
  372. idx = int(sub_obj['index'])
  373. sub_img_list[idx] = openshot.QtImageReader(dir_subtitle + name_hash + '/' + str(idx)+'.png')
  374. sub_img_list[idx].Open()
  375. 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']))
  376. t.AddClip(sub_clip_list[idx])
  377. sub_img_list[idx].Close()
  378. os.remove(tmp_video_dir+name_hash+"raw.mp4")
  379. os.remove(tmp_video_dir+name_hash+"script.txt")
  380. anchor = openshot.FFmpegReader(dir_anchor+name_hash+"/0.mp4")
  381. anchor.Open()
  382. anchor_clip = video_photo_clip(vid=anchor,layer=4,scale_x=0.65,scale_y=0.65,
  383. location_x=0.35,location_y=0.25,position=head_duration, end=anchor.info.duration,ck=ck_anchor,audio=False)
  384. t.AddClip(anchor_clip)
  385. w = video_writer_init(tmp_video_dir+name_hash+".mp4")
  386. w.Open()
  387. frames = int(t.info.fps)*int(main_timer)
  388. for n in range(frames):
  389. f=t.GetFrame(n)
  390. w.WriteFrame(f)
  391. t.Close()
  392. w.Close()
  393. print(name+"ALL DONE : www.choozmo.com:8168/"+video_sub_folder+name_hash+"raw.mp4")
  394. def anchor_video_v2(name_hash,name,text_content, image_urls,multiLang,avatar):
  395. print(os.getcwd())
  396. print('sub image made')
  397. print(multiLang)
  398. file_prepare(name, name_hash, text_content,image_urls,multiLang)
  399. sub_list=generate_subtitle_image(name_hash,text_content)
  400. for fname in range(len(text_content)):
  401. call_anchor(name_hash+"/"+str(fname),avatar)
  402. print('step finish')
  403. print('called............................................')
  404. ck=cKey(0,254,0,270)
  405. ck_anchor=cKey(0,255,1,320)
  406. duration = 0
  407. #average layer level is 3
  408. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  409. t.Open()
  410. main_timer = 0
  411. LOGO_OP = openshot.FFmpegReader(dir_video+"LOGO_OP_4.mp4")
  412. LOGO_OP.Open() # Open the reader
  413. LOGO_OP_clip = video_photo_clip(vid=LOGO_OP,layer=4,position=0,end=LOGO_OP.info.duration
  414. ,location_y=-0.03,scale_x=0.8,scale_y=0.704)
  415. t.AddClip(LOGO_OP_clip)
  416. bg_head = openshot.FFmpegReader(dir_video+"complete_head_aispokesgirl.mp4")
  417. bg_head.Open()
  418. bg_head_clip = video_photo_clip(vid=bg_head,layer=2,position=0,end=LOGO_OP.info.duration,ck=ck)
  419. t.AddClip(bg_head_clip)
  420. main_timer += LOGO_OP.info.duration
  421. head_duration = LOGO_OP.info.duration
  422. bg_head.Close()
  423. LOGO_OP.Close()
  424. clip_duration=0
  425. photo_clip_list = [None]*len(text_content)
  426. img_list = [None]*len(text_content)
  427. anchor_clip_list = [None] * len(text_content)
  428. anchor_list = [None] * len(text_content)
  429. audio_clip_list = [None] * len(text_content)
  430. audio_list = [None] * len(text_content)
  431. sub_clip_list = [None] * len(text_content)
  432. sub_img_list = [None] * len(text_content)
  433. idx = 0
  434. for p in listdir(dir_photo+name_hash):
  435. anchor_list[idx] = openshot.FFmpegReader(dir_anchor+name_hash+"/"+str(idx)+".mp4")
  436. clip_duration = anchor_list[idx].info.duration
  437. anchor_list[idx].Open()
  438. anchor_clip_list[idx] = video_photo_clip(vid=anchor_list[idx],layer=4,scale_x=0.65,scale_y=0.65,
  439. location_x=0.35,location_y=0.25,position=main_timer, end=clip_duration,ck=ck_anchor,audio=False)
  440. t.AddClip(anchor_clip_list[idx])
  441. img_list[idx] = openshot.FFmpegReader(dir_photo+name_hash+'/'+p)
  442. img_list[idx].Open()
  443. photo_clip_list[idx] = video_photo_clip(vid=img_list[idx],layer=3
  444. ,scale_x=0.81,scale_y=0.68,location_y=-0.03,position=main_timer,end=clip_duration,audio=False)
  445. t.AddClip(photo_clip_list[idx])
  446. img_list[idx].Close()
  447. audio_list[idx] = openshot.FFmpegReader(dir_sound+name_hash+"/"+str(idx)+".mp3")
  448. audio_list[idx].Open()
  449. audio_clip_list[idx] = openshot.Clip(audio_list[idx])
  450. audio_clip_list[idx].Position(main_timer)
  451. audio_clip_list[idx].End(clip_duration)
  452. t.AddClip(audio_clip_list[idx])
  453. img_list[idx].Close()
  454. anchor_list[idx].Close()
  455. audio_list[idx].Close()
  456. sub_img_list[idx] = [None] * len(sub_list[idx])
  457. sub_clip_list[idx] = [None] * len(sub_list[idx])
  458. sub_timer = 0
  459. for sub_idx in range(len(sub_list[idx])):
  460. sub_img_list[idx][sub_idx] = openshot.QtImageReader(sub_list[idx][sub_idx]['path'])
  461. sub_img_list[idx][sub_idx].Open()
  462. sub_duration = 0.205*sub_list[idx][sub_idx]['count']
  463. 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)
  464. t.AddClip(sub_clip_list[idx][sub_idx])
  465. sub_img_list[idx][sub_idx].Close()
  466. sub_timer += sub_duration
  467. print(sub_list[idx][sub_idx]['path'])
  468. main_timer += clip_duration
  469. idx+=1
  470. LOGO_ED = openshot.FFmpegReader(dir_video+"LOGO_ED.avi")
  471. LOGO_ED.Open()
  472. LOGO_ED_clip = video_photo_clip(vid=LOGO_ED,layer=4,position=main_timer,end=LOGO_ED.info.duration+2
  473. ,location_x=0.005,location_y=-0.031
  474. ,scale_x=0.8,scale_y=0.6825)
  475. t.AddClip(LOGO_ED_clip)
  476. ED_duration = LOGO_ED.info.duration
  477. LOGO_ED.Close()
  478. bg = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  479. bg.Open()
  480. bg_times = math.floor(main_timer+ED_duration/bg.info.duration)
  481. left_time = (main_timer+ED_duration) % bg.info.duration
  482. bg_clip_list = [None] * bg_times
  483. bg_list = [None] * bg_times
  484. bg.Close()
  485. bg_timer = head_duration
  486. for idx in range(bg_times):
  487. bg_list[idx] = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  488. bg_list[idx].Open()
  489. bg_clip_list[idx] = video_photo_clip(bg_list[idx],layer=2,position=bg_timer
  490. ,end=bg_list[idx].info.duration,ck=ck)
  491. t.AddClip(bg_clip_list[idx])
  492. bg_timer += bg_list[idx].info.duration
  493. bg_list[idx].Close()
  494. bg_left = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  495. bg_left.Open()
  496. bg_left_clip = video_photo_clip(bg_left,layer=2,position=bg_timer,end=left_time,ck=ck)
  497. t.AddClip(bg_left_clip)
  498. bg_left.Close()
  499. title = openshot.QtImageReader(dir_title+name_hash+".png")
  500. title.Open() # Open the reader
  501. title_clip = video_photo_clip(vid=title, layer=4,location_x=-0.047, location_y=0.801,position=0,end=head_duration+main_timer)
  502. t.AddClip(title_clip)
  503. ####start building
  504. w = openshot.FFmpegWriter(tmp_video_dir+name_hash+".mp4")
  505. w.SetAudioOptions(True, "aac", 44100, 2, openshot.LAYOUT_STEREO, 3000000)
  506. w.SetVideoOptions(True, "libx264", openshot.Fraction(30000, 1000), 1280, 720,
  507. openshot.Fraction(1, 1), False, False, 3000000)
  508. w.Open()
  509. #may change duration into t.info.duration
  510. frames = int(t.info.fps)*int(head_duration+main_timer+ED_duration)
  511. for n in range(frames):
  512. f=t.GetFrame(n)
  513. w.WriteFrame(f)
  514. notify_group(name+"的影片已經產生完成囉! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
  515. t.Close()
  516. w.Close()
  517. print("video at : www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
  518. def anchor_video_eng(name_hash,name,text_content, image_urls,sub_titles,avatar):
  519. file_prepare(name, name_hash, text_content,image_urls,'eng')
  520. sub_list=generate_subtitle_image_ENG(name_hash,sub_titles)
  521. for fname in range(len(text_content)):
  522. call_anchor(name_hash+"/"+str(fname),avatar)
  523. print('step finish')
  524. print('called............................................')
  525. ck=cKey(0,254,0,270)
  526. ck_anchor=cKey(0,255,1,320)
  527. duration = 0
  528. #average layer level is 3
  529. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  530. t.Open()
  531. main_timer = 0
  532. #add logo
  533. LOGO_OP = openshot.FFmpegReader(dir_video+"LOGO_OP_4.mp4")
  534. LOGO_OP.Open() # Open the reader
  535. LOGO_OP_clip = video_photo_clip(vid=LOGO_OP,layer=4,position=0,end=LOGO_OP.info.duration
  536. ,location_y=-0.03,scale_x=0.8,scale_y=0.704)
  537. t.AddClip(LOGO_OP_clip)
  538. #add background video (head is different)
  539. bg_head = openshot.FFmpegReader(dir_video+"complete_head_aispokesgirl.mp4")
  540. bg_head.Open()
  541. bg_head_clip = video_photo_clip(vid=bg_head,layer=2,position=0,end=LOGO_OP.info.duration,ck=ck)
  542. t.AddClip(bg_head_clip)
  543. main_timer += LOGO_OP.info.duration
  544. head_duration = LOGO_OP.info.duration
  545. bg_head.Close()
  546. LOGO_OP.Close()
  547. #prepare empty list
  548. clip_duration=0
  549. photo_clip_list = [None]*len(text_content)
  550. img_list = [None]*len(text_content)
  551. anchor_clip_list = [None] * len(text_content)
  552. anchor_list = [None] * len(text_content)
  553. audio_clip_list = [None] * len(text_content)
  554. audio_list = [None] * len(text_content)
  555. sub_clip_list = [None] * len(text_content)
  556. #openshot image holder
  557. sub_img_list = [None] * len(text_content)
  558. idx = 0
  559. for p in listdir(dir_photo+name_hash):
  560. anchor_list[idx] = openshot.FFmpegReader(dir_anchor+name_hash+"/"+str(idx)+".mp4")
  561. clip_duration = anchor_list[idx].info.duration
  562. anchor_list[idx].Open()
  563. anchor_clip_list[idx] = video_photo_clip(vid=anchor_list[idx],layer=4,scale_x=0.65,scale_y=0.65,
  564. location_x=0.35,location_y=0.25,position=main_timer, end=clip_duration,ck=ck_anchor,audio=False)
  565. t.AddClip(anchor_clip_list[idx])
  566. #insert image
  567. img_list[idx] = openshot.FFmpegReader(dir_photo+name_hash+'/'+p)
  568. img_list[idx].Open()
  569. photo_clip_list[idx] = video_photo_clip(vid=img_list[idx],layer=3
  570. ,scale_x=0.81,scale_y=0.68,location_y=-0.03,position=main_timer,end=clip_duration,audio=False)
  571. t.AddClip(photo_clip_list[idx])
  572. img_list[idx].Close()
  573. #insert audio (speech)
  574. audio_list[idx] = openshot.FFmpegReader(dir_sound+name_hash+"/"+str(idx)+".mp3")
  575. audio_list[idx].Open()
  576. audio_clip_list[idx] = openshot.Clip(audio_list[idx])
  577. audio_clip_list[idx].Position(main_timer)
  578. audio_clip_list[idx].End(clip_duration)
  579. t.AddClip(audio_clip_list[idx])
  580. #insert subtitle
  581. sub_img_list[idx] = openshot.QtImageReader(sub_list[idx])
  582. sub_img_list[idx].Open()
  583. 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)
  584. t.AddClip(sub_clip_list[idx])
  585. img_list[idx].Close()
  586. anchor_list[idx].Close()
  587. audio_list[idx].Close()
  588. sub_img_list[idx].Close()
  589. main_timer += clip_duration
  590. idx+=1
  591. LOGO_ED = openshot.FFmpegReader(dir_video+"ED_ENG.mp4")
  592. LOGO_ED.Open()
  593. LOGO_ED_clip = video_photo_clip(vid=LOGO_ED,layer=4,position=main_timer,end=LOGO_ED.info.duration+2
  594. ,location_x=0.005,location_y=-0.031
  595. ,scale_x=0.8,scale_y=0.6825)
  596. t.AddClip(LOGO_ED_clip)
  597. ED_duration = LOGO_ED.info.duration
  598. LOGO_ED.Close()
  599. bg = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  600. bg.Open()
  601. bg_times = math.floor(main_timer+ED_duration/bg.info.duration)
  602. left_time = (main_timer+ED_duration) % bg.info.duration
  603. bg_clip_list = [None] * bg_times
  604. bg_list = [None] * bg_times
  605. bg.Close()
  606. bg_timer = head_duration
  607. for idx in range(bg_times):
  608. bg_list[idx] = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  609. bg_list[idx].Open()
  610. bg_clip_list[idx] = video_photo_clip(bg_list[idx],layer=2,position=bg_timer
  611. ,end=bg_list[idx].info.duration,ck=ck)
  612. t.AddClip(bg_clip_list[idx])
  613. bg_timer += bg_list[idx].info.duration
  614. bg_list[idx].Close()
  615. bg_left = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  616. bg_left.Open()
  617. bg_left_clip = video_photo_clip(bg_left,layer=2,position=bg_timer,end=left_time,ck=ck)
  618. t.AddClip(bg_left_clip)
  619. bg_left.Close()
  620. title = openshot.QtImageReader(dir_title+name_hash+".png")
  621. title.Open() # Open the reader
  622. title_clip = video_photo_clip(vid=title, layer=4,location_x=-0.047, location_y=0.801,position=0,end=head_duration+main_timer)
  623. t.AddClip(title_clip)
  624. ####start building
  625. w = openshot.FFmpegWriter(tmp_video_dir+name_hash+".mp4")
  626. w.SetAudioOptions(True, "aac", 44100, 2, openshot.LAYOUT_STEREO, 3000000)
  627. w.SetVideoOptions(True, "libx264", openshot.Fraction(30000, 1000), 1280, 720,
  628. openshot.Fraction(1, 1), False, False, 3000000)
  629. w.Open()
  630. #may change duration into t.info.duration
  631. frames = int(t.info.fps)*int(head_duration+main_timer+ED_duration)
  632. for n in range(frames):
  633. f=t.GetFrame(n)
  634. w.WriteFrame(f)
  635. notify_group(name+"(ENG)的影片已經產生完成囉! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
  636. t.Close()
  637. w.Close()
  638. print("video at : www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
  639. #line notifs
  640. class video_service(rpyc.Service):
  641. def exposed_call_video(self,name_hash,name,text_content, image_urls,multiLang,avatar):
  642. print('ML:'+str(multiLang))
  643. anchor_video_v2(name_hash,name,text_content, image_urls,multiLang,avatar)
  644. def exposed_call_video_eng(self,name_hash,name,text_content, image_urls,sub_titles,avatar):
  645. anchor_video_eng(name_hash,name,text_content, image_urls,sub_titles,avatar)
  646. def exposed_call_video_gen(self,name_hash,name,text_content, image_urls,multiLang,avatar):
  647. print('ML:'+str(multiLang))#this is long video version,
  648. video_gen(name_hash,name,text_content, image_urls,multiLang,avatar)
  649. from rpyc.utils.server import ThreadedServer
  650. t = ThreadedServer(video_service, port=8858)
  651. print('service started')
  652. t.start()