Frame-Accurate Video Cutting with FFmpeg
Cutting videos with frame-level precision in FFmpeg requires understanding the difference between fast seeking (input seeking) and accurate seeking (output seeking). This guide explains how to combine seeking parameters and re-encoding to split or cut videos at exact frames, avoiding frozen frames, black screens, or audio desync at the cut points.
Understanding the Keyframe Limitation
To cut a video with frame accuracy, you must understand how video compression works. Videos are made of keyframes (I-frames), which contain a complete image, and inter-frames (P-frames and B-frames), which only store the differences between frames.
If you cut a video using the stream copy mode (-c copy),
FFmpeg can only cut at the nearest keyframe. If your specified cut time
falls on a P-frame or B-frame, the video will start with a frozen or
black screen until the next keyframe is reached.
To achieve true frame-accuracy, you must re-encode the video. Re-encoding allows FFmpeg to generate a new keyframe at the exact timestamp you specify.
Method 1: Accurate Seeking with Re-encoding (Recommended)
This is the most reliable method for cutting a video at an exact
frame. By placing the seek parameter (-ss) before the
input, FFmpeg quickly seeks to the closest keyframe before your target
time, and then decodes the remaining frames up to your start point. By
re-encoding, it creates a clean new keyframe at the cut.
The Command:
ffmpeg -ss 00:01:30 -i input.mp4 -to 00:02:15 -c:v libx264 -c:a aac output.mp4Parameter Breakdown:
-ss 00:01:30: The start time of the cut. Placing this before-ienables fast, frame-accurate seeking because FFmpeg will decode the video from the nearest prior keyframe.-i input.mp4: The input video file.-to 00:02:15: The end time of the cut. Alternatively, you can use-t 45to specify a duration of 45 seconds.-c:v libx264 -c:a aac: Re-encodes the video to H.264 and audio to AAC, which forces the creation of a new keyframe at the exact start point.
Method 2: Fast seeking with “Smart Cut” (Experimental)
If you have a very long video and do not want to re-encode the entire output, you can use a hybrid approach. This seeks quickly to the nearest keyframe, copies the majority of the video stream, but re-encodes only the frames at the cut point.
While FFmpeg does not have a native “smart cut” flag, you can achieve a similar result by splitting the process: 1. Re-encode only the small segment from your cut point to the next keyframe. 2. Copy the middle section without re-encoding. 3. Concatenate the segments back together.
For most users, Method 1 is highly recommended because modern CPUs can re-encode short video clips quickly, ensuring 100% compatibility and eliminating audio/video sync issues.
Key Best Practices
- Always put
-ssbefore-i: When placed after the input, FFmpeg decodes every frame from the beginning of the video until it reaches the start time, which is incredibly slow for large files. - Use
-tofor timestamps,-tfor duration: If you seek to 10 seconds (-ss 10) and want a 5-second clip, use-t 5. If you want to stop at the 15-second mark of the original video, use-to 15.