How to Use FFmpeg keyint_min for I-Frame Control
In digital video encoding, controlling the placement of keyframes
(I-frames) is crucial for optimizing video quality, file size, and
streaming compatibility. This article provides a straightforward guide
on how to use the -keyint_min option in FFmpeg to set the
minimum interval between I-frames. You will learn the definition of this
parameter, how it interacts with the maximum GOP (Group of Pictures)
size, and practical command-line examples to optimize your video
compression workflow.
Understanding -keyint_min and I-Frames
An I-frame (Intra-coded picture) is a fully self-contained video frame that does not require information from other frames to decode. Because I-frames require the most data, placing them too frequently wastes bandwidth, while placing them too far apart makes seeking difficult and reduces stream reliability.
The -keyint_min option in FFmpeg sets the
minimum distance (in frames) before the encoder can
insert another I-frame.
When video encoders (like libx264 or
libx265) detect a drastic scene change, they naturally want
to insert a new I-frame. The -keyint_min parameter prevents
the encoder from placing these keyframes too close together, which
prevents rapid spikes in bitrate during high-motion sequences.
The Relationship Between -g and -keyint_min
To control keyframes effectively, you must use
-keyint_min in conjunction with the -g (GOP
size) option:
-g(or-keyint): Defines the maximum interval between keyframes. If no scene cuts occur, the encoder will force an I-frame at this limit.-keyint_min: Defines the minimum interval between keyframes.
Scenario 1: Allowing Dynamic Keyframe Placement
If you want to allow the encoder to place keyframes at natural scene
cuts, but not too close together, set a lower
-keyint_min.
For a 30 fps video, you might want a maximum GOP of 250 frames (approx. 8 seconds) and a minimum GOP of 25 frames (approx. 1 second):
ffmpeg -i input.mp4 -c:v libx264 -g 250 -keyint_min 25 output.mp4In this setup, FFmpeg will insert an I-frame at least every 250 frames. If a scene change occurs, it can insert one sooner, but never closer than 25 frames from the previous I-frame.
Scenario 2: Enforcing a Strict/Fixed GOP (Adaptive Streaming)
For modern streaming protocols like HLS or DASH, chunks of video must be cut at identical intervals. To achieve this, you must force a fixed GOP size where keyframes occur at exact, predictable intervals.
To disable dynamic scene-cut keyframe placement and enforce a strict
interval, set -g and -keyint_min to the exact
same value, and set the scene-cut threshold (-sc_threshold)
to 0.
For a 30 fps video requiring a keyframe exactly every 2 seconds (60 frames):
ffmpeg -i input.mp4 -c:v libx264 -g 60 -keyint_min 60 -sc_threshold 0 output.mp4-g 60forces a keyframe every 60 frames.-keyint_min 60prevents any keyframes from occurring before 60 frames have passed.-sc_threshold 0disables the encoder’s automatic scene-cut detection.
Recommended Settings for Common Framerates
If you are aiming for a standard 2-second keyframe interval (ideal for most streaming platforms), use the following configurations based on your video’s frame rate:
| Framerate (FPS) | Max GOP (-g) |
Min GOP (-keyint_min) |
|---|---|---|
| 24 | 48 | 48 |
| 25 | 50 | 50 |
| 30 | 60 | 60 |
| 60 | 120 | 120 |