Configure FFmpeg Tee Muxer for RTMP and Local File
This article explains how to configure the FFmpeg tee
muxer to split an input stream into multiple outputs simultaneously. You
will learn the exact command syntax needed to record your stream to a
local video file while broadcasting it to a live RTMP destination,
ensuring efficient resource usage without double-encoding.
The Basic Command Structure
The tee muxer works by taking a single encoded stream
and duplicating it into different output formats. This saves CPU cycles
because FFmpeg only encodes the video and audio once.
Here is the standard command template to output to a local MP4 file and an RTMP stream:
ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f tee -map 0:v -map 0:a "[f=flv]rtmp://your-rtmp-server/live/stream_key|[f=mp4]local_record.mp4"Explaining the Syntax
-c:v libx264 -c:a aac: Encodes the input video to H.264 and audio to AAC, which are standard formats compatible with both RTMP streaming and local MP4 playback.-f tee: Tells FFmpeg to use theteemuxer for the output.-map 0:v -map 0:a: Explicitly maps the first video stream and first audio stream to the output. Theteemuxer requires explicit stream mapping to function correctly."[f=flv]rtmp://...|[f=mp4]local_record.mp4": This is the output selection string.- The entire output string must be wrapped in double quotes.
- Outputs are separated by the pipe character (
|). - The bracketed options
[f=flv]and[f=mp4]specify the container format (muxer) for each respective destination.
Handling Network Drops (Important)
By default, if the RTMP stream connection drops, the entire FFmpeg
process will crash, which will stop your local recording. To prevent
this, use the onfail=ignore option for the RTMP output.
This ensures that the local recording continues even if the internet
connection interrupts the stream.
Use this modified command:
ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f tee -map 0:v -map 0:a "[f=flv:onfail=ignore]rtmp://your-rtmp-server/live/stream_key|[f=mp4]local_record.mp4"Escaping Special Characters
Depending on your operating system’s terminal, you may need to escape
special characters like the pipe (|) or brackets
([ and ]).
- Linux/macOS (Bash/Zsh): Use double quotes around the entire output string as shown in the examples above.
- Windows (Command Prompt): Double quotes are generally sufficient.
- Windows (PowerShell): You must wrap the argument in
single quotes or escape the brackets:
'[f=flv]rtmp://...|[f=mp4]local_record.mp4'.