2020-01-08 05:34:01 +00:00
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::io::{BufReader, Read, SeekFrom};
|
|
|
|
use std::fs::File;
|
2020-01-10 06:26:08 +00:00
|
|
|
use std::fmt;
|
2020-01-08 05:34:01 +00:00
|
|
|
use std::convert::TryInto;
|
2020-01-12 03:34:29 +00:00
|
|
|
use byteorder::{ReadBytesExt};
|
|
|
|
|
|
|
|
const HEADER_SIZE: u32 = 8;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
InvalidData(&'static str),
|
|
|
|
}
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
2020-01-08 05:34:01 +00:00
|
|
|
|
2020-01-08 06:51:21 +00:00
|
|
|
#[derive(Debug)]
|
2020-01-12 03:34:29 +00:00
|
|
|
struct BoxHeader {
|
2020-01-08 06:51:21 +00:00
|
|
|
name: String,
|
2020-01-12 03:34:29 +00:00
|
|
|
size: u64,
|
|
|
|
offset: u64,
|
2020-01-08 06:51:21 +00:00
|
|
|
}
|
2020-01-08 05:34:01 +00:00
|
|
|
|
2020-01-10 06:26:08 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct FtypBox {
|
|
|
|
major_brand: FourCC,
|
|
|
|
minor_version: u32,
|
|
|
|
compatible_brands: Vec<FourCC>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, PartialEq, Clone)]
|
|
|
|
pub struct FourCC {
|
|
|
|
pub value: String
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u32> for FourCC {
|
|
|
|
fn from(number: u32) -> FourCC {
|
|
|
|
let mut box_chars = Vec::new();
|
|
|
|
for x in 0..4 {
|
|
|
|
let c = (number >> (x * 8) & 0x0000_00FF) as u8;
|
|
|
|
box_chars.push(c);
|
|
|
|
}
|
|
|
|
box_chars.reverse();
|
|
|
|
|
|
|
|
let box_string = match String::from_utf8(box_chars) {
|
|
|
|
Ok(t) => t,
|
|
|
|
_ => String::from("null"), // error to retrieve fourcc
|
|
|
|
};
|
|
|
|
|
|
|
|
FourCC {
|
|
|
|
value: box_string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for FourCC {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10 06:26:08 +00:00
|
|
|
let mut boxes = Vec::new();
|
2020-01-08 05:34:01 +00:00
|
|
|
|
2020-01-12 03:34:29 +00:00
|
|
|
let mut start = 0u64;
|
|
|
|
while start < filesize {
|
2020-01-08 05:34:01 +00:00
|
|
|
|
2020-01-08 06:51:21 +00:00
|
|
|
// Seek to offset.
|
2020-01-12 03:34:29 +00:00
|
|
|
let _r = reader.seek(SeekFrom::Current(start 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-12 03:34:29 +00:00
|
|
|
let mut buf = [0u8;8]; // 8 bytes for box header.
|
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-12 03:34:29 +00:00
|
|
|
// println!("{:?}", buf);
|
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-12 03:34:29 +00:00
|
|
|
let offset = match size {
|
|
|
|
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
|
2020-01-10 06:26:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-01-08 06:51:21 +00:00
|
|
|
// Make Box struct and add to vector.
|
2020-01-12 03:34:29 +00:00
|
|
|
let b = BoxHeader{
|
2020-01-10 06:26:08 +00:00
|
|
|
name: typ.try_into().unwrap(),
|
2020-01-12 03:34:29 +00:00
|
|
|
size: size as u64,
|
|
|
|
offset: offset as u64,
|
2020-01-08 06:51:21 +00:00
|
|
|
};
|
2020-01-10 06:26:08 +00:00
|
|
|
boxes.push(b);
|
2020-01-08 05:34:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 06:51:21 +00:00
|
|
|
// Print results.
|
2020-01-10 06:26:08 +00:00
|
|
|
for b in boxes {
|
|
|
|
println!("{:?}", b);
|
|
|
|
|
2020-01-08 06:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Done.
|
2020-01-08 05:34:01 +00:00
|
|
|
println!("done");
|
|
|
|
Ok(())
|
2020-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 03:34:29 +00:00
|
|
|
fn parse_ftyp_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<FtypBox> {
|
2020-01-10 06:26:08 +00:00
|
|
|
let major = f.read_u32::<byteorder::BigEndian>().unwrap();
|
|
|
|
let minor = f.read_u32::<byteorder::BigEndian>().unwrap();
|
2020-01-12 03:34:29 +00:00
|
|
|
if size % 4 != 0 {
|
|
|
|
return Err(Error::InvalidData("invalid ftyp size"));
|
|
|
|
}
|
2020-01-10 06:26:08 +00:00
|
|
|
let brand_count = (size - 16) / 4; // header + major + minor
|
|
|
|
|
|
|
|
let mut brands = Vec::new();
|
|
|
|
for _ in 0..brand_count {
|
|
|
|
let b = f.read_u32::<byteorder::BigEndian>().unwrap();
|
|
|
|
brands.push(From::from(b));
|
|
|
|
}
|
|
|
|
|
2020-01-12 03:34:29 +00:00
|
|
|
Ok(FtypBox {
|
2020-01-10 06:26:08 +00:00
|
|
|
major_brand: From::from(major),
|
|
|
|
minor_version: minor,
|
|
|
|
compatible_brands: brands,
|
2020-01-12 03:34:29 +00:00
|
|
|
})
|
2020-01-10 06:26:08 +00:00
|
|
|
}
|