toolAvatarVoiceOnly.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from fastapi import APIRouter
  2. from pydantic import BaseModel
  3. from gtts import gTTS
  4. import ffmpy
  5. from typing import Optional
  6. from fastapi.staticfiles import StaticFiles
  7. from fastapi import FastAPI,File,Request,Response
  8. import util,os, math, time
  9. from pydantic import BaseModel
  10. from fastapi.templating import Jinja2Templates
  11. import queue
  12. import threading
  13. q = queue.Queue()
  14. router = APIRouter()
  15. class text_in(BaseModel):
  16. text: str
  17. lang: int #0:eng 1:zh
  18. avatar: int
  19. dir_sound = ''
  20. dir_anchor = ''
  21. @router.post("/get_material/", tags=["tools"])
  22. async def read_users(text_in: text_in):
  23. q.put(memberOfQueue(text_in))
  24. x = threading.Thread(target=memberOfQueue, args=(text_in))
  25. x.start()
  26. return {'msg':'Pleas wait'}
  27. def memberOfQueue(text_in):
  28. q.put(compose(text_in))
  29. def compose(text_in):
  30. name_hash = str(time.time()).replace('.','')
  31. makeMP3(name_hash,text_in.text, text_in.lang)
  32. call_anchor(name_hash,text_in.avatar)
  33. def makeMP3(text_in,lang):
  34. if lang==0:
  35. tts = gTTS(txt)
  36. tts.save(dir_sound+name_hash+"raw.mp3")
  37. else:
  38. tts = gTTS(txt,lang='zh-tw')
  39. tts.save(dir_sound+name_hash+"raw.mp3")
  40. #speed up
  41. ff = ffmpy.FFmpeg(inputs={dir_sound+name_hash+"raw.mp3": None}
  42. , outputs={dir_sound+name_hash+".mp3": ["-filter:a", "atempo=1.2"]})
  43. ff.run()
  44. os.remove(dir_sound+name_hash+"/raw.mp3")
  45. def call_anchor(name_hash,avatar):
  46. conn = rpyc.classic.connect("192.168.1.111",18812)
  47. ros = conn.modules.os
  48. rsys = conn.modules.sys
  49. fr=open(dir_sound+name_hash+".mp3",'rb')# voice
  50. #warning!!! file my be replaced by other process
  51. fw=conn.builtins.open('/tmp/output.mp3','wb')
  52. while True:
  53. b=fr.read(1024)
  54. if b:
  55. fw.write(b)
  56. else:
  57. break
  58. fr.close()
  59. fw.close()
  60. val=random.randint(1000000,9999999)
  61. ros.chdir('/home/jared/to_video')
  62. ros.system('./p'+str(avatar)+'.sh '+str(val)+' &')
  63. while True:
  64. print('waiting...')
  65. if ros.path.exists('/tmp/results/'+str(val)):
  66. break
  67. time.sleep(5)
  68. print('waiting...')
  69. fr=conn.builtins.open('/tmp/results/'+str(val)+'.mp4','rb')
  70. fw=open(dir_anchor+name_hash+".mp4",'wb')
  71. while True:
  72. b=fr.read(1024)
  73. if b:
  74. fw.write(b)
  75. else:
  76. break
  77. fr.close()
  78. fw.close()