Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(opus and fragments): add support for opus and writing fragmented mp4 files #114

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1101df4
expose TrakBox
sdwoodbury Jul 7, 2023
2fb21bb
expose more boxes
sdwoodbury Jul 12, 2023
b72a26b
feat(opus): add opus box
sdwoodbury Jul 13, 2023
09343a2
fix(opus): export opus
sdwoodbury Jul 13, 2023
c84183a
feat(opus): add opus to StsdBox and implement read/write
sdwoodbury Jul 13, 2023
9d00214
expose stsd box
sdwoodbury Jul 13, 2023
66f1770
expose mfhd box
sdwoodbury Jul 13, 2023
c669d7b
fix(TrafBox): write out tfdt and trun
sdwoodbury Jul 14, 2023
d5eec06
fix get_size() for trunbox
sdwoodbury Jul 14, 2023
cd86f1c
undo bugs introduced to TrunBox by previous commit(s)
sdwoodbury Jul 14, 2023
8ce5134
fix(moof): size returned by write_box
sdwoodbury Jul 14, 2023
23b8c86
fix(traf): box size didn't include tfdt
sdwoodbury Jul 14, 2023
90b9030
expose more boxes
sdwoodbury Jul 14, 2023
5d070eb
expose more boxes
sdwoodbury Jul 14, 2023
52e8a8b
expose StcoBox
sdwoodbury Jul 14, 2023
aeb75eb
fix(moovbox): write out mvex box.
sdwoodbury Jul 14, 2023
4b88f04
fix(trex): write trexbox and make it a vec
sdwoodbury Jul 14, 2023
4d676b9
fix(reader): didn't compile
sdwoodbury Jul 14, 2023
6a59176
fix(trex): didn't compile
sdwoodbury Jul 14, 2023
68c905d
fix(moov): update box size now that mvex is getting written
sdwoodbury Jul 14, 2023
c41654f
fix(mvex): wrong BoxType
sdwoodbury Jul 14, 2023
9887024
fix(opus): remove assert_eq
sdwoodbury Jul 17, 2023
11dbf72
Merge branch 'master' into feat/add-boxes
sdwoodbury Aug 3, 2023
b3ba124
fix(read_header): find correct default sample duration
sdwoodbury Aug 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/mp4dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ fn get_boxes(file: File) -> Result<Vec<Box>> {
if let Some(mehd) = &mvex.mehd {
boxes.push(build_box(mehd));
}
boxes.push(build_box(&mvex.trex));
for trex in &mvex.trex {
boxes.push(build_box(trex));
}
}

// trak.
Expand Down
29 changes: 28 additions & 1 deletion src/mp4box/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub(crate) mod moov;
pub(crate) mod mp4a;
pub(crate) mod mvex;
pub(crate) mod mvhd;
pub(crate) mod opus;
pub(crate) mod smhd;
pub(crate) mod stbl;
pub(crate) mod stco;
Expand All @@ -106,10 +107,33 @@ pub(crate) mod vmhd;
pub(crate) mod vp09;
pub(crate) mod vpcc;

pub use dinf::*;
pub use emsg::EmsgBox;
pub use ftyp::FtypBox;
pub use hdlr::HdlrBox;
pub use mdhd::MdhdBox;
pub use mdia::MdiaBox;
pub use mfhd::MfhdBox;
pub use minf::MinfBox;
pub use moof::MoofBox;
pub use moov::MoovBox;
pub use mvex::MvexBox;
pub use mvhd::MvhdBox;
pub use opus::*;
pub use smhd::SmhdBox;
pub use stbl::StblBox;
pub use stco::StcoBox;
pub use stsc::StscBox;
pub use stsd::StsdBox;
pub use stsz::StszBox;
pub use stts::SttsBox;
pub use tfdt::TfdtBox;
pub use tfhd::TfhdBox;
pub use tkhd::*;
pub use traf::TrafBox;
pub use trak::TrakBox;
pub use trex::TrexBox;
pub use trun::TrunBox;

pub const HEADER_SIZE: u64 = 8;
// const HEADER_LARGE_SIZE: u64 = 16;
Expand Down Expand Up @@ -198,7 +222,10 @@ boxtype! {
DayBox => 0xa9646179,
CovrBox => 0x636f7672,
DescBox => 0x64657363,
WideBox => 0x77696465
WideBox => 0x77696465,
DopsBox => 0x644F7073,
OpusBox => 0x4F707573

}

pub trait Mp4Box: Sized {
Expand Down
2 changes: 1 addition & 1 deletion src/mp4box/moof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ impl<W: Write> WriteBox<&mut W> for MoofBox {
for traf in self.trafs.iter() {
traf.write_box(writer)?;
}
Ok(0)
Ok(size)
}
}
22 changes: 12 additions & 10 deletions src/mp4box/moov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ impl MoovBox {

pub fn get_size(&self) -> u64 {
let mut size = HEADER_SIZE + self.mvhd.box_size();
size += self.meta.as_ref().map(|x| x.box_size()).unwrap_or(0);
size += self.mvex.as_ref().map(|x| x.box_size()).unwrap_or(0);

for trak in self.traks.iter() {
size += trak.box_size();
}
if let Some(meta) = &self.meta {
size += meta.box_size();
}
if let Some(udta) = &self.udta {
size += udta.box_size();
}

size += self.udta.as_ref().map(|x| x.box_size()).unwrap_or(0);
size
}
}
Expand Down Expand Up @@ -131,16 +130,19 @@ impl<W: Write> WriteBox<&mut W> for MoovBox {
BoxHeader::new(self.box_type(), size).write(writer)?;

self.mvhd.write_box(writer)?;
for trak in self.traks.iter() {
trak.write_box(writer)?;
}
if let Some(meta) = &self.meta {
meta.write_box(writer)?;
}
if let Some(mvex) = &self.mvex {
mvex.write_box(writer)?;
}
for trak in self.traks.iter() {
trak.write_box(writer)?;
}
if let Some(udta) = &self.udta {
udta.write_box(writer)?;
}
Ok(0)
Ok(size)
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/mp4box/mvex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ use crate::mp4box::{mehd::MehdBox, trex::TrexBox};
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct MvexBox {
pub mehd: Option<MehdBox>,
pub trex: TrexBox,
pub trex: Vec<TrexBox>,
}

impl MvexBox {
pub fn get_type(&self) -> BoxType {
BoxType::MdiaBox
BoxType::MvexBox
}

pub fn get_size(&self) -> u64 {
HEADER_SIZE + self.mehd.as_ref().map(|x| x.box_size()).unwrap_or(0) + self.trex.box_size()
let trex_sizes = self.trex.iter().fold(0, |acc, x| acc + x.box_size());
HEADER_SIZE + self.mehd.as_ref().map(|x| x.box_size()).unwrap_or(0) + trex_sizes
}
}

Expand Down Expand Up @@ -44,7 +45,7 @@ impl<R: Read + Seek> ReadBox<&mut R> for MvexBox {
let start = box_start(reader)?;

let mut mehd = None;
let mut trex = None;
let mut trex: Vec<TrexBox> = Vec::new();

let mut current = reader.stream_position()?;
let end = start + size;
Expand All @@ -63,7 +64,7 @@ impl<R: Read + Seek> ReadBox<&mut R> for MvexBox {
mehd = Some(MehdBox::read_box(reader, s)?);
}
BoxType::TrexBox => {
trex = Some(TrexBox::read_box(reader, s)?);
trex.push(TrexBox::read_box(reader, s)?);
}
_ => {
// XXX warn!()
Expand All @@ -74,16 +75,13 @@ impl<R: Read + Seek> ReadBox<&mut R> for MvexBox {
current = reader.stream_position()?;
}

if trex.is_none() {
if trex.is_empty() {
return Err(Error::BoxNotFound(BoxType::TrexBox));
}

skip_bytes_to(reader, start + size)?;

Ok(MvexBox {
mehd,
trex: trex.unwrap(),
})
Ok(MvexBox { mehd, trex })
}
}

Expand All @@ -95,7 +93,9 @@ impl<W: Write> WriteBox<&mut W> for MvexBox {
if let Some(mehd) = &self.mehd {
mehd.write_box(writer)?;
}
self.trex.write_box(writer)?;
for trex in &self.trex {
trex.write_box(writer)?;
}

Ok(size)
}
Expand Down
Loading
Loading