I want to show the video on the browser streamed by ffmpeg.
Here’s the python script which i made but can’t find the way to do so since i am not much into javascript. Can anyone help me please ?
import socketio
import subprocess
import uvicorn
sio = socketio.AsyncServer(async_mode='asgi', cors_allowed_origins='*')
app = socketio.ASGIApp(sio)
@sio.event
async def connect(sid, environ):
await sio.enter_room(sid, 'Streaming')
@sio.event
async def stream(cid):
video_path = r'temp_serverresult.avi'
audio_path = r'temp_servertemp.wav'
ffmpeg_command = [
'ffmpeg',
'-re',
'-i', video_path,
'-i', audio_path,
'-c:v', 'copy',
'-c:a', 'aac',
'-f', 'mpegts',
'pipe:1'
]
try:
# Open a subprocess with pipes for stdout
process = subprocess.Popen(ffmpeg_command, stdout=subprocess.PIPE)
while True:
data = process.stdout.read(1024)
if not data:
break
await sio.emit('video', data, to=cid)
finally:
process.stdout.close()
print("Stream ended.")
if __name__ == '__main__':
try:
uvicorn.run(app, host='0.0.0.0', port=8000)
except KeyboardInterrupt:
exit()
New contributor
sam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.