toolAvatarVoiceOnly.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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)
  32. call_anchor(name_hash,text_in.avatar)
  33. def makeMP3(name_hash,text_in):
  34. lang = text_in.lang
  35. txt = text_in.text
  36. if lang==0:
  37. tts = gTTS(txt)
  38. tts.save(dir_sound+name_hash+"raw.mp3")
  39. else:
  40. tts = gTTS(txt,lang='zh-tw')
  41. tts.save(dir_sound+name_hash+"raw.mp3")
  42. #speed up
  43. ff = ffmpy.FFmpeg(inputs={dir_sound+name_hash+"raw.mp3": None}
  44. , outputs={dir_sound+name_hash+".mp3": ["-filter:a", "atempo=1.2"]})
  45. ff.run()
  46. os.remove(dir_sound+name_hash+"raw.mp3")
  47. def call_anchor(name_hash,avatar):
  48. conn = rpyc.classic.connect("192.168.1.111",18812)
  49. ros = conn.modules.os
  50. rsys = conn.modules.sys
  51. fr=open(dir_sound+name_hash+".mp3",'rb')# voice
  52. #warning!!! file my be replaced by other process
  53. fw=conn.builtins.open('/tmp/output.mp3','wb')
  54. while True:
  55. b=fr.read(1024)
  56. if b:
  57. fw.write(b)
  58. else:
  59. break
  60. fr.close()
  61. fw.close()
  62. val=random.randint(1000000,9999999)
  63. ros.chdir('/home/jared/to_video')
  64. ros.system('./p'+str(avatar)+'.sh '+str(val)+' &')
  65. while True:
  66. print('waiting...')
  67. if ros.path.exists('/tmp/results/'+str(val)):
  68. break
  69. time.sleep(5)
  70. print('waiting...')
  71. fr=conn.builtins.open('/tmp/results/'+str(val)+'.mp4','rb')
  72. fw=open(dir_anchor+name_hash+".mp4",'wb')
  73. while True:
  74. b=fr.read(1024)
  75. if b:
  76. fw.write(b)
  77. else:
  78. break
  79. fr.close()
  80. fw.close()