M: A question about FFmpeg encoding. Say I've got a MOV file, and I know the exact frames which should be used as, what's the term, iframes in an mp4 or another format. Something where I know cleanly where scenes separate. Is there a way to incorporate this into the encoding process so that the iframes are at the right spot instead of, you know, whatever algorithm there is straddling a scene split in a way that makes future splitting or modification difficult.
G: Yes. In H.264/H.265 (and most interframe codecs) you can force keyframes (I-frames / IDR frames) at exact timestamps/frames during encode, so scene boundaries become clean cut points later.
If you know the cut points as timestamps:
ffmpeg -i input.mov \
-c:v libx264 -crf 18 -preset medium \
-force_key_frames "expr:gte(t,n_forced*2)" \
-c:a aac -b:a 192k \
output.mp4That example forces a keyframe every 2 seconds (template). For your use, you give an explicit list:
ffmpeg -i input.mov \
-c:v libx264 -crf 18 -preset medium \
-force_key_frames 12.345,27.900,55.033 \
-c:a aac -b:a 192k \
output.mp4Those numbers are seconds.
If you truly mean “frame 1234, 5678, …” you can do it, but FFmpeg’s force mechanism is timestamp-based, so you convert frames → seconds using the frame rate.
For CFR material with fps = F:
Then:
-force_key_frames (frame1/F),(frame2/F),(frame3/F)If your source is VFR (common in phone MOVs), “exact frame number” is slippery; you’ll want to normalize first (see “Third option” below).
A “keyframe” request can result in an I-frame that still references earlier frames depending on encoder settings. For clean random access and splitting, you usually want closed GOP / IDR behavior.
For x264, add:
-x264-params "open-gop=0"And it’s often wise to avoid scenecut inserting extras if you’re trying to make GOP structure predictable:
-x264-params "scenecut=0:open-gop=0"(You can still force your scene-boundary keyframes; you’re just disabling the encoder’s own scene-triggered extras.)
So a more “editing-friendly” command is:
ffmpeg -i input.mov \
-c:v libx264 -crf 18 -preset medium \
-force_key_frames 12.345,27.900,55.033 \
-x264-params "scenecut=0:open-gop=0" \
-c:a aac -b:a 192k \
output.mp4If your forced points are far apart, the encoder will still place keyframes according to max GOP. You can set it explicitly:
-g = maximum GOP length (in frames)-keyint_min = minimum distance between I-framesExample at 30 fps, if you never want an automatic keyframe more often than every 10 seconds unless forced:
-g 300 -keyint_min 300Then your forced list adds additional I-frames at scene cuts.
If you want later splitting/modification to be painless, you have two fundamentally different strategies:
Good компромise: small files, but later cuts are clean if you cut on keyframes.
If you’re going to do lots of slicing/rework, consider intraframe codecs (every frame is a keyframe): ProRes / DNxHR. Huge files, but trivial editing.
Example ProRes:
ffmpeg -i input.mov -c:v prores_ks -profile:v 3 -c:a pcm_s16le output.mov-vf fps=...) or強制(きょうせい) キーフレーム は できます。— You can force
keyframes.
場面(ばめん) の 切(き)れ 目(め) に キーフレーム を 入(い)れる と
編集(へんしゅう)しやすい。— Putting keyframes at scene boundaries
makes editing easier.
FFmpeg can detect scene changes (select='gt(scene,0.3)')
and you can extract timestamps, then feed them back into
-force_key_frames. It’s doable, just a bit
shell-script-y.
If you tell me whether your “exact frames” are literal frame numbers, or timestamps, and whether the MOV is phone/VFR, I can give you a command that’s actually correct for your case.