1
0
Fork 0
mirror of https://github.com/alfg/mp4-rust.git synced 2024-05-19 16:58:04 +00:00
This commit is contained in:
Alf 2020-01-27 21:58:19 -08:00
parent e9936e5f78
commit 4a8c4f57b4
2 changed files with 12 additions and 96 deletions

View file

@ -2,9 +2,6 @@ extern crate mp4;
use std::env;
use std::fs::File;
use std::any::Any;
use std::borrow::Borrow;
use std::fmt::Debug;
use mp4::{TrackType};
fn main() {
@ -48,13 +45,13 @@ fn main() {
println!(" sample count: {:?}", stts.sample_counts[0]);
println!(" timescale: {:?}", mdhd.timescale);
println!(" duration: {:?} (media timescale units)", mdhd.duration);
println!(" duration: {:?} (ms)", getDurationMS(mdhd.duration, mdhd.timescale));
println!(" duration: {:?} (ms)", get_duration_ms(mdhd.duration, mdhd.timescale));
if tkhd.width != 0 && tkhd.height != 0 {
println!(" width: {:?}", tkhd.width);
println!(" height: {:?}", tkhd.height);
}
if get_handler_type(hdlr.handler_type.value.as_ref()) == TrackType::Video {
println!(" frame rate: (computed): {:?}", getFramerate(&stts.sample_counts, mdhd.duration, mdhd.timescale));
println!(" frame rate: (computed): {:?}", get_framerate(&stts.sample_counts, mdhd.duration, mdhd.timescale));
}
}
},
@ -75,12 +72,12 @@ fn get_handler_type(handler: &str) -> TrackType {
return typ;
}
fn getDurationMS(duration: u32, timescale: u32) -> String {
fn get_duration_ms(duration: u32, timescale: u32) -> String {
let ms = (duration as f64 / timescale as f64) * 1000.0;
return format!("{:.2}", ms.floor());
}
fn getFramerate(sample_counts: &Vec<u32>, duration: u32, timescale: u32) -> String {
fn get_framerate(sample_counts: &Vec<u32>, duration: u32, timescale: u32) -> String {
let sc = (sample_counts[0] as f64) * 1000.0;
let ms = (duration as f64 / timescale as f64) * 1000.0;
return format!("{:.2}", sc / ms.floor());

View file

@ -145,12 +145,6 @@ pub struct ElstBox {
pub entries: Vec<ElstEntry>,
}
impl ElstBox {
fn new() -> ElstBox {
Default::default()
}
}
#[derive(Debug, Default)]
pub struct ElstEntry {
pub segment_duration: u32,
@ -184,12 +178,6 @@ pub struct MdhdBox {
pub language_string: String,
}
impl MdhdBox {
fn new() -> MdhdBox {
Default::default()
}
}
#[derive(Debug, Default)]
pub struct HdlrBox {
pub version: u8,
@ -198,12 +186,6 @@ pub struct HdlrBox {
pub name: String,
}
impl HdlrBox {
fn new() -> HdlrBox {
Default::default()
}
}
#[derive(Debug, Default)]
pub struct MinfBox {
pub vmhd: Option<VmhdBox>,
@ -224,12 +206,6 @@ pub struct VmhdBox {
pub op_color: u16,
}
impl VmhdBox {
fn new() -> VmhdBox {
Default::default()
}
}
#[derive(Debug, Default)]
pub struct StblBox {
pub stts: Option<SttsBox>,
@ -251,25 +227,12 @@ pub struct SttsBox {
pub sample_deltas: Vec<u32>,
}
impl SttsBox {
fn new() -> SttsBox {
Default::default()
}
}
#[derive(Debug, Default)]
pub struct StsdBox {
pub version: u8,
pub flags: u32,
}
impl StsdBox {
fn new() -> StsdBox {
Default::default()
}
}
#[derive(Default, PartialEq, Clone)]
pub struct FourCC {
pub value: String
@ -342,7 +305,6 @@ fn read_boxes(f: File) -> Result<BMFF> {
start = (size as u32 - HEADER_SIZE) as u64;
}
"moov" => {
// start = (size as u32 - HEADER_SIZE) as u64;
let moov = parse_moov_box(&mut reader, 0, size as u32).unwrap();
bmff.moov = Some(moov);
}
@ -434,10 +396,8 @@ fn parse_moov_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<Mo
"trak" => {
let trak = parse_trak_box(f, 0, s as u32).unwrap();
moov.traks.push(trak);
// start = (s as u32 - HEADER_SIZE) as u64;
}
"udta" => {
println!("found udta");
start = (s as u32 - HEADER_SIZE) as u64;
}
_ => break
@ -503,10 +463,8 @@ fn parse_trak_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<Tr
trak.edts = Some(edts);
}
"mdia" => {
println!("found mdia");
let mdia = parse_mdia_box(f, 0, s as u32).unwrap();
trak.mdia = Some(mdia);
// start = (s as u32 - HEADER_SIZE) as u64;
}
_ => break
}
@ -760,7 +718,6 @@ fn parse_hdlr_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<Hd
}
fn parse_minf_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<MinfBox> {
println!("size: {:?}", size);
let current = f.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
let mut minf = MinfBox::new();
@ -779,26 +736,18 @@ fn parse_minf_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<Mi
match b.head.name.as_ref() {
"vmhd" => {
println!("found vmhd");
let vmhd = parse_vmhd_box(f, 0, s as u32).unwrap();
minf.vmhd = Some(vmhd);
// start = (s as u32 - HEADER_SIZE) as u64;
}
"smhd" => {
println!("found smhd");
//// let vmhd = parse_smhd_box(f, 0, s as u32).unwrap();
//// minf.smhd = Some(vmhd);
start = (s as u32 - HEADER_SIZE) as u64;
}
"dinf" => {
println!("found dinf");
start = (s as u32 - HEADER_SIZE) as u64;
}
"stbl" => {
println!("found stbl");
let stbl = parse_stbl_box(f, 0, s as u32).unwrap();
minf.stbl = Some(stbl);
// start = (s as u32 - HEADER_SIZE) as u64;
}
_ => break
}
@ -837,11 +786,9 @@ fn parse_vmhd_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<Vm
}
fn parse_stbl_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<StblBox> {
println!("stbl size: {:?}", size);
let current = f.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
let mut stbl = StblBox::new();
let mut start = 0u64;
let start = 0u64;
while start < size as u64 {
// Get box header.
let header = read_box_header(f, start).unwrap();
@ -856,42 +803,16 @@ fn parse_stbl_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<St
match b.head.name.as_ref() {
"stsd" => {
println!("found stsd: {:?}", s);
// let stsd = parse_stsd_box(f, 0, s as u32).unwrap();
start = (s as u32 - HEADER_SIZE) as u64;
let stsd = parse_stsd_box(f, 0, s as u32).unwrap();
stbl.stsd = Some(stsd);
}
"stts" => {
let stts = parse_stts_box(f, 0, s as u32).unwrap();
stbl.stts = Some(stts);
}
"stss" => {
println!("found stss");
start = (s as u32 - HEADER_SIZE) as u64;
}
"ctts" => {
println!("found ctts");
start = (s as u32 - HEADER_SIZE) as u64;
}
"stsc" => {
println!("found stsc");
start = (s as u32 - HEADER_SIZE) as u64;
}
"stsz" => {
println!("found stsz");
start = (s as u32 - HEADER_SIZE) as u64;
}
"stco" => {
println!("found stco");
start = (s as u32 - HEADER_SIZE) as u64;
}
_ => break
}
}
// Skip remaining bytes.
// let after = f.seek(SeekFrom::Current(0)).unwrap();
// let remaining_bytes = (size as u64 - (after - current)) as i64;
// f.seek(SeekFrom::Current(remaining_bytes - HEADER_SIZE as i64)).unwrap();
Ok(stbl)
}
@ -938,7 +859,6 @@ fn parse_stsd_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<St
let flags = u32::from(flags_a) << 16 | u32::from(flags_b) << 8 | u32::from(flags_c);
f.read_u32::<BigEndian>().unwrap(); // skip.
let mut start = 0u64;
while start < size as u64 {
// Get box header.
@ -954,11 +874,11 @@ fn parse_stsd_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<St
match b.head.name.as_ref() {
"avc1" => {
println!("found avc1");
// println!("found avc1");
start = (s as u32 - HEADER_SIZE) as u64;
}
"mp4a" => {
println!("found mp4a");
// println!("found mp4a");
start = (s as u32 - HEADER_SIZE) as u64;
}
_ => break
@ -966,10 +886,9 @@ fn parse_stsd_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<St
}
// Skip remaining bytes.
// let after = f.seek(SeekFrom::Current(0)).unwrap();
// let remaining_bytes = (size as u64 - (after - current)) as i64;
// f.seek(SeekFrom::Current(remaining_bytes - HEADER_SIZE as i64)).unwrap();
let after = f.seek(SeekFrom::Current(0)).unwrap();
let remaining_bytes = (size as u64 - (after - current)) as i64;
f.seek(SeekFrom::Current(remaining_bytes - HEADER_SIZE as i64)).unwrap();
Ok(StsdBox {
version,
flags,