Real-time streaming of multiple videos from Flask to front-end

50 Views Asked by At

Here is what I want to achieve: there is a playlist of videos on the server, I want to play them sequentially in the background (even if no clients are connected), and whenever a client connects, I will stream the current progress of the videos. So, essentially, this is like a livestream.

I was thinking of trying to set up an RTMP server, and then embed a stream in the front-end webpage. But this seems a bit complicated to achieve in Flask. I also looked at WebRTC, but it doesn't seem to be any better.

I am wondering if there is a relatively easy way to do this. Scalability doesn't really matter here, as I will only be expecting less than 5 concurrent clients at most.

1

There are 1 best solutions below

0
Christoph On

If you need a simple solution first i suggest you to create a websocket server like fastapi with websockets. Then you would internally track connections, when a new client is connected you add them to a list and remove them when they are disconnect. Then you could create a asyncio task that would iterate over your Videos, for this use "av" libary what is a cool ffmpeg wrapper. Create a functions that decode a Video and encode it to JPEG, and send this JPEG to all clients that are in websocket connection list. On JS side use JPEG Bytes with img src (objectUrl) to Display the Stream.