mp4box
A minimal, dependency-light MP4/ISOBMFF parser and editor for Rust.
Parses the full box tree (including codec configuration inside stsd), decodes known boxes into typed structures, extracts per-sample tables from both progressive and fragmented (fMP4/DASH/CMAF) files, reads iTunes metadata, and performs non-destructive box editing with automatic size and chunk-offset fixup.
Suitable for CLIs, Tauri backends, media inspectors, and developer tooling.
Installation
Add this to your Cargo.toml:
[]
= "0.9.0"
= "1.0" # For error handling in examples
The library core depends only on anyhow and serde. Two default-on
features add the rest:
edit— non-destructive box editing (no extra dependencies)cli— themp4dump/mp4info/mp4samples/mp4editbinaries (pulls inclapandserde_json)
For the lightest library-only build:
= { = "0.9.0", = false, = ["edit"] }
Quick Start
use get_boxes;
use File;
Features
- Zero-dependency core parser (
Read + Seek) - Full MP4/ISOBMFF box tree
- Leaf, FullBox (version/flags), Container, FullBox containers (
meta,stsd), UUID - Large-size (64-bit) boxes; recursion into
stsdsample entries and their codec configuration children (avcC,hvcC,esds,dOps, ...)
- Leaf, FullBox (version/flags), Container, FullBox containers (
- Known-box registry — hundreds of ISO/HEIF/MPEG boxes with full names
- Typed structured decoding —
mvhd,tkhd,mdhd,stsd, the whole sample table family,elst,sidx, and the fragment boxes (tfhd/tfdt/trun/trex) decode to serializable structs - Sample tables — per-sample DTS/PTS, duration, size, file offset, and keyframe flags for progressive and fragmented (fMP4/DASH/CMAF) files
- iTunes metadata — read tags with
get_itunes_tags, write them with the edit API - Non-destructive editing (
editfeature) — remove/insert/replace boxes, patch header fields, set tags; box sizes andstco/co64chunk offsets are fixed up automatically - Tolerant parsing —
get_boxes_tolerantrecovers from malformed boxes, returning the partial tree plus located issues instead of an error - Custom decoders — attach your own parser for any 4CC or UUID
- Frontend-friendly JSON — works perfectly in Tauri or WebView apps
- CLI tools —
mp4dump,mp4info,mp4samples,mp4edit
Example: Build a JSON tree (for GUIs / Tauri)
use get_boxes;
use File;
let mut file = open?;
let size = file.metadata?.len;
let boxes = get_boxes?;
println!;
This returns a serializable tree:
Known boxes also carry structured_data — typed values instead of strings:
use StructuredData;
if let Some = &boxes.children.as_ref.unwrap.structured_data
Example: Per-sample tables (progressive and fragmented)
use track_samples_from_path;
for track in track_samples_from_path?
Fragmented files (moof/traf/trun) are handled transparently: samples are
assembled across all fragments using the tfhd/trex defaulting rules, with
byte offsets and keyframe flags matching what ffprobe reports.
Example: iTunes metadata
use get_itunes_tags;
use File;
let mut file = open?;
let size = file.metadata?.len;
let tags = get_itunes_tags?;
// {"title": "...", "artist": "...", "encoder": "...", "track": "3/12", ...}
Example: Editing (requires the edit feature)
Editing is non-destructive: the source file is never modified, untouched
bytes are streamed through verbatim (an mdat is never loaded into memory),
and ancestor box sizes plus stco/co64 chunk offsets are recomputed
automatically. Re-serializing with no edits reproduces the input byte for
byte.
use Editor;
let mut editor = new;
editor.set_tag?;
editor.set_field;
editor.remove; // paths support indices: moov/trak[1]/...
editor.remove_all; // strip every `free` box
editor.faststart; // move moov before mdat
let stats = editor.process_file?;
println!;
Fragmented (moof/sidx) and HEIF (iloc) files are refused with a clear
error rather than corrupted — their internal offsets are not covered by the
fixup pass yet.
Command-line tools
mp4dump — box tree explorer
)
)
)
) ()
) (ver=0, flags=0x000000)
)
) ()
) ()
)
Filter a subtree, emit JSON, or dump raw payload bytes:
Damaged files are handled gracefully by default: parsing recovers past
malformed boxes, prints the partial tree, reports what was wrong and where
on stderr, and exits with code 2. Use --strict to fail on the first
malformed box instead.
)
;
;
mp4info — media summary
mp4samples — per-sample tables
) timescale=12800 duration=7680000 sample_count=15000
) )
mp4edit — non-destructive editor
)
Optimize for progressive playback (moov before mdat, like qt-faststart):
Adding Custom Box Decoders
Extend the default registry with a decoder for any 4CC or UUID; the decoded value shows up in the same tree as the built-ins.
use ;
use ;
use File;
use Read;
;
let reg = default_registry.with_decoder;
let mut file = open?;
let size = file.metadata?.len;
let boxes = get_boxes_with_registry?;
Examples
Runnable examples live in examples/:
| Example | Shows |
|---|---|
boxes |
Walking the box tree with names and version/flags |
media_info |
Typed structured data: movie/track headers, codec details, edit lists, tags |
samples |
Sample table analysis |
fragments |
Fragmented MP4 inspection and assembled per-track samples |
edit |
Setting tags and zeroing timestamps with the edit API |
simple |
Hex-dumping a byte range |
License
MIT