123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- 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
- import util,os, math, time
- from pydantic import BaseModel
- from fastapi.templating import Jinja2Templates
- import queue
- import threading
- import rpyc
- 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):
- 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)
- 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...')
- 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()
|