Sub topic 7
Why keyframes and re-encoding affect cut accuracy
This chapter explains why a cut made with a "fast" command can land a second or two away from where you asked, and how to choose between fast cuts and exact cuts. It assumes you can run ffmpeg from a terminal, know what an MP4 file is, and have already cut a segment out of a lecture, as in the previous chapters.
The working model: three kinds of frames
Modern video codecs such as H.264 do not store every frame as a full picture. That would make files huge. They store a few complete pictures and describe the rest as changes from other frames.
- An I-frame (intra frame) is a complete picture. It can be decoded on its own.
- A P-frame (predicted frame) stores only the difference from earlier frames.
- A B-frame (bidirectional frame) stores differences from both earlier and later frames.
A keyframe is an I-frame that a player can start decoding from. Frames between two keyframes form a group of pictures (GOP). To show any P- or B-frame, the decoder needs the frames it depends on, which leads back to the keyframe at the start of its GOP.
A screen recording of a lecture often has long stretches where little changes, so encoders may place keyframes far apart, commonly every 2 to 10 seconds. The gap is set by the encoder's keyframe interval, such as the -g option in ffmpeg or the "keyframe interval" setting in a screen recorder.
Why this affects cuts
A cut that starts at a keyframe is easy. The software copies the compressed data as it is. A cut that starts between keyframes is hard. The first frame of your new clip is a P-frame, and it depends on frames you want to throw away. Without them it cannot be decoded.
You have two ways out:
- Stream copy (
-c copy). ffmpeg copies the compressed data without decoding it. It is very fast and loses no quality. But it can only start cleanly at a keyframe, so the start time is moved to a nearby keyframe, or the clip begins with frames that show as a frozen or broken picture. - Re-encoding. ffmpeg decodes the frames, applies your edit, and encodes them again. It can start on any frame, because it builds a new keyframe at the cut point. It is slower and each encode costs a little quality.
| Property | Stream copy (`-c copy`) | Re-encode (`-c:v libx264`) |
|---|---|---|
| Speed | Seconds, limited by disk | Minutes, limited by CPU |
| Quality change | None | Small loss per generation |
| Cut accuracy | Only at keyframes | Frame-accurate |
| Filters (crop, zoom, burned captions) | Not possible | Possible |
| Typical use | Trimming the ends of a file | Removing mistakes, zooms |
Seeing the problem
First, find where the keyframes are. This command lists the timestamp of each video frame whose type is I.
ffprobe -v error -select_streams v:0 \
-show_entries frame=pts_time,pict_type \
-of csv=p=0 lecture.mp4 | grep ",I"Each output line has a time and the letter I, for example 4.004000,I. Use this list to see how far apart your keyframes are. On a large file the command takes a while because it reads every frame.
Now try a fast cut that asks for a start at 72 seconds.
ffmpeg -ss 72 -i lecture.mp4 -t 8 -c copy fast_cut.mp4If the nearest keyframe before 72 s is at 70.07 s, the result will begin around there, or the first moments may be wrong. How it behaves depends on the container, the ffmpeg version, and whether the file has edit lists. Treat the start time of a stream-copied cut as approximate.
Fast seek versus accurate seek
Where you put -ss matters.
- Before
-i(input seeking): ffmpeg jumps to the nearest keyframe at or before the time, then decodes forward to the exact frame when it re-encodes. It is fast, and with re-encoding it is accurate in current ffmpeg versions. - After
-i(output seeking): ffmpeg decodes from the start of the file and discards frames until the time. It is accurate but slow on long files.
For lectures, the best choice is usually input seeking plus re-encoding. This is what the zoom example from the earlier chapter does.
ffmpeg -ss 20 -t 10 -i lecture.mp4 \
-vf "crop=iw/1.5:ih/1.5:iw/4:ih/4,scale=1920:1080" \
-c:v libx264 -c:a aac zoomed_segment.mp4Because the command names libx264, ffmpeg decodes the clip, crops and scales it, then encodes new frames. The first frame of the output is a new keyframe, so the start is exact. The option -t 10 before -i limits the input to 10 seconds.
Removing a section accurately
The command from the cutting chapter removes a section from 72 s to 80 s. It works because it re-encodes.
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" \
lecture_cut.mp4Filters such as select work on decoded frames, so ffmpeg cannot copy the stream here. The filters drop frames in the range, and setpts and asetpts renumber the timestamps so the rest plays without a gap. Frame accuracy comes from the decoding step, not from any keyframe. Notice that no -c copy appears. If you added one, ffmpeg would refuse, since filters and stream copy cannot be combined on the same stream.
Controlling quality when you re-encode
Re-encoding is a lossy step, so choose settings that keep the loss invisible.
-crfsets quality for libx264. Lower is better and larger. Values from 18 to 23 are common, and 18 is close to visually lossless for most lectures.-presettrades speed for file size.slowgives smaller files thanfastat the same quality.-gsets the keyframe interval in frames. For a 30 fps video,-g 60gives a keyframe every 2 seconds. Shorter intervals make later cuts and seeking easier but raise the file size.
ffmpeg -i lecture.mp4 -c:v libx264 -crf 18 -preset slow -g 60 \
-c:a aac -b:a 160k lecture_editable.mp4This file is a good working copy. Its frequent keyframes make it quick to seek and cheap to cut with stream copy in the future.
Common errors
- Frozen or grey picture at the start of a clip. You used
-c copyand the cut started between keyframes. Re-encode, or move the start to a keyframe time from theffprobelist. - Audio and video drift apart after a fast cut. Audio frames are short, and video keyframes are far apart. Stream copy cuts each stream at different points. Re-encode, then check sync as described in the sync chapter.
- Cut lands 1 to 5 seconds early. This is the nearest-keyframe behavior of stream copy.
- Quality seems worse after several edits. You re-encoded the same file more than once. Go back to the original recording and apply all edits in one pass.
- A filter command fails with an error about filtering and stream copy. Remove
-c copyfor the stream the filter touches.
Decision criteria
Use stream copy when you only trim the head or tail of a file, you can accept keyframe-aligned boundaries, or you need a result in seconds. Use re-encoding when you remove a mistake from the middle, apply a zoom, burn in captions, or need a frame-exact cut. When in doubt for a lecture, re-encode at -crf 18 from the original file. The time cost is small next to the cost of a visible glitch.
Exercise
You run ffmpeg -ss 72 -i lecture.mp4 -t 8 -c copy clip.mp4 and the clip starts at about 70 seconds with a frozen first moment. Explain why, and give two ways to fix it.
Show answer
With -c copy, ffmpeg cannot decode frames, so the clip can only start cleanly at a keyframe, and the nearest one before 72 s is near 70 s. The frames after it that depend on discarded frames can show as frozen or broken. Fix 1: re-encode, for example by replacing -c copy with -c:v libx264 -c:a aac, which creates a new keyframe at 72 s. Fix 2: run ffprobe to list keyframe times and choose a start time that sits on a keyframe, accepting that the cut is not at exactly 72 s.
Exercise
Why can you not use -c copy together with -vf "crop=..." in one command?
Show answer
A crop filter changes the picture content, so ffmpeg must decode the frames, modify them, and encode them again. Stream copy passes compressed data through untouched, so there is nothing to apply a filter to.