Sub topic 7
Why Keyframes and Re-encoding Affect Cut Accuracy
Prerequisites
Assume you understand a timeline editor, clip start and end points, and basic audio/video synchronization. You should also know that an MP4 file is a container that can hold compressed video and audio streams. The examples use ffmpeg, but the same principles apply in a graphical editor.
How compressed video is stored
Most lecture videos use inter-frame compression. The video does not store every frame as a complete image.
- An I-frame, or keyframe, is a complete image.
- A P-frame stores changes from an earlier frame.
- A B-frame stores changes using frames before and after it.
A group of pictures, or GOP, starts with a keyframe and contains dependent frames until the next keyframe.
This structure saves space, but it affects seeking. To decode a frame in the middle of a GOP, a decoder usually starts at the preceding keyframe and reconstructs the following frames.
Why cuts may move
A video editor can display a precise frame on its timeline, but the compressed file may not support a precise cut without extra work. If you cut at a non-keyframe and copy the compressed stream directly, the output may begin at the nearest keyframe instead. The result can include unwanted frames before the intended cut, show a brief decoding error, or lose audio/video alignment.
The same issue appears when using ffmpeg with stream copying:
ffmpeg -ss 72 -to 80 -i lecture.mp4 -c copy unwanted_segment.mp4The -c copy option avoids re-encoding. It is fast and preserves the original quality, but the cut is usually limited by keyframe placement and container behavior. It is useful for rough extraction, not for frame-accurate editing.
Re-encoding for accurate cuts
Re-encoding decodes the source and creates a new stream. The encoder can begin a new GOP at the exact edit point, so the output can start and end on the selected frames.
ffmpeg -i lecture.mp4 \
-vf "select='not(between(t,72,80))',setpts=N/FRAME_RATE/TB" \
-af "aselect='not(between(t,72,80))',asetpts=N/SR/TB" \
-c:v libx264 -c:a aac lecture_cut.mp4The filters remove the same time range from both streams and regenerate timestamps. Re-encoding takes longer, increases file-generation cost, and can reduce quality if the bitrate or codec settings are poor. It can also introduce a generation loss when you repeatedly export the same material.
Choosing a method
| Goal | Recommended method | Main trade-off |
|---|---|---|
| Fast rough segment | Stream copy | Keyframe-limited accuracy |
| Frame-accurate cut | Decode and re-encode | More time and possible quality loss |
| Several edits and effects | One complete re-encode | Longer final export |
Check the exported file near every cut. Look for an extra frame, a frozen image, a click, or a sudden audio shift. If any occurs, inspect timestamps and re-encode the affected workflow rather than relying on stream copying.
Exercise
Why can a stream-copy cut begin earlier than the selected timestamp?
Show answer
The output may begin at the preceding keyframe because dependent frames cannot be decoded independently.