openshot_video_generator.py 21 KB

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