Frame-accurate smart cuts

Copy the middle of each piece, re-encode only around each cut: 1.7× to 2.0× faster than an ffmpeg re-encode.

What it does

export and export_with_audio write an edit from pieces of one or more files. They copy the middle of each piece as it is and re-encode only the frames around each cut, so the result is frame-accurate and 1.7× to 2.0× faster than ffmpeg re-encoding every frame.

ffmpeg's stream copy is faster still, 4.8× to 6.1× faster than this export, but it encodes nothing and can only cut on keyframes.

The frame above carries a caption and a corner mark drawn as one RGBA layer and blended by media::Blender in 1.8 ms at 1280x720.

  • Cuts that share a decode

    When two cut edges fall in one GOP, as a piece's end and the next piece's start do when silences are cut out, they share one decode from the keyframe instead of each decoding it.

  • VP9 sources too

    VP9 sources cut the same way into MP4: packets are copied as they are, and only the frames before each piece's first keyframe are encoded.

  • Overlays composed on the card

    A watermark, or karaoke captions that change at every word. On VA-API with Mesa's driver the overlay is blended by a video-processing pass straight into the encoder's input: no copy to system memory, and the overlay uploaded only when it changes, and only its painted area.

  • Your own audio, to the sample

    The audio callback gets each piece's index and its exact sample count and returns that many 48 kHz stereo samples, which pito-codecs' AAC encoder turns into one track as long as the video.

Measured

a 3-cut 1080p export; ffmpeg re-encoding every frame 9.9×
16.5× real time
How it was measured
a 3-cut 1440p export; ffmpeg re-encoding every frame 5.7×
11.1× real time
How it was measured
46 pieces with the gaps between them removed, on the card, from 9.6 s before cut edges shared their decode
6.4 s
How it was measured
CPU time to export 130 s of 1920x804 with a corner mark on the card; 43.6 s decoding and blending on the CPU
1.9 s
How it was measured
120 s of 1920x804 VP9 to H.264 with a corner mark on the card; 24.1 s on the CPU, 33.9 s with captions
6.4 s
How it was measured

How to use it

  1. Export an edit with audio you mixed yourself:

    let done = export_with_audio("edit.mp4".as_ref(), &pieces, ExportOptions::default(), |piece, samples| mixer.render(piece, samples), |_| true)?;
  2. Draw one RGBA overlay on many frames without copying it each time:

    let mut blender = Blender::new();
    blender.draw(&mut frame, &watermark);
  3. Stitch clips with pcod: copied when they match, re-encoded when they don't, and it says which and why.

    pcod join intro.mp4 scene.mp4 outro.mp4 film.mp4
Paired bars, times real time, higher is better. 1080p: pito-codecs 16.5×, ffmpeg re-encode 9.9×, ffmpeg stream copy 78.6×. 1440p: 11.07×, 5.67×, 67.4×. Ours and the re-encode are frame-accurate; a stream copy encodes nothing and cuts only on keyframes.
Smart-cut export, 3 cuts ffmpeg's stream copy is faster still, but it encodes nothing and can only cut on keyframes. How it was measured: against ffmpeg
Paired bars, seconds, lower is better, on the card against CPU decode and blend. Wall time: 1920x804 130 s with a mark 10.16 against 12.8; with captions 10.06 against 12.97; 1080p 30 s with a mark 2.85 against 3.08. CPU time: 1.9 against 43.6; 2.4 against 44.3; 0.5 against 10.2.
Export with an overlay, H.264 Both encode on the GPU; one run each, load about 90. How it was measured: export with an overlay

Read it. Run it. It's MIT.

The code is MIT licensed, © Catalin Ilinca: read it, fork it, ship it in your own work. Its name and logos, and PITO MD's, are © Catalin Ilinca, all rights reserved, and are not covered by the MIT licence.

Credits

The clip comes from pito-codecs v1.16.3: a picture its examples/readme_shots.rs makes with its own decoders and overlay blend from public-domain NASA footage ("A Message of Resilience from the International Space Station", NASA Johnson).

The charts are drawn by pito-compare from pito-codecs' data files and from numbers its README states.

Set in Archivo by Omnibus-Type and Atkinson Hyperlegible Mono by the Braille Institute, both under the SIL Open Font License.