1
0
Fork 0
mirror of https://github.com/alfg/mp4-rust.git synced 2024-06-02 13:39:54 +00:00
This commit is contained in:
jensenn 2024-03-26 21:23:07 +00:00 committed by GitHub
commit c896f64a68
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 94 additions and 3 deletions

View file

@ -46,6 +46,7 @@
//! mehd //! mehd
//! trex //! trex
//! emsg //! emsg
//! uuid
//! moof //! moof
//! mfhd //! mfhd
//! traf //! traf
@ -102,6 +103,7 @@ pub(crate) mod trex;
pub(crate) mod trun; pub(crate) mod trun;
pub(crate) mod tx3g; pub(crate) mod tx3g;
pub(crate) mod udta; pub(crate) mod udta;
pub(crate) mod uuid;
pub(crate) mod vmhd; pub(crate) mod vmhd;
pub(crate) mod vp09; pub(crate) mod vp09;
pub(crate) mod vpcc; pub(crate) mod vpcc;
@ -146,6 +148,7 @@ pub use trex::TrexBox;
pub use trun::TrunBox; pub use trun::TrunBox;
pub use tx3g::Tx3gBox; pub use tx3g::Tx3gBox;
pub use udta::UdtaBox; pub use udta::UdtaBox;
pub use uuid::UuidBox;
pub use vmhd::VmhdBox; pub use vmhd::VmhdBox;
pub use vp09::Vp09Box; pub use vp09::Vp09Box;
pub use vpcc::VpccBox; pub use vpcc::VpccBox;
@ -238,7 +241,8 @@ boxtype! {
CovrBox => 0x636f7672, CovrBox => 0x636f7672,
DescBox => 0x64657363, DescBox => 0x64657363,
WideBox => 0x77696465, WideBox => 0x77696465,
WaveBox => 0x77617665 WaveBox => 0x77617665,
UuidBox => 0x75756964
} }
pub trait Mp4Box: Sized { pub trait Mp4Box: Sized {

74
src/mp4box/uuid.rs Normal file
View file

@ -0,0 +1,74 @@
use serde::Serialize;
use std::io::{Read, Seek, Write};
use crate::mp4box::*;
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct UuidBox {
pub extended_type: [u8; 16],
pub data: Vec<u8>,
}
impl UuidBox {
pub fn get_type(&self) -> BoxType {
BoxType::UuidBox
}
pub fn get_size(&self) -> u64 {
HEADER_SIZE + 16 + self.data.len() as u64
}
}
impl Mp4Box for UuidBox {
fn box_type(&self) -> BoxType {
self.get_type()
}
fn box_size(&self) -> u64 {
self.get_size()
}
fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}
fn summary(&self) -> Result<String> {
let s = format!("extended_type: {:02x?}", self.extended_type);
Ok(s)
}
}
impl<R: Read + Seek> ReadBox<&mut R> for UuidBox {
fn read_box(reader: &mut R, size: u64) -> Result<Self> {
let start = box_start(reader)?;
let mut extended_type = [0; 16];
reader.read_exact(&mut extended_type)?;
let data_size = (start + size)
.checked_sub(reader.stream_position()?)
.ok_or(Error::InvalidData("uuid size too small"))?;
let mut data = vec![0; data_size as usize];
reader.read_exact(&mut data)?;
skip_bytes_to(reader, start + size)?;
Ok(UuidBox {
extended_type,
data,
})
}
}
impl<W: Write> WriteBox<&mut W> for UuidBox {
fn write_box(&self, writer: &mut W) -> Result<u64> {
let size = self.box_size();
BoxHeader::new(self.box_type(), size).write(writer)?;
writer.write_all(&self.extended_type)?;
writer.write_all(&self.data)?;
Ok(size)
}
}

View file

@ -12,6 +12,7 @@ pub struct Mp4Reader<R> {
pub moov: MoovBox, pub moov: MoovBox,
pub moofs: Vec<MoofBox>, pub moofs: Vec<MoofBox>,
pub emsgs: Vec<EmsgBox>, pub emsgs: Vec<EmsgBox>,
pub uuids: Vec<UuidBox>,
tracks: HashMap<u32, Mp4Track>, tracks: HashMap<u32, Mp4Track>,
size: u64, size: u64,
@ -26,6 +27,7 @@ impl<R: Read + Seek> Mp4Reader<R> {
let mut moofs = Vec::new(); let mut moofs = Vec::new();
let mut moof_offsets = Vec::new(); let mut moof_offsets = Vec::new();
let mut emsgs = Vec::new(); let mut emsgs = Vec::new();
let mut uuids = Vec::new();
let mut current = start; let mut current = start;
while current < size { while current < size {
@ -67,6 +69,10 @@ impl<R: Read + Seek> Mp4Reader<R> {
let emsg = EmsgBox::read_box(&mut reader, s)?; let emsg = EmsgBox::read_box(&mut reader, s)?;
emsgs.push(emsg); emsgs.push(emsg);
} }
BoxType::UuidBox => {
let uuid = UuidBox::read_box(&mut reader, s)?;
uuids.push(uuid);
}
_ => { _ => {
// XXX warn!() // XXX warn!()
skip_box(&mut reader, s)?; skip_box(&mut reader, s)?;
@ -124,6 +130,7 @@ impl<R: Read + Seek> Mp4Reader<R> {
moov: moov.unwrap(), moov: moov.unwrap(),
moofs, moofs,
emsgs, emsgs,
uuids,
size, size,
tracks, tracks,
}) })
@ -138,6 +145,7 @@ impl<R: Read + Seek> Mp4Reader<R> {
let mut moofs = Vec::new(); let mut moofs = Vec::new();
let mut moof_offsets = Vec::new(); let mut moof_offsets = Vec::new();
let mut uuids = Vec::new();
let mut current = start; let mut current = start;
while current < size { while current < size {
@ -166,6 +174,10 @@ impl<R: Read + Seek> Mp4Reader<R> {
moofs.push(moof); moofs.push(moof);
moof_offsets.push(moof_offset); moof_offsets.push(moof_offset);
} }
BoxType::UuidBox => {
let uuid = UuidBox::read_box(&mut reader, s)?;
uuids.push(uuid);
}
_ => { _ => {
// XXX warn!() // XXX warn!()
skip_box(&mut reader, s)?; skip_box(&mut reader, s)?;
@ -210,6 +222,7 @@ impl<R: Read + Seek> Mp4Reader<R> {
moov: self.moov.clone(), moov: self.moov.clone(),
moofs, moofs,
emsgs: Vec::new(), emsgs: Vec::new(),
uuids,
tracks, tracks,
size, size,
}) })

View file

@ -261,7 +261,7 @@ impl Mp4Track {
pub fn sequence_parameter_set(&self) -> Result<&[u8]> { pub fn sequence_parameter_set(&self) -> Result<&[u8]> {
if let Some(ref avc1) = self.trak.mdia.minf.stbl.stsd.avc1 { if let Some(ref avc1) = self.trak.mdia.minf.stbl.stsd.avc1 {
match avc1.avcc.sequence_parameter_sets.get(0) { match avc1.avcc.sequence_parameter_sets.first() {
Some(nal) => Ok(nal.bytes.as_ref()), Some(nal) => Ok(nal.bytes.as_ref()),
None => Err(Error::EntryInStblNotFound( None => Err(Error::EntryInStblNotFound(
self.track_id(), self.track_id(),
@ -276,7 +276,7 @@ impl Mp4Track {
pub fn picture_parameter_set(&self) -> Result<&[u8]> { pub fn picture_parameter_set(&self) -> Result<&[u8]> {
if let Some(ref avc1) = self.trak.mdia.minf.stbl.stsd.avc1 { if let Some(ref avc1) = self.trak.mdia.minf.stbl.stsd.avc1 {
match avc1.avcc.picture_parameter_sets.get(0) { match avc1.avcc.picture_parameter_sets.first() {
Some(nal) => Ok(nal.bytes.as_ref()), Some(nal) => Ok(nal.bytes.as_ref()),
None => Err(Error::EntryInStblNotFound( None => Err(Error::EntryInStblNotFound(
self.track_id(), self.track_id(),