Configure RTMP Buffer Size in FFmpeg
When streaming live video using RTMP, configuring the real-time buffer size in FFmpeg is crucial for preventing packet loss, reducing latency, and ensuring smooth ingestion. This article explains how to adjust the buffer size using specific FFmpeg parameters, target both the RTMP protocol buffer and real-time capture device buffers, and optimize your live stream’s stability.
The RTMP Protocol Buffer Option
FFmpeg allows you to configure the client-side buffer time directly
for the RTMP stream. This is controlled via the
-rtmp_buffer option, which specifies the buffer size in
milliseconds.
To set the RTMP ingestion buffer, place the parameter before your input URL:
ffmpeg -rtmp_buffer 2000 -i rtmp://your-server-ip/live/stream -c copy output.mp4In this example, -rtmp_buffer 2000 sets a 2-second (2000
milliseconds) buffer. Increasing this value helps smooth out playback
over unstable network connections, while decreasing it reduces overall
latency.
The Real-Time Input
Buffer (-rtbufsize)
If you are ingesting a live feed from a local capture device (such as a webcam, capture card, or desktop grabber) and streaming it via RTMP, you must configure the real-time input buffer. If this buffer is too small, FFmpeg will drop frames, resulting in the “real-time buffer [name] too full” warning.
Use the -rtbufsize option to allocate more memory for
buffering raw incoming frames before they are encoded and sent over
RTMP:
ffmpeg -f dshow -rtbufsize 150M -i video="Integrated Camera" -c:v libx264 -f flv rtmp://your-server-ip/live/stream-rtbufsize 150M: Allocates 150 Megabytes of system memory as a temporary buffer for the input device. You can adjust this value (e.g.,50M,200M,1G) depending on your available RAM and stream resolution.
Increasing the Thread Queue Size
During high-bitrate RTMP ingestion, the demuxer queue can overflow if
the CPU cannot process the packets fast enough. To prevent the “Thread
message queue blocking” warning, increase the input thread queue size
using -thread_queue_size:
ffmpeg -thread_queue_size 1024 -f dshow -rtbufsize 100M -i video="Integrated Camera" -c:v libx264 -f flv rtmp://your-server-ip/live/stream-thread_queue_size 1024: Increases the maximum number of queued packets from the default value (usually 8) to 1024, giving FFmpeg a larger safety margin during CPU spikes.
TCP Socket Buffer Adjustment
Because RTMP runs over TCP, you can also instruct FFmpeg to increase
the underlying operating system TCP receive buffer. This is done by
appending the rcvbuf parameter directly to the RTMP
URL:
ffmpeg -i rtmp://your-server-ip/live/stream?rcvbuf=262144 -c copy output.mp4rcvbuf=262144: Sets the TCP socket receive buffer to 256 KB (262,144 bytes), which helps sustain high-bitrate streams over WAN connections with high latency.