|
@@ -74,17 +74,6 @@ async def qqreq(qq:QQ):
|
|
|
print(qq.n2)
|
|
|
return {"msg":"製作影片需要時間,請您耐心等候"}
|
|
|
|
|
|
-@app.post("/make_video_req")
|
|
|
-async def make_video_req(req:request):
|
|
|
- x = threading.Thread(target=make_video, args=(req.name, req.text_content, req.image_urls))
|
|
|
- x.start()
|
|
|
- return {"msg":"製作影片需要時間,請您耐心等候"}
|
|
|
-
|
|
|
-@app.post("/make_anchor_video")
|
|
|
-async def make_anchor_video(req:request):
|
|
|
- x = threading.Thread(target=anchor_video, args=(req.name, req.text_content, req.image_urls))
|
|
|
- x.start()
|
|
|
- return {"msg":"製作影片需要時間,請您耐心等候 稍後可以在www.choozmo.com:8168/"+req.name+".mp4 中觀看"}
|
|
|
|
|
|
@app.post("/make_anchor_video_v2")
|
|
|
async def make_anchor_video_v2(req:request2):
|
|
@@ -104,90 +93,6 @@ def notify_group(msg):
|
|
|
#print(r)
|
|
|
|
|
|
|
|
|
-def make_video(name, text_content, image_urls):
|
|
|
- #save image
|
|
|
- try:
|
|
|
- os.mkdir(dir_photo+name)
|
|
|
- except FileExistsError:
|
|
|
- print("Directory " , dir_photo+name , " already exists")
|
|
|
- img_num = 1
|
|
|
- for imgu in image_urls:
|
|
|
- im = Image.open(requests.get(imgu, stream=True).raw)
|
|
|
- im.save(dir_photo+name+"/"+str(img_num)+".jpg")
|
|
|
- img_num+=1
|
|
|
- #save text
|
|
|
- text_file = open(dir_text+name+".txt", "w")
|
|
|
- text_file.write(text_content)
|
|
|
- text_file.close()
|
|
|
- print("text file made")
|
|
|
- #make mp3
|
|
|
- tts = zhtts.TTS()
|
|
|
- tts.text2wav(text_content,dir_sound+name+".mp3")
|
|
|
- print("mp3 file made")
|
|
|
- #make vido
|
|
|
- t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
|
|
|
- t.Open()
|
|
|
- sound = openshot.FFmpegReader(dir_sound+name+".mp3")
|
|
|
- sound.Open()
|
|
|
- sound_track = openshot.Clip(sound)
|
|
|
- t.AddClip(sound_track)
|
|
|
- duration = sound.info.duration
|
|
|
-
|
|
|
- w = openshot.FFmpegWriter("../html/"+name+".mp4")
|
|
|
- w.SetAudioOptions(True, "libmp3lame", 44100, 2, openshot.LAYOUT_STEREO, 3000000)
|
|
|
- w.SetVideoOptions(True, "libx264", openshot.Fraction(30000, 1000), 1280, 720,
|
|
|
- openshot.Fraction(1, 1), False, False, 3000000)
|
|
|
- w.Open()
|
|
|
- p_num = len(listdir(dir_photo+name))
|
|
|
- clip_list = [None]*p_num
|
|
|
- img_list = [None]*p_num
|
|
|
- single_duration = int(duration/p_num)
|
|
|
- frame_cnt = 0
|
|
|
- for p in listdir(dir_photo+name):
|
|
|
- img_list[frame_cnt] = openshot.QtImageReader(dir_photo+name+"/"+p)
|
|
|
- clip_list[frame_cnt] = openshot.Clip(img_list[frame_cnt])
|
|
|
- clip_list[frame_cnt].has_audio=openshot.Keyframe(0)
|
|
|
- clip_list[frame_cnt].Layer(1)
|
|
|
- clip_list[frame_cnt].Position(frame_cnt*single_duration) # start from time 0 sec
|
|
|
- t.AddClip(clip_list[frame_cnt])
|
|
|
- frame_cnt+=1
|
|
|
-
|
|
|
- t_op = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
|
|
|
- t_op.Open()
|
|
|
- op_vid = openshot.FFmpegReader(dir_video+"LOGO_OP.mp4")
|
|
|
- op_vid.Open()
|
|
|
- op_clip = openshot.Clip(op_vid)
|
|
|
- t_op.AddClip(op_clip)
|
|
|
- duration_op = op_vid.info.duration
|
|
|
-
|
|
|
- for n in range(int(t_op.info.fps)*int(duration_op)):
|
|
|
- f=t_op.GetFrame(n)
|
|
|
- w.WriteFrame(f)
|
|
|
-
|
|
|
- for n in range(int(t.info.fps)*int(duration)):
|
|
|
- f=t.GetFrame(n)
|
|
|
- w.WriteFrame(f)
|
|
|
-
|
|
|
- t_ed = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
|
|
|
- t_ed.Open()
|
|
|
- ed_vid = openshot.FFmpegReader(dir_video+"LOGO_ED.avi")
|
|
|
- ed_vid.Open()
|
|
|
- ed_clip = openshot.Clip(ed_vid)
|
|
|
- t_ed.AddClip(ed_clip)
|
|
|
- duration_ed = ed_vid.info.duration
|
|
|
-
|
|
|
- for n in range(int(t_ed.info.fps)*int(duration_ed)):
|
|
|
- f=t_ed.GetFrame(n)
|
|
|
- w.WriteFrame(f)
|
|
|
-
|
|
|
-
|
|
|
- t_ed.Close()
|
|
|
- t_op.Close()
|
|
|
- t.Close()
|
|
|
- w.Close()
|
|
|
- sound.Close()
|
|
|
-
|
|
|
- notify_group(name+"的影片已經產生完成囉! www.choozmo.com:8168/"+name+".mp4")
|
|
|
|
|
|
def cKey(r,g,b,fuzz):
|
|
|
col=openshot.Color()
|
|
@@ -456,18 +361,6 @@ def split_by_pun(s):
|
|
|
return res
|
|
|
|
|
|
def generate_subtitle_image(name_hash,text_content):
|
|
|
- '''
|
|
|
- sub_len = 10
|
|
|
- img_list = [None]*len(text_content)
|
|
|
- for idx in range(len(text_content)):
|
|
|
- img_list[idx] = []
|
|
|
- for step in range(math.ceil(len(text_content[idx])/sub_len)):
|
|
|
- sv_path = dir_subtitle + name_hash +'/'+str(idx)+ str(step) +'.png'
|
|
|
- sub = text_content[idx][step*sub_len:step*sub_len+sub_len]
|
|
|
- txt2image(sub, sv_path)
|
|
|
- img_list[idx] += [{"count":len(sub),"path":sv_path}]
|
|
|
- print(sub+':'+sv_path)
|
|
|
- '''
|
|
|
img_list = [None]*len(text_content)
|
|
|
for idx in range(len(text_content)):
|
|
|
img_list[idx]=[]
|
|
@@ -480,131 +373,6 @@ def generate_subtitle_image(name_hash,text_content):
|
|
|
return img_list
|
|
|
|
|
|
|
|
|
-def anchor_video(name,text_content, image_urls):
|
|
|
- m = hashlib.md5()
|
|
|
- m.update(name.encode("utf-8"))
|
|
|
- name_hash = m.hexdigest()
|
|
|
-
|
|
|
- file_prepare(name, name_hash, text_content,image_urls)
|
|
|
- call_achor_video(name_hash)
|
|
|
-
|
|
|
- ck=cKey(0,254,0,150)
|
|
|
- ck_anchor=cKey(0,255,1,320)
|
|
|
- duration = 0
|
|
|
- current_time = 0
|
|
|
- current_time_photo = 0
|
|
|
- #average layer level is 3
|
|
|
- t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
|
|
|
- t.Open()
|
|
|
-
|
|
|
- bg_head = openshot.FFmpegReader(dir_video+"bg_head.avi")
|
|
|
- bg_head.Open()
|
|
|
- bg_head_clip = video_photo_clip(vid=bg_head,layer=2,position=current_time,end=bg_head.info.duration,ck=ck)
|
|
|
- t.AddClip(bg_head_clip)
|
|
|
-
|
|
|
- LOGO_OP = openshot.FFmpegReader(dir_video+"LOGO_OP.mp4")
|
|
|
- LOGO_OP.Open() # Open the reader
|
|
|
- LOGO_OP_clip = video_photo_clip(vid=LOGO_OP,layer=4,position=current_time,end=bg_head.info.duration
|
|
|
- ,location_y=-0.03,scale_x=0.8,scale_y=0.71)
|
|
|
- t.AddClip(LOGO_OP_clip)
|
|
|
- duration+=bg_head.info.duration
|
|
|
- current_time += bg_head.info.duration
|
|
|
- current_time_photo += bg_head.info.duration
|
|
|
-
|
|
|
- sound = openshot.FFmpegReader(dir_sound+name_hash+".mp3")
|
|
|
- sound.Open()
|
|
|
- sound_clip = openshot.Clip(sound)
|
|
|
- sound_clip.Position(current_time)
|
|
|
- t.AddClip(sound_clip)
|
|
|
- duration += sound.info.duration
|
|
|
-
|
|
|
- bg = openshot.FFmpegReader(dir_video+"bg.mp4")
|
|
|
- bg.Open()
|
|
|
- bg_times = math.floor(sound.info.duration/bg.info.duration)
|
|
|
- left_time = sound.info.duration % bg.info.duration
|
|
|
- bg_clip_list = [None] * bg_times
|
|
|
- bg_list = [None] * bg_times
|
|
|
- bg.Close()
|
|
|
- for idx in range(bg_times):
|
|
|
- bg_list[idx] = openshot.FFmpegReader(dir_video+"bg.mp4")
|
|
|
- bg_list[idx].Open()
|
|
|
- bg_clip_list[idx] = video_photo_clip(bg_list[idx],layer=2,position=current_time
|
|
|
- ,end=bg_list[idx].info.duration,ck=ck)
|
|
|
- t.AddClip(bg_clip_list[idx])
|
|
|
- current_time += bg_list[idx].info.duration
|
|
|
- bg_list[idx].Close()
|
|
|
-
|
|
|
- bg_left = openshot.FFmpegReader(dir_video+"bg.mp4")
|
|
|
- bg_left.Open()
|
|
|
- bg_left_clip = video_photo_clip(bg_left,layer=2,position=current_time,end=left_time,ck=ck)
|
|
|
- t.AddClip(bg_left_clip)
|
|
|
- current_time += left_time
|
|
|
- bg_left.Close()
|
|
|
-
|
|
|
-
|
|
|
- p_num = len(listdir(dir_photo+name_hash))
|
|
|
- photo_clip_list = [None]*p_num
|
|
|
- img_list = [None]*p_num
|
|
|
- photo_idx = 0
|
|
|
- photo_duration = sound.info.duration/p_num
|
|
|
- for p in listdir(dir_photo+name_hash):
|
|
|
- img_list[photo_idx] = openshot.QtImageReader(dir_photo+name_hash+'/'+p)
|
|
|
- photo_clip_list[photo_idx] = video_photo_clip(vid=img_list[photo_idx],layer=3
|
|
|
- ,scale_x=0.81,scale_y=0.68,location_y=-0.03,position=current_time_photo,end=photo_duration
|
|
|
- ,audio=False)
|
|
|
- t.AddClip(photo_clip_list[photo_idx])
|
|
|
- current_time_photo+=photo_duration
|
|
|
- photo_idx+=1
|
|
|
-
|
|
|
- current_time_anchor = bg_head.info.duration
|
|
|
- anchor = openshot.FFmpegReader(dir_anchor+name_hash+".mp4")
|
|
|
- anchor.Open()
|
|
|
- anchor_times = math.floor(sound.info.duration/anchor.info.duration)
|
|
|
- left_time = sound.info.duration % anchor.info.duration
|
|
|
- anchor_clip_list = [None] * anchor_times
|
|
|
- anchor_list = [None] * anchor_times
|
|
|
- anchor.Close()
|
|
|
- for idx in range(anchor_times):
|
|
|
- anchor_list[idx] = openshot.FFmpegReader(dir_anchor+name_hash+".mp4")
|
|
|
- anchor_list[idx].Open()
|
|
|
- anchor_clip_list[idx] = video_photo_clip(vid=anchor_list[idx],layer=4,scale_x=0.65,scale_y=0.65,
|
|
|
- location_x=0.35,location_y=0.22,position=current_time_anchor, end=anchor_list[idx].info.duration,
|
|
|
- ck=ck_anchor,audio=False)
|
|
|
- t.AddClip(anchor_clip_list[idx])
|
|
|
- current_time_anchor += anchor_list[idx].info.duration
|
|
|
- anchor_list[idx].Close()
|
|
|
- anchor = openshot.FFmpegReader(dir_anchor+"peggy1_1.mp4")
|
|
|
- anchor.Open()
|
|
|
- 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
|
|
|
- , position=current_time_anchor,end=left_time,ck=ck_anchor)
|
|
|
- t.AddClip(anchor_left_clip)
|
|
|
- anchor.Close()
|
|
|
-
|
|
|
- title = openshot.QtImageReader(dir_title+name_hash+".png")
|
|
|
- title.Open() # Open the reader
|
|
|
- title_clip = video_photo_clip(vid=title, layer=4, location_y=0.9,position=0,end=duration)
|
|
|
- t.AddClip(title_clip)
|
|
|
-
|
|
|
- ####start building
|
|
|
- w = openshot.FFmpegWriter("../html/"+name_hash+".mp4")
|
|
|
- w.SetAudioOptions(True, "libmp3lame", 44100, 2, openshot.LAYOUT_STEREO, 3000000)
|
|
|
- w.SetVideoOptions(True, "libx264", openshot.Fraction(30000, 1000), 1280, 720,
|
|
|
- openshot.Fraction(1, 1), False, False, 3000000)
|
|
|
- w.Open()
|
|
|
-
|
|
|
- #may change duration into t.info.duration
|
|
|
- for n in range(int(t.info.fps)*int(duration)):
|
|
|
- f=t.GetFrame(n)
|
|
|
- w.WriteFrame(f)
|
|
|
-
|
|
|
- t.Close()
|
|
|
- w.Close()
|
|
|
- sound.Close()
|
|
|
-
|
|
|
- print("It's fine")
|
|
|
- notify_group(name+"的影片已經產生完成囉! www.choozmo.com:8168/"+name_hash+".mp4")
|
|
|
-
|
|
|
-
|
|
|
def anchor_video_v2(name,text_content, image_urls):
|
|
|
m = hashlib.md5()
|
|
|
m.update(name.encode("utf-8"))
|
|
@@ -689,7 +457,7 @@ def anchor_video_v2(name,text_content, image_urls):
|
|
|
for sub_idx in range(len(sub_list[idx])):
|
|
|
sub_img_list[idx][sub_idx] = openshot.QtImageReader(sub_list[idx][sub_idx]['path'])
|
|
|
sub_img_list[idx][sub_idx].Open()
|
|
|
- sub_duration = 0.25*sub_list[idx][sub_idx]['count']
|
|
|
+ sub_duration = 0.22*sub_list[idx][sub_idx]['count']
|
|
|
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)
|
|
|
t.AddClip(sub_clip_list[idx][sub_idx])
|
|
|
sub_img_list[idx][sub_idx].Close()
|