from fastapi import APIRouter from pydantic import BaseModel from gtts import gTTS import ffmpy import random from typing import Optional from fastapi.staticfiles import StaticFiles from fastapi import FastAPI,File,Request,Response,UploadFile import util,os, math, time from pydantic import BaseModel from fastapi.templating import Jinja2Templates import queue, shutil import threading import rpyc import requests q = queue.Queue() router = APIRouter() class text_in(BaseModel): text: str lang: int #0:eng 1:zh avatar: int dir_sound = '' dir_anchor = '' tool_dest = '/var/www/html/tools/' @router.post("/genAvatar_uploadmp3/", tags=["tools"]) async def genAvatar_uploadmp3(file: UploadFile = File(...)): name_hash = str(time.time()).replace('.','') with open(dir_sound+name_hash+".mp3", "wb+") as file_object: file_object.write(file.file.read()) x = threading.Thread(target=makeAvatar, args=(str(name_hash),99)) x.start() return {'msg':'ok'} def makeAvatar(name_hash,avatar): call_anchor(name_hash,avatar) shutil.copy(dir_anchor+name_hash+".mp4",tool_dest+name_hash+'.mp4') notify_choozmo('avatar at www.choozmo.com:8168/tools/'+name_hash+'.mp4') os.remove(dir_sound+name_hash+".mp3") @router.post("/get_material/", tags=["tools"]) async def get_material(text_in: text_in): x = threading.Thread(target=memberOfQueue, args=(text_in.text,text_in.lang,text_in.avatar)) x.start() return {'msg':'Pleas wait'} def memberOfQueue(txt,lang,avatar): q.put(compose(txt,lang,avatar)) def compose(txt,lang,avatar): name_hash = str(time.time()).replace('.','') makeMP3(name_hash,txt,lang) call_anchor(name_hash,avatar) shutil.copy(dir_sound+name_hash+".mp3",tool_dest+name_hash+'.mp3') shutil.copy(dir_anchor+name_hash+".mp4",tool_dest+name_hash+'.mp4') os.remove(dir_sound+name_hash+".mp3") os.remove(dir_anchor+name_hash+".mp4") notify_choozmo('sound at www.choozmo.com:8168/tools/'+name_hash+'.mp3') notify_choozmo('avatar at www.choozmo.com:8168/tools/'+name_hash+'.mp4') def makeMP3(name_hash,txt,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...',val,',nh:',name_hash) if ros.path.exists('/tmp/results/'+str(val)): break time.sleep(5) 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() def notify_choozmo(msg): #'WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD' is ChoozmoTeam glist = ['WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD'] for gid in glist: headers = {"Authorization": "Bearer " + gid,"Content-Type": "application/x-www-form-urlencoded"} r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params={"message": msg})