Prevent FFmpeg Tee Muxer Crash on Connection Failure
This article explains how to configure the FFmpeg tee
muxer to ignore connection failures on individual outputs. By default,
if one destination in a multi-output stream fails, the entire FFmpeg
process terminates. You will learn how to use the onfail
option and the fifo muxer to keep your remaining streams
running smoothly when a single network connection drops.
Using the
onfail=ignore Option
The tee muxer allows you to apply slave-specific options
to each output by wrapping them in square brackets. To prevent a
connection failure on one output from stopping the entire FFmpeg
process, you must add the onfail=ignore parameter to that
specific output definition.
Here is a basic example of streaming to two RTMP destinations where the first destination is allowed to fail:
ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f tee \
"[f=flv:onfail=ignore]rtmp://primary-destination.com/live/stream_key|[f=flv]rtmp://backup-destination.com/live/stream_key"In this command: * [f=flv:onfail=ignore] specifies that
if the connection to primary-destination.com fails to
establish or drops during the broadcast, FFmpeg will ignore the failure
and continue sending data to backup-destination.com. * The
pipe character (|) separates the independent outputs.
Ensuring Non-Blocking Outputs with the FIFO Muxer
While onfail=ignore prevents FFmpeg from crashing
immediately upon connection loss, a slow or blocking network connection
can still freeze the entire encoding pipeline. Because the
tee muxer writes to outputs synchronously, a delay on one
stream will cause data to back up, affecting the other streams.
To solve this, combine the tee muxer with the
fifo muxer. The fifo muxer buffers the data in
a separate thread, allowing the main FFmpeg process to continue
uninterrupted even if one destination temporarily slows down or
disconnects.
Here is the recommended production-ready syntax:
ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f tee \
"[f=fifo:fifo_format=flv:onfail=ignore]rtmp://primary-destination.com/live/stream_key|[f=fifo:fifo_format=flv:onfail=ignore]rtmp://backup-destination.com/live/stream_key"In this advanced command: * f=fifo tells the
tee muxer to pass the stream to the fifo muxer
first. * fifo_format=flv specifies the actual output format
(FLV for RTMP streaming) that the FIFO muxer will use. *
onfail=ignore ensures that if the FIFO queue overflows or
the network connection fails completely, the stream is dropped silently
without interrupting the other output.