1
0
Fork 0
mirror of https://github.com/alfg/mp4-rust.git synced 2025-01-03 08: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,87 +82,93 @@ 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 {
// Seek to offset. // Get box header.
let _r = reader.seek(SeekFrom::Current(start as i64)); let header = read_box_header(&mut reader, start).unwrap();
let BoxHeader{ name, size, offset } = header;
// Create and read to buf. let mut b = BMFFBox::new();
let mut buf = [0u8;8]; // 8 bytes for box header. b.head = BoxHeader{
let _n = reader.read(&mut buf); name: name.try_into().unwrap(),
size: size as u64,
// Get size. offset: offset as u64,
let s = buf[0..4].try_into().unwrap();
let size = u32::from_be_bytes(s);
// Exit loop if size is 0.
if size == 0 { break; }
// Get box type string.
// println!("{:?}", buf);
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),
}; };
let offset = match size {
1 => 4 + 4 + 8,
_ => 4 + 4,
};
// println!("{:?}", typ);
// println!("{:?}", size);
// Match and parse the filetype. // Match and parse the filetype.
match typ { match b.head.name.as_ref() {
"ftyp" => { "ftyp" => {
let o = parse_ftyp_box(&mut reader, 0, size); let ftyp = parse_ftyp_box(&mut reader, 0, size as u32).unwrap();
println!("{:?}", o.unwrap()); bmff.ftyp = ftyp;
// start += (size - HEADER_SIZE) as u64;
// TODO: Add to context.
} }
"free" => { "free" => {
start = 0; start = 0;
} }
"mdat" => { "mdat" => {
start = (size - HEADER_SIZE) as u64; start = (size as u32 - HEADER_SIZE) as u64;
} }
"moov" => { "moov" => {
start = (size - HEADER_SIZE) as u64; start = (size as u32 - HEADER_SIZE) as u64;
} }
"moof" => { "moof" => {
start = (size - HEADER_SIZE) as u64; start = (size as u32 - HEADER_SIZE) as u64;
} }
_ => break _ => break
}; };
// Make Box struct and add to vector.
let b = BoxHeader{
name: typ.try_into().unwrap(),
size: size as u64,
offset: offset as u64,
};
boxes.push(b);
} }
Ok(bmff)
}
// Print results. fn read_box_header(reader: &mut BufReader<File>, start: u64) -> Result<BoxHeader> {
for b in boxes { // Seek to offset.
println!("{:?}", b); let _r = reader.seek(SeekFrom::Current(start as i64));
} // Create and read to buf.
let mut buf = [0u8;8]; // 8 bytes for box header.
let _n = reader.read(&mut buf);
// Done. // Get size.
println!("done"); let s = buf[0..4].try_into().unwrap();
Ok(()) let size = u32::from_be_bytes(s);
// TODO: Err if size is 0.
// if size == 0 { break; }
// Get box type string.
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),
};
let offset = match size {
1 => 4 + 4 + 8,
_ => 4 + 4,
};
Ok(BoxHeader {
name: typ.try_into().unwrap(),
size: size as u64,
offset: offset as u64,
})
} }
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> {