1
0
Fork 0
mirror of https://github.com/alfg/mp4-rust.git synced 2024-06-13 02:19:21 +00:00
mp4-rust/src/lib.rs

139 lines
3.1 KiB
Rust
Raw Normal View History

use std::io::{BufReader, Read, SeekFrom, Seek};
use std::fs::File;
use std::convert::TryInto;
mod atoms;
use crate::atoms::*;
2020-01-12 03:34:29 +00:00
mod error;
pub use error::Error;
2020-01-12 03:34:29 +00:00
pub type Result<T> = std::result::Result<T, Error>;
// XXX if box has largesize
const HEADER_SIZE: u64 = 8;
#[derive(Debug, PartialEq)]
pub enum TrackType {
Audio,
Video,
Metadata,
Unknown,
}
2020-01-12 08:00:30 +00:00
#[derive(Debug, Default)]
pub struct BMFF {
pub ftyp: FtypBox,
pub moov: Option<MoovBox>,
pub size: u64,
2020-01-12 08:00:30 +00:00
}
impl BMFF {
fn new() -> BMFF {
Default::default()
}
}
#[derive(Debug, Clone, Copy)]
2020-01-12 03:34:29 +00:00
struct BoxHeader {
name: BoxType,
2020-01-12 03:34:29 +00:00
size: u64,
2020-01-08 06:51:21 +00:00
}
impl BoxHeader {
fn new(name: BoxType, size: u64) -> Self {
Self { name, size }
}
}
pub fn read_mp4(f: File) -> Result<BMFF> {
2020-01-12 08:00:30 +00:00
// Open file and read boxes.
let bmff = read_boxes(f)?;
2020-01-12 08:00:30 +00:00
Ok(bmff)
2020-01-12 08:00:30 +00:00
}
fn read_boxes(f: File) -> Result<BMFF> {
let filesize = f.metadata()?.len();
let mut reader = BufReader::new(f);
2020-01-12 08:00:30 +00:00
let mut bmff = BMFF::new();
bmff.size = filesize;
2020-01-12 03:34:29 +00:00
let mut start = 0u64;
while start < filesize {
2020-01-12 08:00:30 +00:00
// Get box header.
let header = read_box_header(&mut reader, start)?;
let BoxHeader{ name, size } = header;
// Match and parse the atom boxes.
match name {
BoxType::FtypBox => {
let ftyp = FtypBox::read_box(&mut reader, size)?;
2020-01-12 08:00:30 +00:00
bmff.ftyp = ftyp;
2020-01-12 03:34:29 +00:00
}
BoxType::FreeBox => {
2020-01-12 03:34:29 +00:00
start = 0;
}
BoxType::MdatBox => {
start = size - HEADER_SIZE;
2020-01-12 03:34:29 +00:00
}
BoxType::MoovBox => {
let moov = MoovBox::read_box(&mut reader, size)?;
2020-01-16 06:53:42 +00:00
bmff.moov = Some(moov);
2020-01-12 03:34:29 +00:00
}
BoxType::MoofBox => {
start = size - HEADER_SIZE;
2020-01-12 03:34:29 +00:00
}
_ => {
// Skip over unsupported boxes, but stop if the size is zero,
// meaning the last box has been reached.
if size == 0 {
break;
} else {
start = size - HEADER_SIZE;
}
}
2020-01-10 06:26:08 +00:00
};
2020-01-08 06:51:21 +00:00
}
2020-01-12 08:00:30 +00:00
Ok(bmff)
}
2020-01-08 06:51:21 +00:00
// TODO: if size is 0, then this box is the last one in the file
2020-07-25 02:57:46 +00:00
fn read_box_header<R: Read + Seek>(reader: &mut BufReader<R>, start: u64) -> Result<BoxHeader> {
2020-01-12 08:00:30 +00:00
// Seek to offset.
let _r = reader.seek(SeekFrom::Current(start as i64));
// Create and read to buf.
let mut buf = [0u8;8]; // 8 bytes for box header.
reader.read(&mut buf)?;
2020-01-12 08:00:30 +00:00
// Get size.
let s = buf[0..4].try_into().unwrap();
let size = u32::from_be_bytes(s);
// Get box type string.
let t = buf[4..8].try_into().unwrap();
let typ = u32::from_be_bytes(t);
2020-01-12 08:00:30 +00:00
// Get largesize if size is 1
if size == 1 {
reader.read(&mut buf)?;
let s = buf.try_into().unwrap();
let largesize = u64::from_be_bytes(s);
Ok(BoxHeader {
name: BoxType::from(typ),
size: largesize,
})
} else {
Ok(BoxHeader {
name: BoxType::from(typ),
size: size as u64,
})
}
2020-01-10 06:26:08 +00:00
}