Configure TTL and Packet Size for UDP in FFmpeg
Configuring the Time-to-Live (TTL) and packet size in FFmpeg is essential for optimizing UDP network broadcasts, particularly when streaming multicast video across local subnets or managing network fragmentation. This guide explains how to use FFmpeg's built-in UDP protocol parameters to set the TTL value to control network hop limits and adjust the maximum packet size to fit standard Ethernet Maximum Transmission Unit (MTU) constraints.
UDP URL Options in FFmpeg
FFmpeg configures UDP socket behaviors directly inside the output URL using query parameters. The two critical parameters for controlling network routing and payload boundaries are:
ttl: Specifies the Time-to-Live value (integer between 0 and 255). This defines how many router hops a packet can traverse before being dropped. By default, UDP multicast often defaults to a TTL of 1 (restricted to the local subnet).pkt_size: Sets the maximum UDP payload size in bytes. This determines how much data FFmpeg bundles into a single UDP datagram before transmission.
Basic Syntax
To apply these parameters, append them to your destination UDP address using standard URL query format:
udp://<destination_ip>:<port>?ttl=<value>&pkt_size=<value>
Complete Command Example
The following command takes an input video file, encodes it to H.264 video and AAC audio, encapsulates it into an MPEG-TS container, and broadcasts it over UDP multicast with a TTL of 16 and a packet size of 1316 bytes:
ffmpeg -re -i input.mp4 -c:v libx264 -preset veryfast -b:v 3000k \
-c:a aac -b:a 128k -f mpegts \
"udp://239.255.0.1:1234?ttl=16&pkt_size=1316"Recommended Configurations
Setting the TTL
- TTL = 1: Keeps the multicast traffic contained strictly within the local physical subnet. Use this to prevent multicast streams from leaking into your broader corporate or campus network.
- TTL > 1 (e.g., 5 to 64): Allows the multicast stream to traverse routers configured for Protocol Independent Multicast (PIM). Set this value according to the expected hop count between your streaming source and the farthest client subnet.
Setting the Packet Size
(pkt_size)
- Standard Recommendation
(
pkt_size=1316): For MPEG-TS streams,1316is the standard production value. MPEG-TS packets are 188 bytes each. Seven MPEG-TS packets equal exactly 1,316 bytes (\(7 \times 188\)). When combined with the 20-byte IP header and the 8-byte UDP header, the total datagram size is 1,344 bytes. This safely stays below the standard Ethernet MTU limit of 1,500 bytes, completely avoiding IP packet fragmentation. - Jumbo Frames (
pkt_size=8960or similar): If your local network explicitly supports Jumbo Frames (9000-byte MTU) across all intermediate switches and network interfaces, you can increasepkt_sizeto reduce per-packet overhead, though 1316 remains the most universally compatible setting for receiving decoders.