1

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_server\result.avi'
    audio_path = r'temp_server\temp.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()

1 Answer 1

0

Don't use Socket.IO.

Rather than all the extra machinery for Socket.IO and its transports, you can just use regular HTTP. And, if you do that, you don't even need anything special on the client-side. A regular video element will suffice.

<video src="https://your-python-server.example.com/stream" preload="none"></video>
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, but the thing is that this is just the demo code. My AI model will generate video from audio and then i want to stream it so another script will be connected with this file and will send the file paths (which currently is static) and i want to merge them and send it and there will be more than one file means i need some kind of queue mechanism in client-side

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.