openshot_video_generator.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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'] = lines[idx * 4]
  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").Open()
  339. bg_left_clip = video_photo_clip(bg_left,layer=2,position=bg_timer,end=left_time,ck=ck)
  340. t.AddClip(bg_left_clip)
  341. bg_left.Close()
  342. title = openshot.QtImageReader(dir_title+name_hash+".png")
  343. title.Open() # Open the reader
  344. title_clip = video_photo_clip(vid=title, layer=4,location_x=-0.047, location_y=0.801,position=0,end=head_duration+main_timer)
  345. t.AddClip(title_clip)
  346. w = video_writer_init(tmp_video_dir+name_hash+"raw.mp4")
  347. w.Open()
  348. frames = int(t.info.fps)*int(main_timer)
  349. for n in range(frames):
  350. f=t.GetFrame(n)
  351. w.WriteFrame(f)
  352. t.Close()
  353. w.Close()
  354. print(name+"RAW DONE : www.choozmo.com:8168/"+video_sub_folder+name_hash+"raw.mp4")
  355. #start adding sub
  356. #add sub
  357. Ctr_Autosub.init()
  358. 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)
  359. sub_dict = parse_script(tmp_video_dir+name_hash+"script.txt")
  360. generate_subtitle_image_from_dict(sub_dict)
  361. #sv_path = dir_subtitle + name_hash + '/' + str(script['index'])+'.png'
  362. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  363. t.Open()
  364. raw = openshot.FFmpegReader(tmp_video_dir+name_hash+"raw.mp4")
  365. raw.Open()
  366. raw_clip = video_photo_clip(vid=raw,layer=2,position=0, end=raw.info.duration)
  367. t.AddClip(raw_clip)
  368. sub_img_list[idx] = [None] * len(sub_dict)
  369. sub_clip_list[idx] = [None] * len(sub_dict)
  370. for sub_obj in sub_dict:
  371. sub_img_list[sub_obj['index']] = openshot.QtImageReader(dir_subtitle + name_hash + '/' + str(sub_obj['index'])+'.png')
  372. sub_img_list[sub_obj['index']].Open()
  373. sub_clip_list[sub_obj['index']] = video_photo_clip(vid=sub_img_list[sub_obj['index']], layer=6,location_x=0.069, location_y=0.89,position=sub_obj['start'],end=math.ceil(sub_obj['duration']))
  374. t.AddClip(sub_clip_list[idx][sub_idx])
  375. sub_img_list[sub_obj['index']].Close()
  376. os.remove(tmp_video_dir+name_hash+"raw.mp4")
  377. os.remove(tmp_video_dir+name_hash+"script.txt")
  378. anchor = openshot.FFmpegReader(dir_anchor+name_hash+"/0.mp4")
  379. anchor.Open()
  380. anchor_clip = video_photo_clip(vid=anchor,layer=4,scale_x=0.65,scale_y=0.65,
  381. location_x=0.35,location_y=0.25,position=head_duration, end=anchor.info.duration,ck=ck_anchor,audio=False)
  382. t.AddClip(anchor_clip)
  383. w = video_writer_init(tmp_video_dir+name_hash+".mp4")
  384. w.Open()
  385. frames = int(t.info.fps)*int(main_timer)
  386. for n in range(frames):
  387. f=t.GetFrame(n)
  388. w.WriteFrame(f)
  389. t.Close()
  390. w.Close()
  391. print(name+"ALL DONE : www.choozmo.com:8168/"+video_sub_folder+name_hash+"raw.mp4")
  392. def anchor_video_v2(name_hash,name,text_content, image_urls,multiLang,avatar):
  393. print(os.getcwd())
  394. print('sub image made')
  395. print(multiLang)
  396. file_prepare(name, name_hash, text_content,image_urls,multiLang)
  397. sub_list=generate_subtitle_image(name_hash,text_content)
  398. for fname in range(len(text_content)):
  399. call_anchor(name_hash+"/"+str(fname),avatar)
  400. print('step finish')
  401. print('called............................................')
  402. ck=cKey(0,254,0,270)
  403. ck_anchor=cKey(0,255,1,320)
  404. duration = 0
  405. #average layer level is 3
  406. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  407. t.Open()
  408. main_timer = 0
  409. LOGO_OP = openshot.FFmpegReader(dir_video+"LOGO_OP_4.mp4")
  410. LOGO_OP.Open() # Open the reader
  411. LOGO_OP_clip = video_photo_clip(vid=LOGO_OP,layer=4,position=0,end=LOGO_OP.info.duration
  412. ,location_y=-0.03,scale_x=0.8,scale_y=0.704)
  413. t.AddClip(LOGO_OP_clip)
  414. bg_head = openshot.FFmpegReader(dir_video+"complete_head_aispokesgirl.mp4")
  415. bg_head.Open()
  416. bg_head_clip = video_photo_clip(vid=bg_head,layer=2,position=0,end=LOGO_OP.info.duration,ck=ck)
  417. t.AddClip(bg_head_clip)
  418. main_timer += LOGO_OP.info.duration
  419. head_duration = LOGO_OP.info.duration
  420. bg_head.Close()
  421. LOGO_OP.Close()
  422. clip_duration=0
  423. photo_clip_list = [None]*len(text_content)
  424. img_list = [None]*len(text_content)
  425. anchor_clip_list = [None] * len(text_content)
  426. anchor_list = [None] * len(text_content)
  427. audio_clip_list = [None] * len(text_content)
  428. audio_list = [None] * len(text_content)
  429. sub_clip_list = [None] * len(text_content)
  430. sub_img_list = [None] * len(text_content)
  431. idx = 0
  432. for p in listdir(dir_photo+name_hash):
  433. anchor_list[idx] = openshot.FFmpegReader(dir_anchor+name_hash+"/"+str(idx)+".mp4")
  434. clip_duration = anchor_list[idx].info.duration
  435. anchor_list[idx].Open()
  436. anchor_clip_list[idx] = video_photo_clip(vid=anchor_list[idx],layer=4,scale_x=0.65,scale_y=0.65,
  437. location_x=0.35,location_y=0.25,position=main_timer, end=clip_duration,ck=ck_anchor,audio=False)
  438. t.AddClip(anchor_clip_list[idx])
  439. img_list[idx] = openshot.FFmpegReader(dir_photo+name_hash+'/'+p)
  440. img_list[idx].Open()
  441. photo_clip_list[idx] = video_photo_clip(vid=img_list[idx],layer=3
  442. ,scale_x=0.81,scale_y=0.68,location_y=-0.03,position=main_timer,end=clip_duration,audio=False)
  443. t.AddClip(photo_clip_list[idx])
  444. img_list[idx].Close()
  445. audio_list[idx] = openshot.FFmpegReader(dir_sound+name_hash+"/"+str(idx)+".mp3")
  446. audio_list[idx].Open()
  447. audio_clip_list[idx] = openshot.Clip(audio_list[idx])
  448. audio_clip_list[idx].Position(main_timer)
  449. audio_clip_list[idx].End(clip_duration)
  450. t.AddClip(audio_clip_list[idx])
  451. img_list[idx].Close()
  452. anchor_list[idx].Close()
  453. audio_list[idx].Close()
  454. sub_img_list[idx] = [None] * len(sub_list[idx])
  455. sub_clip_list[idx] = [None] * len(sub_list[idx])
  456. sub_timer = 0
  457. for sub_idx in range(len(sub_list[idx])):
  458. sub_img_list[idx][sub_idx] = openshot.QtImageReader(sub_list[idx][sub_idx]['path'])
  459. sub_img_list[idx][sub_idx].Open()
  460. sub_duration = 0.205*sub_list[idx][sub_idx]['count']
  461. 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)
  462. t.AddClip(sub_clip_list[idx][sub_idx])
  463. sub_img_list[idx][sub_idx].Close()
  464. sub_timer += sub_duration
  465. print(sub_list[idx][sub_idx]['path'])
  466. main_timer += clip_duration
  467. idx+=1
  468. LOGO_ED = openshot.FFmpegReader(dir_video+"LOGO_ED.avi")
  469. LOGO_ED.Open()
  470. LOGO_ED_clip = video_photo_clip(vid=LOGO_ED,layer=4,position=main_timer,end=LOGO_ED.info.duration+2
  471. ,location_x=0.005,location_y=-0.031
  472. ,scale_x=0.8,scale_y=0.6825)
  473. t.AddClip(LOGO_ED_clip)
  474. ED_duration = LOGO_ED.info.duration
  475. LOGO_ED.Close()
  476. bg = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  477. bg.Open()
  478. bg_times = math.floor(main_timer+ED_duration/bg.info.duration)
  479. left_time = (main_timer+ED_duration) % bg.info.duration
  480. bg_clip_list = [None] * bg_times
  481. bg_list = [None] * bg_times
  482. bg.Close()
  483. bg_timer = head_duration
  484. for idx in range(bg_times):
  485. bg_list[idx] = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  486. bg_list[idx].Open()
  487. bg_clip_list[idx] = video_photo_clip(bg_list[idx],layer=2,position=bg_timer
  488. ,end=bg_list[idx].info.duration,ck=ck)
  489. t.AddClip(bg_clip_list[idx])
  490. bg_timer += bg_list[idx].info.duration
  491. bg_list[idx].Close()
  492. bg_left = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  493. bg_left.Open()
  494. bg_left_clip = video_photo_clip(bg_left,layer=2,position=bg_timer,end=left_time,ck=ck)
  495. t.AddClip(bg_left_clip)
  496. bg_left.Close()
  497. title = openshot.QtImageReader(dir_title+name_hash+".png")
  498. title.Open() # Open the reader
  499. title_clip = video_photo_clip(vid=title, layer=4,location_x=-0.047, location_y=0.801,position=0,end=head_duration+main_timer)
  500. t.AddClip(title_clip)
  501. ####start building
  502. w = openshot.FFmpegWriter(tmp_video_dir+name_hash+".mp4")
  503. w.SetAudioOptions(True, "aac", 44100, 2, openshot.LAYOUT_STEREO, 3000000)
  504. w.SetVideoOptions(True, "libx264", openshot.Fraction(30000, 1000), 1280, 720,
  505. openshot.Fraction(1, 1), False, False, 3000000)
  506. w.Open()
  507. #may change duration into t.info.duration
  508. frames = int(t.info.fps)*int(head_duration+main_timer+ED_duration)
  509. for n in range(frames):
  510. f=t.GetFrame(n)
  511. w.WriteFrame(f)
  512. notify_group(name+"的影片已經產生完成囉! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
  513. t.Close()
  514. w.Close()
  515. print("video at : www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
  516. def anchor_video_eng(name_hash,name,text_content, image_urls,sub_titles,avatar):
  517. file_prepare(name, name_hash, text_content,image_urls,'eng')
  518. sub_list=generate_subtitle_image_ENG(name_hash,sub_titles)
  519. for fname in range(len(text_content)):
  520. call_anchor(name_hash+"/"+str(fname),avatar)
  521. print('step finish')
  522. print('called............................................')
  523. ck=cKey(0,254,0,270)
  524. ck_anchor=cKey(0,255,1,320)
  525. duration = 0
  526. #average layer level is 3
  527. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  528. t.Open()
  529. main_timer = 0
  530. #add logo
  531. LOGO_OP = openshot.FFmpegReader(dir_video+"LOGO_OP_4.mp4")
  532. LOGO_OP.Open() # Open the reader
  533. LOGO_OP_clip = video_photo_clip(vid=LOGO_OP,layer=4,position=0,end=LOGO_OP.info.duration
  534. ,location_y=-0.03,scale_x=0.8,scale_y=0.704)
  535. t.AddClip(LOGO_OP_clip)
  536. #add background video (head is different)
  537. bg_head = openshot.FFmpegReader(dir_video+"complete_head_aispokesgirl.mp4")
  538. bg_head.Open()
  539. bg_head_clip = video_photo_clip(vid=bg_head,layer=2,position=0,end=LOGO_OP.info.duration,ck=ck)
  540. t.AddClip(bg_head_clip)
  541. main_timer += LOGO_OP.info.duration
  542. head_duration = LOGO_OP.info.duration
  543. bg_head.Close()
  544. LOGO_OP.Close()
  545. #prepare empty list
  546. clip_duration=0
  547. photo_clip_list = [None]*len(text_content)
  548. img_list = [None]*len(text_content)
  549. anchor_clip_list = [None] * len(text_content)
  550. anchor_list = [None] * len(text_content)
  551. audio_clip_list = [None] * len(text_content)
  552. audio_list = [None] * len(text_content)
  553. sub_clip_list = [None] * len(text_content)
  554. #openshot image holder
  555. sub_img_list = [None] * len(text_content)
  556. idx = 0
  557. for p in listdir(dir_photo+name_hash):
  558. anchor_list[idx] = openshot.FFmpegReader(dir_anchor+name_hash+"/"+str(idx)+".mp4")
  559. clip_duration = anchor_list[idx].info.duration
  560. anchor_list[idx].Open()
  561. anchor_clip_list[idx] = video_photo_clip(vid=anchor_list[idx],layer=4,scale_x=0.65,scale_y=0.65,
  562. location_x=0.35,location_y=0.25,position=main_timer, end=clip_duration,ck=ck_anchor,audio=False)
  563. t.AddClip(anchor_clip_list[idx])
  564. #insert image
  565. img_list[idx] = openshot.FFmpegReader(dir_photo+name_hash+'/'+p)
  566. img_list[idx].Open()
  567. photo_clip_list[idx] = video_photo_clip(vid=img_list[idx],layer=3
  568. ,scale_x=0.81,scale_y=0.68,location_y=-0.03,position=main_timer,end=clip_duration,audio=False)
  569. t.AddClip(photo_clip_list[idx])
  570. img_list[idx].Close()
  571. #insert audio (speech)
  572. audio_list[idx] = openshot.FFmpegReader(dir_sound+name_hash+"/"+str(idx)+".mp3")
  573. audio_list[idx].Open()
  574. audio_clip_list[idx] = openshot.Clip(audio_list[idx])
  575. audio_clip_list[idx].Position(main_timer)
  576. audio_clip_list[idx].End(clip_duration)
  577. t.AddClip(audio_clip_list[idx])
  578. #insert subtitle
  579. sub_img_list[idx] = openshot.QtImageReader(sub_list[idx])
  580. sub_img_list[idx].Open()
  581. 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)
  582. t.AddClip(sub_clip_list[idx])
  583. img_list[idx].Close()
  584. anchor_list[idx].Close()
  585. audio_list[idx].Close()
  586. sub_img_list[idx].Close()
  587. main_timer += clip_duration
  588. idx+=1
  589. LOGO_ED = openshot.FFmpegReader(dir_video+"ED_ENG.mp4")
  590. LOGO_ED.Open()
  591. LOGO_ED_clip = video_photo_clip(vid=LOGO_ED,layer=4,position=main_timer,end=LOGO_ED.info.duration+2
  592. ,location_x=0.005,location_y=-0.031
  593. ,scale_x=0.8,scale_y=0.6825)
  594. t.AddClip(LOGO_ED_clip)
  595. ED_duration = LOGO_ED.info.duration
  596. LOGO_ED.Close()
  597. bg = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  598. bg.Open()
  599. bg_times = math.floor(main_timer+ED_duration/bg.info.duration)
  600. left_time = (main_timer+ED_duration) % bg.info.duration
  601. bg_clip_list = [None] * bg_times
  602. bg_list = [None] * bg_times
  603. bg.Close()
  604. bg_timer = head_duration
  605. for idx in range(bg_times):
  606. bg_list[idx] = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  607. bg_list[idx].Open()
  608. bg_clip_list[idx] = video_photo_clip(bg_list[idx],layer=2,position=bg_timer
  609. ,end=bg_list[idx].info.duration,ck=ck)
  610. t.AddClip(bg_clip_list[idx])
  611. bg_timer += bg_list[idx].info.duration
  612. bg_list[idx].Close()
  613. bg_left = openshot.FFmpegReader(dir_video+"complete_double_aispokesgirl.mp4")
  614. bg_left.Open()
  615. bg_left_clip = video_photo_clip(bg_left,layer=2,position=bg_timer,end=left_time,ck=ck)
  616. t.AddClip(bg_left_clip)
  617. bg_left.Close()
  618. title = openshot.QtImageReader(dir_title+name_hash+".png")
  619. title.Open() # Open the reader
  620. title_clip = video_photo_clip(vid=title, layer=4,location_x=-0.047, location_y=0.801,position=0,end=head_duration+main_timer)
  621. t.AddClip(title_clip)
  622. ####start building
  623. w = openshot.FFmpegWriter(tmp_video_dir+name_hash+".mp4")
  624. w.SetAudioOptions(True, "aac", 44100, 2, openshot.LAYOUT_STEREO, 3000000)
  625. w.SetVideoOptions(True, "libx264", openshot.Fraction(30000, 1000), 1280, 720,
  626. openshot.Fraction(1, 1), False, False, 3000000)
  627. w.Open()
  628. #may change duration into t.info.duration
  629. frames = int(t.info.fps)*int(head_duration+main_timer+ED_duration)
  630. for n in range(frames):
  631. f=t.GetFrame(n)
  632. w.WriteFrame(f)
  633. notify_group(name+"(ENG)的影片已經產生完成囉! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
  634. t.Close()
  635. w.Close()
  636. print("video at : www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
  637. #line notifs
  638. class video_service(rpyc.Service):
  639. def exposed_call_video(self,name_hash,name,text_content, image_urls,multiLang,avatar):
  640. print('ML:'+str(multiLang))
  641. anchor_video_v2(name_hash,name,text_content, image_urls,multiLang,avatar)
  642. def exposed_call_video_eng(self,name_hash,name,text_content, image_urls,sub_titles,avatar):
  643. anchor_video_eng(name_hash,name,text_content, image_urls,sub_titles,avatar)
  644. def exposed_call_video_gen(self,name_hash,name,text_content, image_urls,multiLang,avatar):
  645. print('ML:'+str(multiLang))#this is long video version,
  646. video_gen(name_hash,name,text_content, image_urls,multiLang,avatar)
  647. from rpyc.utils.server import ThreadedServer
  648. t = ThreadedServer(video_service, port=8858)
  649. print('service started')
  650. t.start()