main.py 25 KB

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