How to Set RTMP Buffer Size in FFmpeg
Configuring the real-time buffer size for RTMP ingestion in FFmpeg is critical for maintaining stream stability, preventing dropped frames, and balancing latency. This guide explains how to use FFmpeg's native input options to adjust RTMP client buffer times and underlying network socket buffer sizes to optimize live stream ingest performance.
The Primary RTMP Buffer Option
FFmpeg provides a dedicated protocol option for RTMP inputs to control the client-side buffer duration:
- Option:
-rtmp_buffer - Unit: Milliseconds (ms)
- Default:
3000(3 seconds)
Because this is an input option, it must be placed
before the -i input flag in your
command:
ffmpeg -rtmp_buffer 2000 -i rtmp://example.com/live/stream_key -c copy output.mp4In this example, the ingest buffer is reduced to 2,000 milliseconds (2 seconds) to reduce latency while still maintaining a brief safety cushion against network jitter.
Low Latency vs. High Stability
The optimal -rtmp_buffer value depends on your specific
streaming requirements:
Ultra-Low Latency: Set the buffer to a very low value (e.g.,
100to500ms). This prevents FFmpeg from accumulating a delay, but requires a very reliable network connection to avoid stuttering or dropped packets.ffmpeg -rtmp_buffer 200 -i rtmp://example.com/live/stream_key ...High Stability (Lossy or Fluctuating Networks): If the input stream suffers from intermittent packet drops or bandwidth fluctuations, increase the buffer to
5000ms (5 seconds) or higher to allow adequate time for data recovery.ffmpeg -rtmp_buffer 5000 -i rtmp://example.com/live/stream_key ...
Adjusting the Network Socket Receive Buffer
In high-bitrate environments, the OS-level socket buffer can overflow
before FFmpeg processes the incoming packets. You can expand the
underlying TCP receive buffer using the -recv_buffer_size
flag:
- Option:
-recv_buffer_size - Unit: Bytes
ffmpeg -rtmp_buffer 3000 -recv_buffer_size 1048576 -i rtmp://example.com/live/stream_key -c copy output.flvThis command sets a 1 MB (1,048,576 bytes) TCP receive buffer in addition to the standard 3,000 ms RTMP client buffer, reducing the risk of OS-level packet drops during temporary CPU spikes.
Controlling Queue Limits with FIFO Buffers
If FFmpeg warns that input queues are full during ingestion, you can
configure the internal packet buffer size using the
-fifo_size parameter to prevent packet loss during
processing bottlenecks:
ffmpeg -rtmp_buffer 2000 -fifo_size 50000 -i rtmp://example.com/live/stream_key -c copy output.flvThis ensures FFmpeg allocates memory for up to 50,000 packets internally before discarding data, allowing the ingest pipeline to handle brief processing delays smoothly.