Configure libx264 Scene Cut Detection in FFmpeg

This article explains how to configure and adjust the scene cut detection threshold in FFmpeg when encoding video with the libx264 library. You will learn about the parameters that control keyframe placement at scene transitions, how these settings affect compression efficiency and playback seekability, and how to apply them using practical command-line examples.

Understanding Scene Cut Detection in libx264

When encoding video, libx264 attempts to detect scene changes to insert an I-frame (keyframe) at the beginning of a new scene. This ensures high visual quality at transitions and improves seeking performance. The behavior of this detection is controlled primarily by the scene cut threshold.

A higher threshold makes the encoder more sensitive to changes, resulting in more frequent keyframes at scene transitions. A lower threshold makes it less sensitive, while setting the threshold to 0 disables scene cut detection entirely, forcing the encoder to rely strictly on a fixed Group of Pictures (GOP) interval.

Setting the Threshold with -sc_threshold

The standard way to configure this in FFmpeg is using the -sc_threshold option.

In libx264, the default threshold value is 40.

Example: Adjusting the Threshold

To increase sensitivity and force more frequent keyframe placement on subtle scene changes, you can raise the threshold:

ffmpeg -i input.mp4 -c:v libx264 -sc_threshold 60 output.mp4

To decrease sensitivity, lower the value:

ffmpeg -i input.mp4 -c:v libx264 -sc_threshold 25 output.mp4

Example: Disabling Scene Cut Detection

For scenarios like HTTP Live Streaming (HLS) or Dynamic Adaptive Streaming over HTTP (DASH), you often need a strictly fixed keyframe interval (GOP size) without extra keyframes inserted at scene cuts. You can disable scene cut detection by setting the threshold to 0:

ffmpeg -i input.mp4 -c:v libx264 -g 60 -keyint_min 60 -sc_threshold 0 output.mp4

Note: In this command, -g 60 and -keyint_min 60 force a keyframe exactly every 60 frames.

Setting the Threshold with -x264-params

Alternatively, you can pass the parameter directly to the underlying libx264 library using the -x264-params flag with the scenecut option. This achieves the same result as -sc_threshold.

ffmpeg -i input.mp4 -c:v libx264 -x264-params scenecut=40 output.mp4

To disable it using this method:

ffmpeg -i input.mp4 -c:v libx264 -g 60 -keyint_min 60 -x264-params scenecut=0 output.mp4