main.py 25 KB

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