Expand description
Non-destructive MP4/ISOBMFF box editing.
Editing works on a tree where unmodified boxes are references into the
source file, so serialization streams untouched bytes through verbatim
(an mdat is never loaded into memory) and re-serializing an unedited
tree reproduces the source byte for byte. Box sizes are recomputed
bottom-up during layout, so every ancestor of an edited box is correct by
construction, and stco/co64 chunk offsets are remapped through the
exact old-offset → new-offset extent map — data that didn’t move keeps
its offsets, data that moved shifts by exactly the right amount.
use mp4box::edit::{Command, Editor};
use std::fs::File;
let mut editor = Editor::new();
editor.add_command(Command::Remove { path: "moov/udta".into() });
editor.set_tag("title", "My Movie")?;
let mut src = File::open("in.mp4")?;
let mut dst = File::create("out.mp4")?;
let stats = editor.process(&mut src, &mut dst)?;
println!("wrote {} bytes, {} chunk offsets adjusted",
stats.bytes_written, stats.chunk_offsets_adjusted);Fragmented (moof/sidx) and HEIF (iloc) files are refused: their
internal offsets are not covered by the fixup pass yet, and editing them
would corrupt the output.
Structs§
- Edit
Node - One box in the edit tree.
- Edit
Stats - Statistics from a completed edit.
- Edit
Tree - The whole file: top-level boxes plus any trailing bytes after the last box (shorter than a box header).
- Editor
- Applies a batch of
Commands to an MP4 source and writes the result. - Fixup
Stats
Enums§
- Command
- A single edit operation. Paths are slash-delimited fourcc segments with
optional indices for repeated boxes:
"moov/trak[1]/mdia/mdhd". An omitted index means the first match. - Header
Form - How the original box header was written, preserved on round-trip.
- Payload
- Payload of an edit node.