[FFmpeg-user] FFmpeg CLI - swap RTMP source using ZMQ (zmqsend)

Jess Portnoy jess.portnoy at kaltura.com
Thu Apr 30 16:06:05 EEST 2020


Hello,

In case it's of interest to anyone, I solved my problem in a different way.
I now use named pipes, like so:

```
PIPE_FILE= /path/to/pip/file
mkfifo $PIPE_FILE
exec 7<>$PIPE_FILE
ffmpeg -nostdin  -i /path/to/source -acodec copy -vcodec copy -vbsf h264_mp4toannexb -f mpegts pipe:1 > $PIPE_FILE
```

`/path/to/source` can be a media file on the FS or an RTMP stream, for that matter.

I then re-stream from the pipe to the final RTMP endpoint:
```sh
ffmpeg -re -i $PIPE_FILE -c:v libx264 -preset veryfast -r 25 -g 50 -f flv $RTMP_ENDPOINT
```

When $PIPE_FILE stops receiving data (i.e - when streaming stops or, in the case of sending data from a local media file, when EOF is reached), I immediately launch a different FFmpeg CLI proc and feed the pipe data from the backup media file/stream.

That keeps the re-streaming FFmpeg CLI proc continuously up and running.

Cheers,

Excerpts from Jess Portnoy's message of 2020-04-21 13:00:06 +0100:
> Hello all,
> 
> My setup is as follows:
> 
> - Nginx with the RTMP module
> - Multiple RTMP stream pairs, each one with a primary and backup RTMP endpoint (so streaming to rtmp://localhost/main/$STREAM_NAME and rtmp://localhost/backup/$STREAM_NAME)
> - Using the Nginx RTMP module `exec_publish` and `exec_publish_done` hooks, I push either main or backup to an FFmpeg CLI proc that restreams it to a remote RTMP endpoint (Wowza server in this case, though it's not very relevant to my question)
> 
> My problem is that currently, if the main stream is stopped, I have to stop the FFmpeg CLI process that restreams to Wowza and start another with a new input source (the backup stream). This often causes issues on the Wowza side so I'm looking for a way to avoid that.
> 
> After some research, I found that FFmpeg encapsulated ZMQ support but it seems documentation is somewhat sparse. Is it possible to send a message to the running FFmpeg process to alert it that it must change its source to a different RTMP stream?
> 
> I have reviewed https://ffmpeg.org/pipermail/ffmpeg-user/2014-August/022827.html which seems to suggest that perhaps this can be accomplished but requires implementing extra functionality.
> 
> I found some examples for using ZMQ to send commands to a running FFmpeg CLI proc, for instance:
> https://lists.ffmpeg.org/pipermail/ffmpeg-user/2016-September/033777.html
> 
> But no example for what I'm trying to achieve. If it doesn't exist, I am happy to implement it myself as suggested in the first thread I referenced but would appreciate any pointers.
> 
> Another alternative, if it's possible and easier, would perhaps be to start the FFmpeg proc with two input sources and somehow toggle between them with a select filter. For my purposes, one doesn't actually have to be an RTMP stream, it can be a media file that I have on the FS. A flow such as:
> If the source RTMP stream receives data, restream to Wowza, otherwise, stream the backup media file would also work well for me.
> 
> Thanks in advance,
> 

-- 
May the source be with you,

Jess Portnoy


More information about the ffmpeg-user mailing list