Using FFMPEG to stream continuously videos files to a RTMP server

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.


Update (as I can't delete the accepted answer): the proper solution is to implement a custom demuxer, similar to the concat one. There's currently no other clean way. You have to get your hands dirty and code!

Below is an ugly hack. This is a very bad way to do it, just don't!

The solution uses the concat demuxer and assumes all your source media files use the same codec. The example is based on MPEG-TS but the same can be done for RTMP.

  1. Make a playlist file holding a huge list of entry points for you dynamic playlist with the following format:

    file 'item_1.ts' file 'item_2.ts' file 'item_3.ts' [...] file 'item_[ENOUGH_FOR_A_LIFETIME].ts'

    These files are just placeholders.

  2. Make a script that keeps track of you current playlist index and creates symbolic links on-the-fly for current_index + 1

    ln -s /path/to/what/to/play/next.ts item_1.ts

    ln -s /path/to/what/to/play/next.ts item_2.ts

    ln -s /path/to/what/to/play/next.ts item_3.ts

    [...]

  3. Start playing ffmpeg -f concat -i playlist.txt -c copy output -f mpegts udp://<ip>:<port>

  4. Get chased and called names by an angry system administrator


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.


Need to create two playlist files and at the end of each file specify a link to another file.

list_1.txt

ffconcat version 1.0
file 'item_1.mp4'
file 'list_2.txt'

list_2.txt

ffconcat version 1.0
file 'item_2.mp4'
file 'list_1.txt'

Now all you need is to dynamically change the contents of the next playlist file.