2020-01-08 05:34:01 +00:00
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::io::{BufReader, Read, SeekFrom};
|
|
|
|
use std::fs::File;
|
|
|
|
use std::convert::TryInto;
|
|
|
|
|
2020-01-08 06:51:21 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct Box {
|
|
|
|
name: String,
|
|
|
|
size: u32,
|
|
|
|
offset: u32,
|
|
|
|
}
|
2020-01-08 05:34:01 +00:00
|
|
|
|
|
|
|
fn main() -> std::io::Result<()> {
|
|
|
|
|
|
|
|
// Using BufReader.
|
2020-01-08 06:51:21 +00:00
|
|
|
let f = File::open("tears-of-steel-2s.mp4")?;
|
2020-01-08 05:34:01 +00:00
|
|
|
let filesize = f.metadata().unwrap().len();
|
|
|
|
let mut reader = BufReader::new(f);
|
2020-01-08 06:51:21 +00:00
|
|
|
let mut v = Vec::new();
|
2020-01-08 05:34:01 +00:00
|
|
|
|
|
|
|
let mut offset = 0u64;
|
|
|
|
while offset < filesize {
|
|
|
|
|
2020-01-08 06:51:21 +00:00
|
|
|
// Seek to offset.
|
|
|
|
let _r = reader.seek(SeekFrom::Current(offset as i64));
|
2020-01-08 05:34:01 +00:00
|
|
|
|
2020-01-08 06:51:21 +00:00
|
|
|
// Create and read to buf.
|
2020-01-08 05:34:01 +00:00
|
|
|
let mut buf = [0u8;8];
|
2020-01-08 06:51:21 +00:00
|
|
|
let _n = reader.read(&mut buf);
|
2020-01-08 05:34:01 +00:00
|
|
|
|
2020-01-08 06:51:21 +00:00
|
|
|
// Get size.
|
2020-01-08 05:34:01 +00:00
|
|
|
let s = buf[0..4].try_into().unwrap();
|
|
|
|
let size = u32::from_be_bytes(s);
|
2020-01-08 06:51:21 +00:00
|
|
|
|
|
|
|
// Exit loop if size is 0.
|
|
|
|
if size == 0 { break; }
|
2020-01-08 05:34:01 +00:00
|
|
|
|
2020-01-08 06:51:21 +00:00
|
|
|
// Get box type string.
|
2020-01-08 05:34:01 +00:00
|
|
|
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),
|
|
|
|
};
|
|
|
|
|
2020-01-08 06:51:21 +00:00
|
|
|
// Make Box struct and add to vector.
|
|
|
|
let b = Box{
|
|
|
|
name: typ.try_into().expect("asdf"),
|
|
|
|
size: size,
|
|
|
|
offset: offset as u32,
|
|
|
|
};
|
|
|
|
v.push(b);
|
2020-01-08 05:34:01 +00:00
|
|
|
|
|
|
|
// This will find all boxes, including nested boxes.
|
|
|
|
// let mut offset = match size {
|
|
|
|
// 1 => 4 + 4 + 8,
|
|
|
|
// _ => 4 + 4,
|
|
|
|
// };
|
|
|
|
// assert!(offset <= size);
|
|
|
|
|
2020-01-08 06:51:21 +00:00
|
|
|
// Increment offset.
|
2020-01-08 05:34:01 +00:00
|
|
|
offset = (size - 8) as u64;
|
|
|
|
}
|
|
|
|
|
2020-01-08 06:51:21 +00:00
|
|
|
// Print results.
|
|
|
|
for a in v {
|
|
|
|
println!("{:?}", a);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done.
|
2020-01-08 05:34:01 +00:00
|
|
|
println!("done");
|
|
|
|
Ok(())
|
|
|
|
}
|