How to Stream RTP Multiple Multicast Groups with FFmpeg

Streaming media to multiple multicast groups simultaneously allows you to efficiently distribute live audio or video to different network segments or client groups. This article explains how to configure FFmpeg to broadcast a single input stream to multiple RTP (Real-time Transport Protocol) multicast addresses using both the basic multi-output method and the highly efficient tee muxer.

The tee muxer is the most efficient way to stream to multiple multicast groups because it encodes the input stream only once and then duplicates the encoded packets to multiple destinations. This drastically reduces CPU usage.

Run the following command to stream a video file to two different multicast groups:

ffmpeg -re -i input.mp4 -c:v libx264 -preset veryfast -c:a aac -f tee "[f=rtp]rtp://224.1.1.1:5004|[f=rtp]rtp://224.1.1.2:5004"

Command breakdown: * -re: Forces FFmpeg to read the input in real-time (essential for live streaming). * -i input.mp4: Specifies the source file or live camera input. * -c:v libx264 -c:a aac: Encodes the video to H.264 and audio to AAC. * -f tee: Selects the tee muxer. * "[f=rtp]rtp://...|[f=rtp]rtp://...": Defines the output destinations separated by the pipe (|) character. The [f=rtp] prefix specifies that each destination should use the RTP format.

Method 2: Dual-Output Encoding (Independent Streams)

If you need to stream different quality levels (transcoding) to different multicast groups, you can define multiple outputs directly in the command line. This method uses more CPU because it processes encoding for each output separately.

ffmpeg -re -i input.mp4 \
  -c:v libx264 -b:v 2000k -f rtp rtp://224.1.1.1:5004 \
  -c:v libx264 -b:v 1000k -f rtp rtp://224.1.1.2:5006

In this example, the first multicast group (224.1.1.1:5004) receives a 2000k bitrate stream, while the second group (224.1.1.2:5006) receives a 1000k bitrate stream.

Managing SDP Files

RTP streams require a Session Description Protocol (SDP) file for receivers to decode the stream properly. When streaming to multiple destinations, you can output the SDP information to a file by appending the -sdp_file option:

ffmpeg -re -i input.mp4 -c:v libx264 -an -f rtp -sdp_file multicast_1.sdp rtp://224.1.1.1:5004

For multiple streams, you will need to generate SDP files for each multicast group so the receiving players (like VLC) can join and decode the respective channels.