Configure libvpx-vp9 GOP Size in FFmpeg

This article explains how to configure the Group of Pictures (GOP) size, also known as the keyframe interval, for the VP9 video encoder (libvpx-vp9) in FFmpeg. You will learn how to use command-line arguments to set both variable and fixed GOP sizes to optimize your video for standard playback or adaptive bitrate streaming.

Understanding GOP Size in FFmpeg

The GOP size determines the distance between keyframes (I-frames). In FFmpeg, the GOP size for the libvpx-vp9 encoder is primarily controlled by the -g flag, which defines the maximum interval between keyframes.

By default, FFmpeg and libvpx-vp9 use a variable GOP size. The encoder inserts keyframes automatically at scene cuts to maintain visual quality, up to the limit set by the -g flag.

Setting a Maximum GOP Size (Default/Variable GOP)

To set the maximum keyframe interval, use the -g option followed by the number of frames. For example, if your video is 30 frames per second (fps) and you want a maximum GOP size of 10 seconds, you would set the limit to 300 frames.

ffmpeg -i input.mp4 -c:v libvpx-vp9 -g 300 output.webm

Configuring a Strict/Constant GOP Size

For adaptive bitrate streaming formats like DASH or HLS, you need a strict, fixed GOP size. This ensures that keyframes align perfectly across different quality renditions.

To force a constant GOP size, you must do three things: 1. Set the maximum keyframe interval (-g). 2. Set the minimum keyframe interval (-keyint_min) to the same value. 3. Disable scene change detection (-sc_threshold 0) to prevent the encoder from inserting extra keyframes at scene cuts.

Here is an example of forcing a strict 2-second GOP size for a 24 fps video (48 frames):

ffmpeg -i input.mp4 -c:v libvpx-vp9 -g 48 -keyint_min 48 -sc_threshold 0 output.webm

For HTTP streaming (DASH/HLS), a 2-second or 5-second GOP is standard. Below are the configurations for a 2-second constant GOP at common framerates: