MP4 Reader and Writer in Rust 🦀
mp4
is a Rust library to read and write ISO-MP4 files. This package contains MPEG-4 specifications defined in parts:
- ISO/IEC 14496-12 - ISO Base Media File Format (QuickTime, MPEG-4, etc)
- ISO/IEC 14496-14 - MP4 file format
- ISO/IEC 14496-17 - Streaming text format
use std::fs::File;
use std::io::{BufReader};
use mp4::{Result};
fn main() -> Result<()> {
let f = File::open("tests/samples/minimal.mp4").unwrap();
let size = f.metadata()?.len();
let reader = BufReader::new(f);
let mp4 = mp4::Mp4Reader::read_header(reader, size)?;
// Print boxes.
println!("major brand: {}", mp4.ftyp.major_brand);
println!("timescale: {}", mp4.moov.mvhd.timescale);
// Use available methods.
println!("size: {}", mp4.size());
let mut compatible_brands = String::new();
for brand in mp4.compatible_brands().iter() {
compatible_brands.push_str(&brand.to_string());
compatible_brands.push_str(",");
}
println!("compatible brands: {}", compatible_brands);
println!("duration: {:?}", mp4.duration());
// Track info.
for track in mp4.tracks().values() {
println!(
"track: #{}({}) {} : {}",
track.track_id(),
track.language(),
track.track_type()?,
track.box_type()?,
);
}
Ok(())
}
See examples/ for more examples.
Add to your Cargo.toml
:
mp4 = "0.12.0"
cargo build
cargo clippy --fix
cargo fmt --all
mp4info
cargo run --example mp4info <movie.mp4>
mp4dump
cargo run --example mp4dump <movie.mp4>
cargo test
With print statement output.
cargo test -- --nocapture
Run fmt to catch formatting errors.
rustup component add rustfmt
cargo fmt --all -- --check
Run Clippy tests to catch common lints and mistakes.
rustup component add clippy
cargo clippy --no-deps -- -D warnings
cargo bench
View HTML report at target/criterion/report/index.html
cargo docs
View at target/doc/mp4/index.html
Run afl++ to find invalid inputs that can trigger crashes.
cargo install afl
cd fuzz
cargo afl build
cargo afl fuzz -i in -o out target/debug/mp4-fuzz-target
Fuzzing is computationally expensive; it's recommended to fuzz on a machine with plenty of resources.
See the Rust Fuzz Book and the afl++ documentation for more information.
afl++ needs at least one example of a valid mp4 file in the fuzz/in
directory to use as a starting point. A simple video is included, but you can add others or replace it if you wish. Each file should be 10 MB or less.
The included video was generated with ffmpeg; you can use ffmpeg instead of/in addition to capturing videos with a camera:
ffmpeg -f lavfi -t 5 -i color=c=#000000:s=1920x1080 -f lavfi -t 5 -i anullsrc=channel_layout=stereo:sample_rate=44100 -c:v libx264 -tune stillimage -pix_fmt yuv420p in/blank_5s.mp4
See the mp4-inspector project as a reference for using this library in Javascript via Web Assembly.
- https://github.com/mozilla/mp4parse-rust
- https://github.com/pcwalton/rust-media
- https://github.com/alfg/mp4
MIT