Tool

Lossless Cut Keyframe Planner

Cutting with ffmpeg -c copy finishes instantly with no quality loss, but it often starts at the keyframe before the point you wanted rather than at the point itself. Pick a file and this tool shows where the keyframes are, then calculates how many seconds the cut drifts from the start and end times you want. The file is read only inside this browser.

Why is a -c copy cut off by a few seconds?

In compressed video, most frames are drawn by referencing the frames before (and sometimes after) them. A frame that can be reconstructed on its own, without any other frame, is a keyframe, and decoding can begin from it. Because -c copy moves the compressed data as it is, it can only start at a point where decoding is possible, that is, at a keyframe. So when the start time falls between keyframes, FFmpeg commonly starts at the keyframe before it. This tool calculates on that assumption; be sure to verify the real result with the ‘Checking the result’ method below. The reason keyframe structure looks the way it does is covered in the GOP structure lesson (Korean).

What the tool shows

  • Keyframe time list: read from the sync sample table (stss) and the frame timing tables (stts, ctts) in the MP4/MOV header. The video is not decoded, so even large files load quickly. A file without stss means that, by the spec, every frame is a keyframe, so the tool shows it as all-intra.
  • Gap statistics: the minimum, median and maximum keyframe gap. The maximum gap is the worst-case amount by which the start gets pulled back.
  • Average shift: how far back the start is pulled on average if you pick the start point anywhere in the video, calculated as Σg² ÷ (2·Σg). With evenly spaced keyframes, this is half the gap.
  • Calculation for your start and end times: the previous keyframe, how much the start is pulled back, how much gets trimmed off if you start at the next keyframe instead, and the last keyframe before the end.

Which command should you use?

  • ① Plain copy: fastest, with the quality unchanged. Use it when a bit of extra footage at the start doesn’t matter. The end is cut near a frame boundary.
  • ② Copy from the next keyframe: the start gets trimmed, but you don’t get unwanted footage before it. Convenient when the exact point isn’t critical.
  • ③ Re-encode the whole segment: cuts at the exact point. The price is that the entire segment is compressed one more time, which takes time and changes the quality slightly. For choosing a CRF, see the CRF recommender.
  • ④ Re-encode only the head, then join: only the part from the start to the next keyframe is compressed anew, the rest is copied, and the two pieces are joined with concat. You get an exact cut while most of the segment stays identical to the original. However, the two pieces may differ in specs and cause problems at the seam, so check them with the concat compatibility checker. This approach is commonly used, but depending on the file there can be an audio gap at the seam or differences between players.

What about MKV, WebM or fragmented MP4?

What this tool reads directly is regular MP4 and MOV. For anything else, run the command in the tab at the top of the tool in a terminal and paste the output. The command reads only packet information and doesn’t decode the video. Lines marked with K in the output are keyframes. Whether an MKV can be moved to MP4 can be checked with the MKV to MP4 remux feasibility checker.

Where is the keyframe interval decided?

It is decided at encoding time. x264 sets the maximum keyframe interval with -g (keyint), and it is known that extra keyframes can be inserted at scene changes regardless of this value. A shorter interval makes lossless cuts more accurate and seeking faster, but at the same quality it generally increases file size. This site has not yet measured the size increase per keyint, so it doesn’t put a number on it. Instead, you can check the actual interval of your own file with this tool. Seeing for yourself how many seconds apart the keyframes are in your screen recorder or camera files gives you a feel for it before you cut.

Checking the result

With ffprobe you can check whether the first keyframe of the cut file lands at the start point and whether the duration matches what you expected. If the start looks frozen or a black frame flashes by, the start isn’t aligned to a keyframe, so use ② or ③.

ffprobe -v error -select_streams v:0 -show_entries stream=start_time,duration -of default=nw=1 output.mp4
ffprobe -v error -select_streams v:0 -show_entries packet=pts_time,flags -of csv=p=0 output.mp4

Limitations to keep in mind

  • This tool is a calculation model. It assumes the common behavior that ‘the start is pulled back to the previous keyframe’. Depending on the FFmpeg version, container and options (such as -avoid_negative_ts), the resulting timestamps and the handling of the first frames can differ, and this site has not measured actual cut results.
  • Edit lists are not taken into account. If an MP4 has an edit list, the times a player shows can differ slightly from the times in the header. The calculation sets the first frame to 0.
  • In HEVC, the keyframe table can also include random access points other than IDR (such as CRA), and in that case some frames right after the start are known to possibly be skipped.
  • Audio is cut separately and breaks at compressed-frame boundaries. A slight offset where the sound comes in earlier or later than the video can occur.
  • Fragmented MP4, MKV and WebM can’t be read directly. Files with more than 3 million frames or a header larger than 400 MB are not read either.
  • Paste mode can’t know the video duration, so the stretch after the last keyframe is not included in the statistics.
  • Files and pasted text are not sent to a server, and this tool does not modify or save your files.