openshot_video_generator.py 21 KB

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