How to Stream to Multiple UDP Targets with FFmpeg Tee
Streaming a single media source to multiple destinations
simultaneously is a common requirement in live broadcasting. This
article provides a straightforward guide on how to configure and execute
an FFmpeg command using the tee muxer to duplicate an input
stream and send it to multiple UDP targets efficiently, minimizing CPU
and network overhead by encoding the stream only once.
The FFmpeg Tee Command
To stream to multiple UDP targets, you use FFmpeg’s tee
muxer. This muxer duplicates the output packets and sends them to the
specified destinations.
Here is the standard command structure:
ffmpeg -re -i input.mp4 -c:v libx264 -c:a aac -f tee "[f=mpegts]udp://192.168.1.50:5000|[f=mpegts]udp://192.168.1.60:6000"Command Breakdown
-re: Instructs FFmpeg to read the input at the native frame rate. This is required when streaming a file to simulate a live feed. If your input is a live capture card or IP camera, you can omit this option.-i input.mp4: Specifies your input source (this can be a file, an RTSP stream, or a capture device).-c:v libx264 -c:a aac: Encodes the video to H.264 and the audio to AAC. If your input is already encoded in the desired format, you can use-c copyto bypass encoding entirely and save CPU resources.-f tee: Selects theteemuxer."[f=mpegts]udp://...|[f=mpegts]udp://...": This is the payload containing your outputs, enclosed in double quotes:|(Pipe character): Acts as the delimiter separating each target destination.[f=mpegts]: This option is prefixed to each UDP target to force the container format to MPEG-TS, which is the standard transport stream format for UDP streaming.
Streaming with Stream Copy (No Re-encoding)
If your input file or stream is already in a compatible format (like
H.264 and AAC) and does not need transcoding, use the copy
codec. This drastically reduces CPU usage:
ffmpeg -re -i input.mp4 -c copy -f tee "[f=mpegts]udp://239.0.0.1:1234|[f=mpegts]udp://239.0.0.2:1234"Important Syntax Considerations
- Quoting: Always wrap the entire
teeoutput string in double quotes. This prevents your command-line shell from interpreting the pipe character (|) as a shell command. - Escaping: If your stream options contain special characters (like commas or colons), you may need to escape them using backslashes to ensure FFmpeg parses the arguments correctly.