Configure FFmpeg MPEG-TS Buffer and Delay Settings

This article explains how to configure the maximum delay and buffer size in FFmpeg’s MPEG-TS (MPEG Transport Stream) muxer to prevent player buffer starvation. By correctly setting the muxing delay, constant bitrate constraints, and Video Buffer Verifier (VBV) parameters, you can ensure a continuous, stutter-free data stream to your playback devices.

Understanding MPEG-TS Buffer Starvation

Player buffer starvation occurs when a media player’s input buffer runs out of packets to decode, leading to playback freezes or buffering loops. In MPEG-TS streaming, this is often caused by variable bitrate spikes, inconsistent muxing delays, or a lack of null packet padding.

To prevent this, you must configure three key elements in your FFmpeg command: 1. The maximum muxing delay (-max_delay) 2. The overall stream muxrate (-muxrate) 3. The codec-level VBV buffer size (-bufsize and -maxrate)


Step 1: Configure Maximum Muxing Delay (-max_delay)

The -max_delay option is a global FFmpeg parameter that defines the maximum interval (in microseconds) that the muxer can delay packets to buffer and sort them before writing them to the output stream.

For real-time streaming, keeping this low prevents latency, but setting it too low can cause packet drops. For reliable playback without starvation, a value between 500 milliseconds and 2 seconds is standard.


Step 2: Set a Constant Muxrate (-muxrate)

MPEG-TS players expect a steady stream of data. If your video content becomes static (e.g., a talking head), the encoded bitrate drops drastically, which can cause the player’s buffer to empty.

By setting the -muxrate option, you force the MPEG-TS muxer to output a Constant Bitrate (CBR) stream. If the actual video and audio bitrate falls below this limit, FFmpeg automatically inserts null packets (PID 0x1FFF) to pad the stream and maintain the constant flow.

Note: Ensure your -muxrate is set roughly 10% to 15% higher than the combined target bitrates of your video and audio tracks to accommodate container overhead.


Step 3: Configure VBV Buffer Settings at the Codec Level

To prevent the video stream itself from exceeding the player’s buffer limits, you must enforce the Video Buffer Verifier (VBV) model during encoding. This is done using the -maxrate and -bufsize flags on your video encoder (e.g., libx264).


Complete FFmpeg Command Example

Below is a complete command combining these settings to encode an input file into a compliant MPEG-TS stream with a controlled buffer and fixed muxrate:

ffmpeg -i input.mp4 \
  -c:v libx264 -b:v 4000k -maxrate:v 4000k -bufsize:v 8000k \
  -c:a aac -b:a 128k \
  -f mpegts \
  -muxrate 4500k \
  -max_delay 500000 \
  output.ts

In this command: * The video is capped at 4000 kbps with an 8000 kb buffer. * The audio is set to 128 kbps. * The total TS muxrate is locked at 4500 kbps (leaving ~372 kbps of padding and overhead). * The maximum muxing delay is restricted to 500 milliseconds (500000 microseconds).