123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- from typing import Optional
- from fastapi.staticfiles import StaticFiles
- from fastapi import FastAPI,File, UploadFile,Request,Response
- import util,os, math, time
- import openshot
- from pydantic import BaseModel
- from fastapi.templating import Jinja2Templates
- templates = Jinja2Templates(directory="static")
- app = FastAPI()
- app.mount("/static", StaticFiles(directory="static"), name="static")
- rootPath = '/app/components/'
- @app.get("/")
- def read_root(request: Request, response: Response):
- return templates.TemplateResponse("uploadmp3.html", {"request": request, "response": response})
- @app.get("/modifyScript")
- def read_root(request: Request, response: Response):
- return templates.TemplateResponse("modifyScript.html", {"request": request, "response": response})
- @app.get("/items/{item_id}")
- def read_item(item_id: int, q: Optional[str] = None):
- return {"item_id": item_id, "q": q}
- class updateScriptModel(BaseModel):
- name_hash:str
- scriptStr:str
-
- @app.post("/updateScript")
- def read_item(info : updateScriptModel):
- lines = info.scriptStr.split(',')
- cPath = rootPath+info.name_hash+'/'
-
- util.rewriteScript(cPath,lines)
- sub_dict,img_dict=filePrepare(info.name_hash)
- genVideo(info.name_hash,sub_dict,img_dict)
- return 'ok'
- @app.post("/uploadmp3/")
- async def uploadmp3(file: UploadFile = File(...)):
- name_hash = str(time.time()).replace('.','')
- cPath = rootPath+name_hash+'/'
- try:
- os.mkdir(cPath)
- except FileExistsError:
- pass
- with open(cPath+'speech.mp3', "wb+") as file_object:
- file_object.write(file.file.read())
-
- util.transScript(cPath)
- scripts = util.get_script(cPath)
-
- return name_hash, scripts
- def filePrepare(name_hash):
- cPath = rootPath+name_hash+'/'
- try:
- os.mkdir(cPath)
- except FileExistsError:
- pass
- sub_dict,img_dict = util.parse_script("script.txt")
- util.generate_subtitle_image_from_dict(cPath, sub_dict)
- for imgd in img_dict:
- print(imgd)
- util.downloadFromDrive(cPath,imgd['imgid'],imgd['index'])
-
- util.call_anchor(cPath+'speech.mp3',7)
- return sub_dict,img_dict
- def genVideo(name_hash,sub_dict,img_dict):
- basicPath = rootPath+'basic/'
- cPath = rootPath+name_hash+'/'
- ck=util.cKey(0,254,0,270)
- ck_anchor=util.cKey(0,255,1,320)
- t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
- t.Open()
- main_timer = 0
- LOGO_OP = openshot.FFmpegReader(basicPath+"LOGO_OP_4.mp4")
- LOGO_OP.Open() # Open the reader
- head_duration = LOGO_OP.info.duration
- LOGO_OP_clip = util.video_photo_clip(vid=LOGO_OP,layer=4,position=0,end=head_duration)
- t.AddClip(LOGO_OP_clip)
- main_timer+=head_duration
- anchor = openshot.FFmpegReader(cPath+"/speaker.mp4")
- anchor.Open()
- anchor_clip = util.video_photo_clip(vid=anchor,layer=4,scale_x=0.65,scale_y=0.65,
- location_x=0.35,location_y=0.25,position=main_timer, end=anchor.info.duration,ck=ck_anchor,audio=False)
- t.AddClip(anchor_clip)
- speech = openshot.FFmpegReader(cPath+"/speech.mp3")
- speech.Open()
- speech_clip = openshot.Clip(speech)
- speech_clip.Position(main_timer)
- speech_clip.End(anchor.info.duration)
- t.AddClip(speech_clip)
- main_timer += anchor.info.duration
- anchor.Close()
- speech.Close()
- sub_img_list = [None] * len(sub_dict)
- sub_clip_list = [None] * len(sub_dict)
- for sub_obj in sub_dict:
- idx = int(sub_obj['index'])
- sub_img_list[idx] = openshot.QtImageReader(cPath +str(idx)+'.png')
- sub_img_list[idx].Open()
- sub_clip_list[idx] = util.video_photo_clip(vid=sub_img_list[idx], layer=5,location_x=0.069, location_y=0.89
- ,position=head_duration+sub_obj['start'],end=sub_obj['duration'])
- t.AddClip(sub_clip_list[idx])
- sub_img_list[idx].Close()
-
- img_list = [None] * len(img_dict)
- img_clip_list = [None] * len(img_dict)
- for img_d in img_dict:
- idx = int(img_d['index'])
- print(cPath +str(idx) +'img.jpg')
- try:
- img_list[idx] = openshot.QtImageReader(cPath +str(idx) +'img.jpg')
- img_list[idx].Open()
- except:
- img_list[idx] = openshot.QtImageReader(cPath +str(idx) +'img.png')
- img_list[idx].Open()
- img_clip_list[idx] = util.video_photo_clip(vid=img_list[idx], layer=3
- ,position=head_duration+img_d['start'],end=img_d['duration'])
- t.AddClip(img_clip_list[idx])
- img_list[idx].Close()
- w = util.video_writer_init("myraw.mp4")
- w.Open()
- frames = int(t.info.fps)*int(main_timer)
- for n in range(frames):
- f=t.GetFrame(n)
- w.WriteFrame(f)
- t.Close()
- w.Close()
|