How to Set FFmpeg UDP TTL and Buffer Size
This article provides a straightforward guide on how to configure the Time-to-Live (TTL) and socket buffer size parameters for UDP streaming in FFmpeg. You will learn the exact syntax and options required to control network hops, prevent packet loss, and optimize the stability of your live UDP streams.
Configuring UDP Parameters in FFmpeg
In FFmpeg, network protocols like UDP are configured by appending
query parameters directly to the stream’s destination or source URL. The
syntax follows a standard query string format:
udp://[address]:[port]?option1=value1&option2=value2.
1. Setting the Time-to-Live (TTL)
The TTL parameter determines the maximum number of network hops a packet can take before being discarded. This is especially important in multicast streaming to prevent packets from circulating indefinitely or traveling outside the intended network segment.
To set the TTL, use the ttl option:
ttl=integer_valueFor example, to set the TTL to 5:
udp://239.0.0.1:1234?ttl=52. Setting the Buffer Size
The buffer size parameter specifies the size of the operating system’s socket send or receive buffer in bytes. If your stream has a high bitrate, the default system buffer may overflow, causing packet drops and video corruption. Increasing the buffer size prevents this.
To set the socket buffer size, use the buffer_size
option (values are in bytes):
buffer_size=integer_valueFor example, to set a 10 megabyte (10,000,000 bytes) buffer:
udp://239.0.0.1:1234?buffer_size=10000000Note: On Linux and macOS, the operating system imposes a maximum
limit on socket buffers. If you request a buffer larger than the system
limit (defined by sysctl net.core.rmem_max or
wmem_max), FFmpeg will only allocate up to the OS-allowed
maximum.
Complete Example Command
To stream an input file via UDP multicast, setting the TTL to 10 and the buffer size to 5MB, use the following FFmpeg command:
ffmpeg -re -i input.mp4 -c copy -f mpegts "udp://239.0.0.1:1234?ttl=10&buffer_size=5000000"In this command: * -re reads the input file in
real-time. * -c copy copies the streams without re-encoding
to save CPU. * -f mpegts muxes the output into the MPEG
transport stream format, which is standard for UDP streaming. * The URL
parameters are separated by an ampersand (&) and
enclosed in quotes to prevent the terminal shell from misinterpreting
the special character.