Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using FFMPEG to stream continuously videos files to a RTMP server

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

like image 293
kketch Avatar asked Jul 25 '12 10:07

kketch


People also ask

How do I reduce latency in FFmpeg?

You can also decrease latency by tuning any broadcast server you are using to minimize latency, and finally by tuning the client that receives the stream to not "cache" any incoming data, which, if it does, increases latency. Sometimes audio codecs also introduce some latency of their own.


2 Answers

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

like image 137
aergistal Avatar answered Oct 10 '22 18:10

aergistal


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.

like image 35
bnku Avatar answered Oct 10 '22 19:10

bnku