FFmpeg Cut Video at Keyframe Automatically
This article explains how to write an FFmpeg command that automatically cuts a video at the nearest keyframe. By using input seeking combined with stream copying, you can perform lightning-fast video cuts that snap directly to keyframes without the need for time-consuming re-encoding.
The Keyframe Cutting Command
To cut a video at the nearest keyframe without re-encoding, use the following FFmpeg command structure:
ffmpeg -ss 00:01:30 -i input.mp4 -t 00:00:45 -c copy output.mp4How It Works
-ss 00:01:30(Input Seeking): Placing the start time parameter before the input file (-i) is crucial. This tells FFmpeg to use “input seeking.” FFmpeg will search the input file and automatically snap to the nearest keyframe (I-frame) just before or at the specified timestamp (1 minute and 30 seconds).-i input.mp4: Specifies your source video file.-t 00:00:45: Specifies the duration of the cut (45 seconds in this example). Because input seeking resets the video timeline to zero at the seek point, using-tis the most reliable way to define how long the cut should be.-c copy: This instructs FFmpeg to copy the video and audio streams directly without re-encoding them. This preservation of the original streams is what makes the process nearly instantaneous and lossless.output.mp4: The name of the exported video file.
Why Keyframes Matter
Video files are compressed using “keyframes” (which contain complete image data) and “interframes” (which only contain the differences between frames).
If you attempt to cut a video at a non-keyframe while copying the
stream (-c copy), the player will not have a full image to
render at the very beginning of the clip. This results in a frozen
screen, black frames, or corrupted video artifacts until the player
reaches the next keyframe.
By placing the -ss flag before -i, FFmpeg
automatically seeks to the closest preceding keyframe, ensuring the
output video starts with a clean, fully-rendered frame.
Keyframe Cutting vs. Accurate Cutting
Because this method snaps to the nearest existing keyframe, the cut may not happen at the exact millisecond you specified.
- For fast, lossless cuts: Use the command above. It takes seconds to process because it does not re-encode the video.
- For frame-accurate cuts: If you need to cut at an
exact frame that is not a keyframe, you must re-encode the video. This
takes longer but allows for pixel-perfect precision. To do this, replace
-c copywith a video encoder, like so:
ffmpeg -i input.mp4 -ss 00:01:30 -t 00:00:45 -c:v libx264 -c:a aac output.mp4