mp4box 0.8.0

Minimal MP4/ISOBMFF parser with JSON output, box decoding, and UUID support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use clap::Parser;
use mp4box::{Box, get_boxes};
use serde::Serialize;
use std::path::PathBuf;

#[derive(Parser, Debug)]
#[command(version, about = "Simple MP4 media info (like mp4info)")]
struct Args {
    /// MP4/ISOBMFF file path
    path: String,

    /// Output as JSON instead of human-readable text
    #[arg(long)]
    json: bool,
}

#[derive(Debug, Serialize)]
struct TrackInfo {
    index: usize,

    #[serde(skip_serializing_if = "Option::is_none")]
    track_type: Option<String>, // "video" / "audio" / "other"

    #[serde(skip_serializing_if = "Option::is_none")]
    codec: Option<String>, // e.g. "avc1", "hvc1", "mp4a"

    #[serde(skip_serializing_if = "Option::is_none")]
    width: Option<u32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    height: Option<u32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    timescale: Option<u32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    duration_ticks: Option<u64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    duration_seconds: Option<f64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    language: Option<String>,
}

#[derive(Debug, Serialize)]
struct MediaInfo {
    file: String,

    #[serde(skip_serializing_if = "Option::is_none")]
    major_brand: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    minor_version: Option<u32>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    compatible_brands: Vec<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    movie_timescale: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    movie_duration_ticks: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    movie_duration_seconds: Option<f64>,

    #[serde(skip_serializing_if = "Vec::is_empty")]
    tracks: Vec<TrackInfo>,
}

fn main() -> anyhow::Result<()> {
    let args = Args::parse();
    let path = PathBuf::from(&args.path);

    let mut file = std::fs::File::open(&path)?;
    let size = file.metadata()?.len();

    let boxes = get_boxes(&mut file, size, /*decode=*/ true)?;
    let mut info = MediaInfo {
        file: path.display().to_string(),
        major_brand: None,
        minor_version: None,
        compatible_brands: Vec::new(),
        movie_timescale: None,
        movie_duration_ticks: None,
        movie_duration_seconds: None,
        tracks: Vec::new(),
    };

    // Walk top-level boxes: ftyp, moov, etc.
    for b in &boxes {
        match b.typ.as_str() {
            "ftyp" => parse_ftyp(b, &mut info),
            "moov" => parse_moov(b, &mut info),
            _ => {}
        }
    }

    if args.json {
        println!("{}", serde_json::to_string_pretty(&info)?);
    } else {
        print_human(&info);
    }

    Ok(())
}

fn parse_ftyp(b: &Box, info: &mut MediaInfo) {
    let decoded = match &b.decoded {
        Some(s) => s,
        None => return,
    };

    // Example decoded string:
    // "major=isom minor=512 compatible=[\"isom\", \"iso2\", \"avc1\", \"mp41\"]"
    if let Some(major) = parse_string_field(decoded, "major=") {
        info.major_brand = Some(major);
    }
    if let Some(minor) = parse_u32_field(decoded, "minor=") {
        info.minor_version = Some(minor);
    }
    if let Some(compat) = parse_compatible_brands(decoded) {
        info.compatible_brands = compat;
    }
}

fn parse_moov(b: &Box, info: &mut MediaInfo) {
    let children = match &b.children {
        Some(c) => c,
        None => return,
    };

    // mvhd for overall movie duration
    if let Some(mvhd) = children.iter().find(|c| c.typ == "mvhd")
        && let Some(decoded) = &mvhd.decoded
    {
        // Example: "timescale=600000 duration=65536"
        if let Some(ts) = parse_u32_field(decoded, "timescale=") {
            info.movie_timescale = Some(ts);
        }
        if let Some(dur) = parse_u64_field(decoded, "duration=") {
            info.movie_duration_ticks = Some(dur);
            if let Some(ts) = info.movie_timescale {
                info.movie_duration_seconds = Some(dur as f64 / ts as f64);
            }
        }
    }

    // trak boxes for per-track timing
    for (i, trak) in children.iter().filter(|c| c.typ == "trak").enumerate() {
        parse_trak(trak, i + 1, info);
    }
}

fn parse_trak(trak: &Box, index: usize, info: &mut MediaInfo) {
    let mut ti = TrackInfo {
        index,
        track_type: None,
        codec: None,
        width: None,
        height: None,
        timescale: None,
        duration_ticks: None,
        duration_seconds: None,
        language: None,
    };

    // tkhd at the trak level: possible width/height
    if let Some(tkhd) = find_child(trak, "tkhd")
        && let Some(decoded) = &tkhd.decoded
    {
        // For “normal” tkhd decoders you’ll get something like:
        // "track_id=1 duration=... width=1920 height=1080"
        if let Some(w) = parse_u32_field(decoded, "width=") {
            ti.width = Some(w);
        }
        if let Some(h) = parse_u32_field(decoded, "height=") {
            ti.height = Some(h);
        }
    }

    // mdia -> mdhd + hdlr + minf
    let mdia = match find_child(trak, "mdia") {
        Some(m) => m,
        None => {
            info.tracks.push(ti);
            return;
        }
    };

    // mdhd: timescale / duration / language
    if let Some(mdhd) = find_child(mdia, "mdhd") {
        // Try structured data first
        if let Some(mp4box::registry::StructuredData::MediaHeader(mdhd_data)) =
            &mdhd.structured_data
        {
            ti.timescale = Some(mdhd_data.timescale);
            ti.duration_ticks = Some(mdhd_data.duration as u64);
            ti.duration_seconds = Some(mdhd_data.duration as f64 / mdhd_data.timescale as f64);
            ti.language = Some(mdhd_data.language.clone());
        }
        // Fallback to text parsing
        else if let Some(decoded) = &mdhd.decoded {
            if let Some(ts) = parse_u32_field(decoded, "timescale=") {
                ti.timescale = Some(ts);
            }
            if let Some(dur) = parse_u64_field(decoded, "duration=") {
                ti.duration_ticks = Some(dur);
                if let Some(ts) = ti.timescale {
                    ti.duration_seconds = Some(dur as f64 / ts as f64);
                }
            }
            if let Some(lang) = parse_string_field(decoded, "language=") {
                ti.language = Some(lang);
            }
        }
    }

    // hdlr: determine track type (video/audio/other)
    if let Some(hdlr) = find_child(mdia, "hdlr") {
        // Try structured data first
        if let Some(mp4box::registry::StructuredData::HandlerReference(hdlr_data)) =
            &hdlr.structured_data
        {
            let tt = match hdlr_data.handler_type.as_str() {
                "vide" => "video",
                "soun" => "audio",
                _ => "other",
            };
            ti.track_type = Some(tt.to_string());
        }
        // Fallback to text parsing
        else if let Some(decoded) = &hdlr.decoded {
            // Ideally your hdlr decoder now prints "handler=vide name=..."
            if let Some(handler) = parse_string_field(decoded, "handler=") {
                let tt = match handler.as_str() {
                    "vide" => "video",
                    "soun" => "audio",
                    _ => "other",
                };
                ti.track_type = Some(tt.to_string());
            }
        }
    }

    // minf -> stbl -> stsd: codec + width/height from decoded text
    if let Some(minf) = find_child(mdia, "minf")
        && let Some(stbl) = find_child(minf, "stbl")
        && let Some(stsd) = find_child(stbl, "stsd")
        && let Some(decoded) = &stsd.decoded
    {
        // codec
        if let Some(c) = parse_string_field(decoded, "codec=") {
            ti.codec = Some(c.clone());

            // If no type from hdlr, infer from codec
            if ti.track_type.is_none() {
                let tt = match c.as_str() {
                    "avc1" | "hvc1" | "hev1" | "vp09" | "av01" => "video",
                    "mp4a" | "ac-3" | "ec-3" | "Opus" => "audio",
                    _ => "other",
                };
                ti.track_type = Some(tt.to_string());
            }
        }

        // width / height (for video)
        if let Some(w) = parse_u32_field(decoded, "width=") {
            ti.width = Some(w);
        }
        if let Some(h) = parse_u32_field(decoded, "height=") {
            ti.height = Some(h);
        }
    }

    info.tracks.push(ti);
}

fn find_child<'a>(parent: &'a Box, typ: &str) -> Option<&'a Box> {
    parent
        .children
        .as_ref()
        .and_then(|kids| kids.iter().find(|c| c.typ == typ))
}

// ---- tiny string parsers over the `decoded` text --------------------

fn parse_u32_field(s: &str, key: &str) -> Option<u32> {
    parse_u64_field(s, key).and_then(|v| u32::try_from(v).ok())
}

fn parse_u64_field(s: &str, key: &str) -> Option<u64> {
    let idx = s.find(key)?;
    let rest = &s[idx + key.len()..];
    let digits: String = rest.chars().take_while(|c| c.is_ascii_digit()).collect();
    if digits.is_empty() {
        None
    } else {
        digits.parse().ok()
    }
}

fn parse_string_field(s: &str, key: &str) -> Option<String> {
    let idx = s.find(key)?;
    let mut rest = &s[idx + key.len()..];

    // Trim leading whitespace
    rest = rest.trim_start();

    // Take until next space or end
    let token: String = rest.chars().take_while(|c| !c.is_whitespace()).collect();

    if token.is_empty() {
        None
    } else {
        Some(token.trim_matches('"').to_string())
    }
}

fn parse_compatible_brands(s: &str) -> Option<Vec<String>> {
    // e.g. compatible=["isom", "iso2", "avc1", "mp41"]
    let idx = s.find("compatible=[")?;
    let rest = &s[idx + "compatible=[".len()..];
    let end = rest.find(']')?;
    let inside = &rest[..end];
    if inside.trim().is_empty() {
        return Some(Vec::new());
    }
    let mut out = Vec::new();
    for part in inside.split(',') {
        let trimmed = part.trim().trim_matches('"');
        if !trimmed.is_empty() {
            out.push(trimmed.to_string());
        }
    }
    Some(out)
}

// ---- human-readable output -----------------------------------------

fn print_human(info: &MediaInfo) {
    println!("File: {}", info.file);
    if let Some(major) = &info.major_brand {
        println!("Major brand: {}", major);
    }
    if let Some(minor) = info.minor_version {
        println!("Minor version: {}", minor);
    }
    if !info.compatible_brands.is_empty() {
        println!("Compatible brands: {}", info.compatible_brands.join(", "));
    }

    if let (Some(ts), Some(dur)) = (info.movie_timescale, info.movie_duration_ticks) {
        let sec = dur as f64 / ts as f64;
        println!("Movie duration: {} ticks @ {} -> {:.3} s", dur, ts, sec);
    }

    if info.tracks.is_empty() {
        println!("Tracks: (none)");
        return;
    }

    println!("Tracks:");
    for t in &info.tracks {
        println!("  Track {}:", t.index);

        if let Some(tt) = &t.track_type {
            println!("    type: {}", tt);
        }
        if let Some(codec) = &t.codec {
            println!("    codec: {}", codec);
        }
        if let (Some(w), Some(h)) = (t.width, t.height) {
            println!("    size: {}x{}", w, h);
        }

        if let Some(ts) = t.timescale {
            println!("    timescale: {}", ts);
        }
        if let Some(dur) = t.duration_ticks {
            if let Some(sec) = t.duration_seconds {
                println!("    duration: {} ticks -> {:.3} s", dur, sec);
            } else {
                println!("    duration: {} ticks", dur);
            }
        }
        if let Some(lang) = &t.language {
            println!("    language: {}", lang);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_u64_field_extracts_number() {
        let s = "timescale=600000 duration=65536";
        assert_eq!(parse_u64_field(s, "timescale="), Some(600000));
        assert_eq!(parse_u64_field(s, "duration="), Some(65536));
        assert_eq!(parse_u64_field(s, "missing="), None);
    }

    #[test]
    fn parse_u32_field_clamps_to_u32() {
        let s = "timescale=12345";
        assert_eq!(parse_u32_field(s, "timescale="), Some(12345));
    }

    #[test]
    fn parse_string_field_basic() {
        let s = "major=isom minor=512";
        assert_eq!(parse_string_field(s, "major="), Some("isom".to_string()));
        assert_eq!(parse_string_field(s, "minor="), Some("512".to_string()));
        assert_eq!(parse_string_field(s, "missing="), None);
    }

    #[test]
    fn parse_string_field_trims_quotes() {
        let s = r#"language="und""#;
        assert_eq!(parse_string_field(s, "language="), Some("und".to_string()));
    }

    #[test]
    fn parse_compatible_brands_parses_list() {
        let s = r#"major=isom minor=512 compatible=["isom", "iso2", "avc1", "mp41"]"#;
        let brands = parse_compatible_brands(s).unwrap();
        assert_eq!(brands, vec!["isom", "iso2", "avc1", "mp41"]);
    }

    #[test]
    fn parse_compatible_brands_empty() {
        let s = r#"compatible=[]"#;
        let brands = parse_compatible_brands(s).unwrap();
        assert!(brands.is_empty());
    }
}