1
0
Fork 0
mirror of https://github.com/alfg/mp4-rust.git synced 2025-01-05 09:58:40 +00:00

update box parsing logic.

This commit is contained in:
Alf 2020-01-12 00:00:30 -08:00
parent fc92c60298
commit 06f388f20e

View file

@ -13,14 +13,36 @@ pub enum Error {
} }
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)] #[derive(Debug, Default)]
struct BMFF {
ftyp: FtypBox,
}
impl BMFF {
fn new() -> BMFF {
Default::default()
}
}
#[derive(Debug, Default)]
struct BMFFBox {
head: BoxHeader,
}
impl BMFFBox {
fn new() -> BMFFBox {
Default::default()
}
}
#[derive(Debug, Default)]
struct BoxHeader { struct BoxHeader {
name: String, name: String,
size: u64, size: u64,
offset: u64, offset: u64,
} }
#[derive(Debug)] #[derive(Debug, Default)]
struct FtypBox { struct FtypBox {
major_brand: FourCC, major_brand: FourCC,
minor_version: u32, minor_version: u32,
@ -60,15 +82,63 @@ impl fmt::Debug for FourCC {
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
// Using BufReader. // Open file and read boxes.
let f = File::open("tears-of-steel-2s.mp4")?; let f = File::open("tears-of-steel-2s.mp4")?;
// let boxes = read_boxes(f);
let bmff = read_boxes(f);
// Print results.
println!("{:?}", bmff.unwrap());
// Done.
println!("done");
Ok(())
}
fn read_boxes(f: File) -> Result<BMFF> {
let filesize = f.metadata().unwrap().len(); let filesize = f.metadata().unwrap().len();
let mut reader = BufReader::new(f); let mut reader = BufReader::new(f);
let mut boxes = Vec::new(); let mut bmff = BMFF::new();
let mut start = 0u64; let mut start = 0u64;
while start < filesize { while start < filesize {
// Get box header.
let header = read_box_header(&mut reader, start).unwrap();
let BoxHeader{ name, size, offset } = header;
let mut b = BMFFBox::new();
b.head = BoxHeader{
name: name.try_into().unwrap(),
size: size as u64,
offset: offset as u64,
};
// Match and parse the filetype.
match b.head.name.as_ref() {
"ftyp" => {
let ftyp = parse_ftyp_box(&mut reader, 0, size as u32).unwrap();
bmff.ftyp = ftyp;
}
"free" => {
start = 0;
}
"mdat" => {
start = (size as u32 - HEADER_SIZE) as u64;
}
"moov" => {
start = (size as u32 - HEADER_SIZE) as u64;
}
"moof" => {
start = (size as u32 - HEADER_SIZE) as u64;
}
_ => break
};
}
Ok(bmff)
}
fn read_box_header(reader: &mut BufReader<File>, start: u64) -> Result<BoxHeader> {
// Seek to offset. // Seek to offset.
let _r = reader.seek(SeekFrom::Current(start as i64)); let _r = reader.seek(SeekFrom::Current(start as i64));
@ -80,11 +150,10 @@ fn main() -> std::io::Result<()> {
let s = buf[0..4].try_into().unwrap(); let s = buf[0..4].try_into().unwrap();
let size = u32::from_be_bytes(s); let size = u32::from_be_bytes(s);
// Exit loop if size is 0. // TODO: Err if size is 0.
if size == 0 { break; } // if size == 0 { break; }
// Get box type string. // Get box type string.
// println!("{:?}", buf);
let t = buf[4..8].try_into().unwrap(); let t = buf[4..8].try_into().unwrap();
let typ = match std::str::from_utf8(t) { let typ = match std::str::from_utf8(t) {
Ok(v) => v, Ok(v) => v,
@ -95,52 +164,11 @@ fn main() -> std::io::Result<()> {
1 => 4 + 4 + 8, 1 => 4 + 4 + 8,
_ => 4 + 4, _ => 4 + 4,
}; };
Ok(BoxHeader {
// println!("{:?}", typ);
// println!("{:?}", size);
// Match and parse the filetype.
match typ {
"ftyp" => {
let o = parse_ftyp_box(&mut reader, 0, size);
println!("{:?}", o.unwrap());
// start += (size - HEADER_SIZE) as u64;
// TODO: Add to context.
}
"free" => {
start = 0;
}
"mdat" => {
start = (size - HEADER_SIZE) as u64;
}
"moov" => {
start = (size - HEADER_SIZE) as u64;
}
"moof" => {
start = (size - HEADER_SIZE) as u64;
}
_ => break
};
// Make Box struct and add to vector.
let b = BoxHeader{
name: typ.try_into().unwrap(), name: typ.try_into().unwrap(),
size: size as u64, size: size as u64,
offset: offset as u64, offset: offset as u64,
}; })
boxes.push(b);
}
// Print results.
for b in boxes {
println!("{:?}", b);
}
// Done.
println!("done");
Ok(())
} }
fn parse_ftyp_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<FtypBox> { fn parse_ftyp_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<FtypBox> {