FFmpeg Tee Muxer: Stream to Multiple RTMP Servers
This article explains how to configure the tee muxer in
FFmpeg to split a single input stream and broadcast it simultaneously to
two different RTMP destinations. You will learn the exact command-line
syntax, key configuration parameters, and best practices to ensure
stable, low-latency dual-streaming without unnecessarily taxing your
system’s CPU.
The tee muxer is an efficient tool that duplicates the
output of an FFmpeg process, allowing you to write to multiple
destinations (like YouTube and Twitch) at the same time. Because it
duplicates the packet stream after encoding, your CPU only has to encode
the video and audio once, saving massive amounts of processing
power.
Basic Command Syntax
To stream to two RTMP servers, use the -f tee output
flag and map your destinations inside double quotes, separated by a pipe
(|) character. Each destination must specify its muxer
format using the f=flv flag, as RTMP requires the FLV
container format.
Here is the basic command structure:
ffmpeg -i input_source -c:v libx264 -c:a aac -f tee "[f=flv]rtmp://server1.example.com/app/stream_key1|[f=flv]rtmp://server2.example.com/app/stream_key2"Command Breakdown
-i input_source: Your input, which could be a local video file, a webcam, or an RTSP stream.-c:v libx264 -c:a aac: Encodes the video to H.264 and the audio to AAC, which are standard formats for RTMP streaming.-f tee: Instructs FFmpeg to use theteemuxer."[f=flv]rtmp://...": The double quotes enclose the output destinations. Inside,[f=flv]tells the tee muxer to wrap the stream in an FLV container before sending it to the respective RTMP URL.|: The pipe character separates the different stream destinations.
Handling Connection Failures (Onfail Option)
By default, if one of your RTMP connections drops, the entire FFmpeg
process will crash, stopping the stream to the surviving server. To
prevent this, you can use the onfail=ignore option. This
ensures that if one RTMP server disconnects, FFmpeg will continue
streaming to the other.
Add onfail=ignore inside the brackets of each
destination:
ffmpeg -i input_source -c:v libx264 -preset veryfast -maxrate 3000k -bufsize 6000k -c:a aac -f tee "[f=flv:onfail=ignore]rtmp://server1.example.com/app/stream_key1|[f=flv:onfail=ignore]rtmp://server2.example.com/app/stream_key2"Streaming with Existing Encoded Video (No Re-encoding)
If your input source is already encoded in an RTMP-compatible format
(H.264 video and AAC audio) and does not need to be re-encoded, you can
use the copy codec option to further reduce CPU usage to
near zero:
ffmpeg -i input_source -c copy -f tee "[f=flv]rtmp://server1.example.com/app/stream_key1|[f=flv]rtmp://server2.example.com/app/stream_key2"