Configure GOP Size in FFmpeg libx265
This guide explains how to configure the Group of Pictures (GOP)
size, also known as the keyframe interval, when encoding video using the
libx265 (H.265/HEVC) encoder in FFmpeg. You will learn the
specific command-line arguments needed to set both maximum and minimum
GOP boundaries, as well as how to force a fixed GOP size for adaptive
streaming formats like HLS and DASH.
Setting GOP Size Using Standard FFmpeg Flags
The easiest way to set the GOP size in FFmpeg is by using the generic video encoding flags. The maximum GOP size determines how often a keyframe (I-frame) is forced into the video stream.
To set the maximum GOP size, use the -g flag:
ffmpeg -i input.mp4 -c:v libx265 -g 120 output.mp4In this example, -g 120 sets the maximum interval
between keyframes to 120 frames. If your source video is 30 frames per
second (fps), this creates a keyframe at least once every 4 seconds.
To set the minimum GOP size, which prevents the encoder from placing
keyframes too close together during high-motion scenes, use the
-keyint_min flag:
ffmpeg -i input.mp4 -c:v libx265 -g 120 -keyint_min 24 output.mp4Setting GOP Size Using x265-Params
For more precise control, you can pass parameters directly to the
libx265 library using the -x265-params flag.
The library-specific options for GOP boundaries are keyint
(maximum) and min-keyint (minimum).
ffmpeg -i input.mp4 -c:v libx265 -x265-params keyint=120:min-keyint=24 output.mp4Using -x265-params is often preferred when you are
combining multiple encoder-specific settings into a single, clean
argument.
How to Force a Fixed GOP Size
By default, libx265 uses scene-cut detection to
dynamically insert keyframes at scene changes, even if it falls below
the maximum GOP limit. While this improves compression efficiency, HTTP
Live Streaming (HLS) and Dynamic Adaptive Streaming over HTTP (DASH)
require a strictly fixed GOP size (no dynamic keyframes) so segment
splits occur at exact intervals.
To force a fixed GOP size, you must disable scene-cut detection by
setting scenecut=0 and matching the minimum and maximum
keyframe intervals:
ffmpeg -i input.mp4 -c:v libx265 -x265-params keyint=60:min-keyint=60:scenecut=0 output.mp4In this command: * keyint=60 forces a keyframe every 60
frames. * min-keyint=60 prevents the encoder from creating
keyframes earlier. * scenecut=0 disables the detection of
scene changes, ensuring keyframes are placed only at the
specified 60-frame interval.