1
0
Fork 0
mirror of https://github.com/alfg/mp4-rust.git synced 2024-05-19 16:58:04 +00:00

Basic example of parsing mp4 atom boxes in rust.

This commit is contained in:
Alf 2020-01-07 21:34:01 -08:00
commit 5d11bf9c78
4 changed files with 80 additions and 0 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
/target
**/*.rs.bk
*.exe
*.pdb
*.mp4

6
Cargo.lock generated Normal file
View file

@ -0,0 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "mp4"
version = "0.1.0"

9
Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "mp4"
version = "0.1.0"
authors = ["Alf <alf.g.jr@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

60
src/main.rs Normal file
View file

@ -0,0 +1,60 @@
use std::io::prelude::*;
use std::io::{BufReader, Read, SeekFrom};
use std::fs::File;
use std::convert::TryInto;
// struct Box {
// name: String,
// size: u32,
// offset: u32,
// }
fn main() -> std::io::Result<()> {
// Using BufReader.
let mut f = File::open("tears-of-steel-2s.mp4")?;
let filesize = f.metadata().unwrap().len();
println!("{:?}", filesize);
let mut reader = BufReader::new(f);
let mut offset = 0u64;
while offset < filesize {
// reader.seek(SeekFrom::Current(40 + 2872360));
reader.seek(SeekFrom::Current(offset as i64));
let mut buf = [0u8;8];
let n = reader.read(&mut buf);
let s = buf[0..4].try_into().unwrap();
let size = u32::from_be_bytes(s);
let t = buf[4..8].try_into().unwrap();
let typ = match std::str::from_utf8(t) {
Ok(v) => v,
Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
};
// Exit loop if size is 0.
if size == 0 { break; }
// println!("{}", buf.len());
// println!("{:?}", buf);
println!("{:?}", size);
println!("{:?}", typ);
// This will find all boxes, including nested boxes.
// let mut offset = match size {
// 1 => 4 + 4 + 8,
// _ => 4 + 4,
// };
// assert!(offset <= size);
offset = (size - 8) as u64;
println!("skip {:?}\n", offset);
}
println!("done");
Ok(())
}