import pyttsx3
import rpyc
import random
import time
#conn = rpyc.classic.connect("192.168.1.110",18812)
conn = rpyc.classic.connect("192.168.192.134",18812)


ros = conn.modules.os 
rsys = conn.modules.sys 


fr=open('c:/video/congress.mp3','rb')
fw=conn.builtins.open('/tmp/ok2.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/Wav2Lip-master')
#ros.system('./proc.sh '+str(val)+' &')
ros.system('./p2.sh '+str(val)+' &')


while True:
    print('waiting...')
    if ros.path.exists('/tmp/results/'+str(val)):
        break
    time.sleep(5)
    print('waiting...')



ros.system('cp /home/jared/Wav2Lip-master/results/result_voice.mp4 /tmp')

#ros.chdir('/home/jared/tests/libopenshot')
#ros.system('python3 jaredtest.py')

#ros.system('cp /tmp/out.mp4 /var/www/html/out.mp4')
#
#ros.system('telegram-send "video is ready, http://139.162.121.30:54321/out.mp4"')


fr=conn.builtins.open('/home/jared/Wav2Lip-master/results/result_voice.mp4','rb')
#fr=conn.builtins.open('/tmp/out.mp4','rb')

fw=open('c:/video/result.mp4','wb')
while True:
    b=fr.read(1024)
    if b:
        fw.write(b)
    else:
        break

fr.close()
fw.close()



#fr=conn.builtins.open('/home/jared/Wav2Lip-master/results/result_voice.mp4','rb')
#fr=conn.builtins.open('/tmp/out.mp4','rb')

#fw=open('result.mp4','wb')
#while True:
#    b=fr.read(1024)
#    if b:
#        fw.write(b)
#    else:
#        break

#fr.close()
#fw.close()
