Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: alfg/mp4-rust
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.12.0
Choose a base ref
...
head repository: alfg/mp4-rust
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 0.13.0
Choose a head ref
  • 8 commits
  • 50 files changed
  • 6 contributors

Commits on Jan 6, 2023

  1. fix "mdat size too large" (#80)

    * fix "mdat size too large"
    
    * fix clippy
    
    * Fix clippy::derive-partial-eq-without-eq
    rolleifx authored Jan 6, 2023
    Copy the full SHA
    c26bdca View commit details
  2. Fix clippy warnings (#88)

    * chore: fix clippy warnings
    
    * Update readme with clippy usage.
    
    * fix fmt warnings.
    alfg authored Jan 6, 2023
    Copy the full SHA
    3095051 View commit details
  3. Fix TfhdBox optional fields (#86)

    * Fix TfhdBox optional fields
    
    * clippy fixes
    
    * Cargo fmt fix
    
    Co-authored-by: Alfred Gutierrez <alfg@users.noreply.github.com>
    jensenn and alfg authored Jan 6, 2023
    Copy the full SHA
    df6e0b5 View commit details

Commits on Jan 10, 2023

  1. Don't subtract overflow when 64-bit box size is less than 8 (#89)

    * don't subtract overflow when 64-bit box size is less than 8
    
    * fix largesize of 8 being conflated with a size of 0
    jessa0 authored Jan 10, 2023
    Copy the full SHA
    16c1b5d View commit details

Commits on Jan 12, 2023

  1. Feature/tfdt box (#90)

    * Add Tfdt box parsing
    
    * Derive Default for TfdtBox
    
    * Derive Eq for TfdtBox
    jensenn authored Jan 12, 2023
    Copy the full SHA
    9c0f653 View commit details

Commits on Jan 31, 2023

  1. Parse unknown metadata and write metadata (#91)

    * validate meta box handler type, parse meta with unknown handlers
    
    * parse meta in moov and trak position
    
    * write meta and udta
    
    * fix clippy nit
    jessa0 authored Jan 31, 2023
    Copy the full SHA
    2a374c5 View commit details
  2. Copy the full SHA
    9f10bd3 View commit details

Commits on Feb 2, 2023

  1. 0.13.0

    alfg committed Feb 2, 2023
    Copy the full SHA
    c8cc6f2 View commit details
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mp4"
version = "0.12.0"
version = "0.13.0"
authors = ["Alf <alf.g.jr@gmail.com>"]
edition = "2018"
description = "MP4 reader and writer library in Rust."
@@ -10,6 +10,7 @@ homepage = "https://github.com/alfg/mp4-rust"
repository = "https://github.com/alfg/mp4-rust"
keywords = ["mp4", "iso-mp4", "isobmff", "video", "multimedia"]
license = "MIT"
include = ["src", "Cargo.toml", "README", "LICENSE"]

[dependencies]
thiserror = "^1.0"
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -58,9 +58,12 @@ fn main() -> Result<()> {
See [examples/](examples/) for more examples.

#### Install
Add to your `Cargo.toml`:
```
cargo add mp4
```
or add to your `Cargo.toml`:
```toml
mp4 = "0.12.0"
mp4 = "0.13.0"
```

#### Documentation
@@ -103,6 +106,22 @@ With print statement output.
cargo test -- --nocapture
```

#### Run Cargo fmt
Run fmt to catch formatting errors.

```
rustup component add rustfmt
cargo fmt --all -- --check
```

#### Run Clippy
Run Clippy tests to catch common lints and mistakes.

```
rustup component add clippy
cargo clippy --no-deps -- -D warnings
```

#### Run Benchmark Tests
```
cargo bench
6 changes: 3 additions & 3 deletions src/mp4box/avc1.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use std::io::{Read, Seek, Write};

use crate::mp4box::*;

#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Avc1Box {
pub data_reference_index: u16,
pub width: u16,
@@ -153,7 +153,7 @@ impl<W: Write> WriteBox<&mut W> for Avc1Box {
}
}

#[derive(Debug, Clone, PartialEq, Default, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct AvcCBox {
pub configuration_version: u8,
pub avc_profile_indication: u8,
@@ -262,7 +262,7 @@ impl<W: Write> WriteBox<&mut W> for AvcCBox {
}
}

#[derive(Debug, Clone, PartialEq, Default, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct NalUnit {
pub bytes: Vec<u8>,
}
2 changes: 1 addition & 1 deletion src/mp4box/co64.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use std::io::{Read, Seek, Write};

use crate::mp4box::*;

#[derive(Debug, Clone, PartialEq, Default, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct Co64Box {
pub version: u8,
pub flags: u32,
4 changes: 2 additions & 2 deletions src/mp4box/ctts.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use std::io::{Read, Seek, Write};

use crate::mp4box::*;

#[derive(Debug, Clone, PartialEq, Default, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct CttsBox {
pub version: u8,
pub flags: u32,
@@ -23,7 +23,7 @@ impl CttsBox {
}
}

#[derive(Debug, Clone, PartialEq, Default, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct CttsEntry {
pub sample_count: u32,
pub sample_offset: i32,
92 changes: 90 additions & 2 deletions src/mp4box/data.rs
Original file line number Diff line number Diff line change
@@ -7,12 +7,45 @@ use serde::Serialize;

use crate::mp4box::*;

#[derive(Debug, Clone, PartialEq, Default, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct DataBox {
pub data: Vec<u8>,
pub data_type: DataType,
}

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

pub fn get_size(&self) -> u64 {
let mut size = HEADER_SIZE;
size += 4; // data_type
size += 4; // reserved
size += self.data.len() as u64;
size
}
}

impl Mp4Box for DataBox {
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!("type={:?} len={}", self.data_type, self.data.len());
Ok(s)
}
}

impl<R: Read + Seek> ReadBox<&mut R> for DataBox {
fn read_box(reader: &mut R, size: u64) -> Result<Self> {
let start = box_start(reader)?;
@@ -21,10 +54,65 @@ impl<R: Read + Seek> ReadBox<&mut R> for DataBox {

reader.read_u32::<BigEndian>()?; // reserved = 0

let current = reader.seek(SeekFrom::Current(0))?;
let current = reader.stream_position()?;
let mut data = vec![0u8; (start + size - current) as usize];
reader.read_exact(&mut data)?;

Ok(DataBox { data, data_type })
}
}

impl<W: Write> WriteBox<&mut W> for DataBox {
fn write_box(&self, writer: &mut W) -> Result<u64> {
let size = self.box_size();
BoxHeader::new(self.box_type(), size).write(writer)?;

writer.write_u32::<BigEndian>(self.data_type.clone() as u32)?;
writer.write_u32::<BigEndian>(0)?; // reserved = 0
writer.write_all(&self.data)?;

Ok(size)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::mp4box::BoxHeader;
use std::io::Cursor;

#[test]
fn test_data() {
let src_box = DataBox {
data_type: DataType::Text,
data: b"test_data".to_vec(),
};
let mut buf = Vec::new();
src_box.write_box(&mut buf).unwrap();
assert_eq!(buf.len(), src_box.box_size() as usize);

let mut reader = Cursor::new(&buf);
let header = BoxHeader::read(&mut reader).unwrap();
assert_eq!(header.name, BoxType::DataBox);
assert_eq!(src_box.box_size(), header.size);

let dst_box = DataBox::read_box(&mut reader, header.size).unwrap();
assert_eq!(src_box, dst_box);
}

#[test]
fn test_data_empty() {
let src_box = DataBox::default();
let mut buf = Vec::new();
src_box.write_box(&mut buf).unwrap();
assert_eq!(buf.len(), src_box.box_size() as usize);

let mut reader = Cursor::new(&buf);
let header = BoxHeader::read(&mut reader).unwrap();
assert_eq!(header.name, BoxType::DataBox);
assert_eq!(src_box.box_size(), header.size);

let dst_box = DataBox::read_box(&mut reader, header.size).unwrap();
assert_eq!(src_box, dst_box);
}
}
14 changes: 7 additions & 7 deletions src/mp4box/dinf.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use std::io::{Read, Seek, Write};

use crate::mp4box::*;

#[derive(Debug, Clone, PartialEq, Default, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct DinfBox {
dref: DrefBox,
}
@@ -43,7 +43,7 @@ impl<R: Read + Seek> ReadBox<&mut R> for DinfBox {

let mut dref = None;

let mut current = reader.seek(SeekFrom::Current(0))?;
let mut current = reader.stream_position()?;
let end = start + size;
while current < end {
// Get box header.
@@ -60,7 +60,7 @@ impl<R: Read + Seek> ReadBox<&mut R> for DinfBox {
}
}

current = reader.seek(SeekFrom::Current(0))?;
current = reader.stream_position()?;
}

if dref.is_none() {
@@ -84,7 +84,7 @@ impl<W: Write> WriteBox<&mut W> for DinfBox {
}
}

#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct DrefBox {
pub version: u8,
pub flags: u32,
@@ -140,7 +140,7 @@ impl<R: Read + Seek> ReadBox<&mut R> for DrefBox {
fn read_box(reader: &mut R, size: u64) -> Result<Self> {
let start = box_start(reader)?;

let mut current = reader.seek(SeekFrom::Current(0))?;
let mut current = reader.stream_position()?;

let (version, flags) = read_box_header_ext(reader)?;
let end = start + size;
@@ -166,7 +166,7 @@ impl<R: Read + Seek> ReadBox<&mut R> for DrefBox {
}
}

current = reader.seek(SeekFrom::Current(0))?;
current = reader.stream_position()?;
}

skip_bytes_to(reader, start + size)?;
@@ -196,7 +196,7 @@ impl<W: Write> WriteBox<&mut W> for DrefBox {
}
}

#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct UrlBox {
pub version: u8,
pub flags: u32,
2 changes: 1 addition & 1 deletion src/mp4box/edts.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use std::io::{Read, Seek, Write};
use crate::mp4box::elst::ElstBox;
use crate::mp4box::*;

#[derive(Debug, Clone, PartialEq, Default, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct EdtsBox {
pub elst: Option<ElstBox>,
}
4 changes: 2 additions & 2 deletions src/mp4box/elst.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use std::io::{Read, Seek, Write};

use crate::mp4box::*;

#[derive(Debug, Clone, PartialEq, Default, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct ElstBox {
pub version: u8,
pub flags: u32,
@@ -13,7 +13,7 @@ pub struct ElstBox {
pub entries: Vec<ElstEntry>,
}

#[derive(Debug, Clone, PartialEq, Default, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct ElstEntry {
pub segment_duration: u64,
pub media_time: u64,
4 changes: 2 additions & 2 deletions src/mp4box/emsg.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ use serde::Serialize;

use crate::mp4box::*;

#[derive(Debug, Clone, PartialEq, Default, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct EmsgBox {
pub version: u8,
pub flags: u32,
@@ -26,7 +26,7 @@ impl EmsgBox {
4 + // id
Self::time_size(version) +
(scheme_id_uri.len() + 1) as u64 +
(value.len() as u64 + 1) as u64
(value.len() as u64 + 1)
}

fn time_size(version: u8) -> u64 {
2 changes: 1 addition & 1 deletion src/mp4box/ftyp.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use std::io::{Read, Seek, Write};

use crate::mp4box::*;

#[derive(Debug, Clone, PartialEq, Default, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct FtypBox {
pub major_brand: FourCC,
pub minor_version: u32,
2 changes: 1 addition & 1 deletion src/mp4box/hdlr.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use std::io::{Read, Seek, Write};

use crate::mp4box::*;

#[derive(Debug, Clone, PartialEq, Default, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct HdlrBox {
pub version: u8,
pub flags: u32,
4 changes: 2 additions & 2 deletions src/mp4box/hev1.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use std::io::{Read, Seek, Write};

use crate::mp4box::*;

#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Hev1Box {
pub data_reference_index: u16,
pub width: u16,
@@ -153,7 +153,7 @@ impl<W: Write> WriteBox<&mut W> for Hev1Box {
}
}

#[derive(Debug, Clone, PartialEq, Default, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct HvcCBox {
pub configuration_version: u8,
}
Loading