Using FFMPEG to stream continuously videos files to a RTMP server

Viewed 67241

ffmpeg handles RTMP streaming as input or output, and it's working well.

I want to stream some videos (a dynamic playlist managed by a python script) to a RTMP server, and i'm currently doing something quite simple: streaming my videos one by one with FFMPEG to the RTMP server, however this causes a connection break every time a video end, and the stream is ready to go when the next video begins.

I would like to stream those videos without any connection breaks continuously, then the stream could be correctly viewed.

I use this command to stream my videos one by one to the server

ffmpeg -re -y -i myvideo.mp4 -vcodec libx264 -b:v 600k -r 25 -s 640x360 \
-filter:v yadif -ab 64k -ac 1 -ar 44100 -f flv \
"rtmp://mystreamingserver/app/streamName"

I looked for some workarounds over the internet for many days, and i found some people talking about using a named pipe as input in ffmpeg, I've tried it and it didn't work well since ffmpeg does not only close the RTMP stream when a new video comes but also closes itself.

Is there any way to do this ? (stream a dynamic playlist of videos with ffmpeg to RTMP server without connection breaks

4 Answers

You can pipe your loop to a buffer, and from this buffer you pipe to your streaming instance.

In shell it would look like:

#!/bin/bash

for i in *.mp4; do
        ffmpeg -hide_banner -nostats -i "$i" -c:v mpeg2video \
[proper settings] -f mpegts -
done | mbuffer -q -c -m 20000k | ffmpeg -hide_banner \ 
-nostats -re -fflags +igndts \ 
-thread_queue_size 512 -i pipe:0 -fflags +genpts \ 
[proper codec setting] -f flv rtmp://127.0.0.1/live/stream

Of course you can use any kind of loop, also looping through a playlist.

  • I figure out that mpeg is a bit more stabile, then x264 for the input stream.
  • I don't know why, but minimum 2 threads for the mpeg compression works better.
  • the input compression need to be faster then the output frame rate, so we get fast enough new input.
  • Because of the non-continuing timestamp we have to skip them and generate a new one in the output.
  • The buffer size needs to be big enough for the loop to have enough time to get the new clip.

I work on python based solution, is not complete yet, but except some warnings my test stream runs multiple days:

ffplayout

This uses a xml playlist format. And the Playlist is dynamic, in that way that you can edit always the current playlist, and change tracks or add new ones.

Very Late Answer, but I recently ran into the exact same issue as the poster above.

I solved this problem by using OBS and the OBS websockets plugin.

First, set your RTMP streaming app as you have it now. but stream to a LOCAL RTMP stream.

Then have OBS load this RTMP stream as a VLC source layer with the local RTMP as the source.

then (in your app), using the OBS websockets plugin, have your VLC source switch to a static black video or PNG file when the video ends. Then switch back to the RTMP stream once the next video starts. This will prevent the RTMP stream from stopping when the video ends. OBS will go black durring the short transition, but the final OBS RTMP output will never stop.

There is surely a way to do this with manually setting up a intermediate RTMP server that pushes to a final RTMP server, but I find using OBS to be easier, with little overhead.

I hope this helps others, this solutions has been working incredible for me.

Related