What Does the -g Option Do in FFmpeg?
In FFmpeg, the -g option is a crucial parameter used to
define the Group of Pictures (GOP) size, commonly referred to as the
keyframe or I-frame interval. This article explains how the
-g option works, its impact on video compression, playback,
and streaming, and how to use it effectively in your video processing
workflows.
Understanding the GOP and Keyframes
To understand the -g option, it helps to understand how
video compression works. Videos are made up of three types of frames: *
I-frames (Keyframes): Full images containing complete
visual data. They do not rely on other frames to decode. *
P-frames and B-frames: Delta frames that only store the
changes between frames to save space.
The -g option specifies the maximum distance between
I-frames. For example, if you set -g 60, FFmpeg will place
an I-frame at least once every 60 frames.
How to Use the -g Option
The basic syntax for applying the -g option in an FFmpeg
command is as follows:
ffmpeg -i input.mp4 -c:v libx264 -g 60 output.mp4In this example, if the output video has a framerate of 30 frames per
second (fps), a GOP size of 60 means a keyframe will be
inserted every 2 seconds (60 frames / 30 fps = 2 seconds).
Why the -g Option Matters
Adjusting the GOP size has a direct impact on video quality, file size, and playback performance.
1. Video Streaming (HLS and DASH)
For modern streaming protocols like HTTP Live Streaming (HLS) or
Dynamic Adaptive Streaming over HTTP (DASH), consistent keyframe
intervals are critical. Streaming players segment videos into short
chunks (usually 2 to 6 seconds long). Every chunk must begin with a
keyframe so the player can start playback instantly. If you are
streaming a 30 fps video and want 2-second segments, you should set
-g 60.
2. Compression and File Size
- Larger GOP (e.g.,
-g 240): Fewer keyframes are created. Because P and B-frames require much less data than I-frames, a larger GOP size generally results in better compression and smaller file sizes for highly compressible content. - Smaller GOP (e.g.,
-g 15): More keyframes are created. This results in larger file sizes but provides better overall picture quality in high-motion scenes.
3. Seeking and Editing
During video playback, when a user drags the progress bar (seeks) to a new timestamp, the player must locate the nearest preceding keyframe to render the image. A smaller GOP size allows for faster, more precise seeking and makes the video easier to cut and edit without re-encoding.