from fastapi import APIRouter from pydantic import BaseModel from gtts import gTTS import ffmpy from typing import Optional from fastapi.staticfiles import StaticFiles from fastapi import FastAPI,File,Request,Response import util,os, math, time from pydantic import BaseModel from fastapi.templating import Jinja2Templates import queue import threading q = queue.Queue() router = APIRouter() class text_in(BaseModel): text: str lang: int #0:eng 1:zh avatar: int dir_sound = '' dir_anchor = '' @router.post("/get_material/", tags=["tools"]) async def read_users(text_in: text_in): q.put(memberOfQueue(text_in)) x = threading.Thread(target=memberOfQueue, args=(text_in)) x.start() return {'msg':'Pleas wait'} def memberOfQueue(text_in): q.put(compose(text_in)) def compose(text_in): name_hash = str(time.time()).replace('.','') makeMP3(name_hash,text_in.text, text_in.lang) call_anchor(name_hash,text_in.avatar) def makeMP3(text_in,lang): if lang==0: tts = gTTS(txt) tts.save(dir_sound+name_hash+"raw.mp3") else: tts = gTTS(txt,lang='zh-tw') tts.save(dir_sound+name_hash+"raw.mp3") #speed up ff = ffmpy.FFmpeg(inputs={dir_sound+name_hash+"raw.mp3": None} , outputs={dir_sound+name_hash+".mp3": ["-filter:a", "atempo=1.2"]}) ff.run() os.remove(dir_sound+name_hash+"/raw.mp3") def call_anchor(name_hash,avatar): conn = rpyc.classic.connect("192.168.1.111",18812) ros = conn.modules.os rsys = conn.modules.sys fr=open(dir_sound+name_hash+".mp3",'rb')# voice #warning!!! file my be replaced by other process fw=conn.builtins.open('/tmp/output.mp3','wb') while True: b=fr.read(1024) if b: fw.write(b) else: break fr.close() fw.close() val=random.randint(1000000,9999999) ros.chdir('/home/jared/to_video') ros.system('./p'+str(avatar)+'.sh '+str(val)+' &') while True: print('waiting...') if ros.path.exists('/tmp/results/'+str(val)): break time.sleep(5) print('waiting...') fr=conn.builtins.open('/tmp/results/'+str(val)+'.mp4','rb') fw=open(dir_anchor+name_hash+".mp4",'wb') while True: b=fr.read(1024) if b: fw.write(b) else: break fr.close() fw.close()