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:
parent
fc92c60298
commit
06f388f20e
1 changed files with 83 additions and 55 deletions
128
src/main.rs
128
src/main.rs
|
@ -13,14 +13,36 @@ pub enum 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 {
|
||||
name: String,
|
||||
size: u64,
|
||||
offset: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Default)]
|
||||
struct FtypBox {
|
||||
major_brand: FourCC,
|
||||
minor_version: u32,
|
||||
|
@ -60,15 +82,63 @@ impl fmt::Debug for FourCC {
|
|||
|
||||
fn main() -> std::io::Result<()> {
|
||||
|
||||
// Using BufReader.
|
||||
// Open file and read boxes.
|
||||
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 mut reader = BufReader::new(f);
|
||||
let mut boxes = Vec::new();
|
||||
let mut bmff = BMFF::new();
|
||||
|
||||
let mut start = 0u64;
|
||||
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.
|
||||
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 size = u32::from_be_bytes(s);
|
||||
|
||||
// Exit loop if size is 0.
|
||||
if size == 0 { break; }
|
||||
// TODO: Err 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,
|
||||
|
@ -95,52 +164,11 @@ fn main() -> std::io::Result<()> {
|
|||
1 => 4 + 4 + 8,
|
||||
_ => 4 + 4,
|
||||
};
|
||||
|
||||
// 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{
|
||||
Ok(BoxHeader {
|
||||
name: typ.try_into().unwrap(),
|
||||
size: size 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> {
|
||||
|
|
Loading…
Reference in a new issue