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

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 ]).