toolAvatarVoiceOnly.py 3.2 KB

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