croft-software 0.2.182

VSCode-style TUI written in Rust
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! Audio/video info view (#183): pure-Rust HEADER parsers for the
//! common containers - WAV (RIFF fmt), FLAC (STREAMINFO), MP3 (ID3v2
//! text tags + first MPEG frame), MP4/M4A/MOV (mvhd + tkhd) - reading
//! bounded byte windows, never decoding streams. The view renders as
//! markdown through the preview machinery, with an optional poster
//! frame via ffmpeg when it exists on PATH (the pdftoppm pattern).

use std::io::Read as _;
use std::path::Path;

/// Bytes read from the head (and tail for some formats): headers only.
const HEAD_BYTES: usize = 512 * 1024;

#[derive(Debug, Default, PartialEq)]
pub struct MediaInfo {
    pub format: String,
    pub codec: Option<String>,
    pub duration_s: Option<f64>,
    pub sample_rate: Option<u32>,
    pub channels: Option<u16>,
    pub width: Option<u32>,
    pub height: Option<u32>,
    pub bitrate_kbps: Option<u32>,
    pub tags: Vec<(String, String)>,
}

pub fn extension_is_media(ext: &str) -> bool {
    matches!(
        ext.to_ascii_lowercase().as_str(),
        "mp3" | "wav" | "flac" | "m4a" | "mp4" | "mov" | "m4v"
    )
}

/// Parse what the head declares. `None` when nothing recognisable.
pub fn probe(path: &Path) -> Option<MediaInfo> {
    let len = std::fs::metadata(path).ok()?.len();
    let mut head = vec![0u8; HEAD_BYTES.min(len as usize)];
    let mut f = std::fs::File::open(path).ok()?;
    f.read_exact(&mut head).ok()?;
    if head.starts_with(b"RIFF") && head.get(8..12) == Some(b"WAVE") {
        return parse_wav(&head, len);
    }
    if head.starts_with(b"fLaC") {
        return parse_flac(&head);
    }
    if head.starts_with(b"ID3")
        || head.get(0..2) == Some(&[0xff, 0xfb])
        || head.get(0..2) == Some(&[0xff, 0xf3])
    {
        return parse_mp3(&head, len);
    }
    if head.get(4..8) == Some(b"ftyp") {
        let brand = head
            .get(8..12)
            .map(|b| String::from_utf8_lossy(b).trim().to_string());
        return parse_mp4(&head, brand);
    }
    None
}

fn be32(b: &[u8], i: usize) -> Option<u32> {
    Some(u32::from_be_bytes(b.get(i..i + 4)?.try_into().ok()?))
}
fn le32(b: &[u8], i: usize) -> Option<u32> {
    Some(u32::from_le_bytes(b.get(i..i + 4)?.try_into().ok()?))
}
fn le16(b: &[u8], i: usize) -> Option<u16> {
    Some(u16::from_le_bytes(b.get(i..i + 2)?.try_into().ok()?))
}

fn parse_wav(head: &[u8], file_len: u64) -> Option<MediaInfo> {
    // Walk RIFF chunks for fmt and data.
    let mut i = 12usize;
    let mut info = MediaInfo {
        format: String::from("WAV (PCM audio)"),
        ..Default::default()
    };
    let mut byte_rate = 0u32;
    let mut data_len = None;
    while i + 8 <= head.len() {
        let id = &head[i..i + 4];
        let sz = le32(head, i + 4)? as usize;
        if id == b"fmt " {
            info.codec = le16(head, i + 8).map(|tag| match tag {
                1 => String::from("PCM"),
                3 => String::from("IEEE float"),
                6 => String::from("A-law"),
                7 => String::from("mu-law"),
                85 => String::from("MP3"),
                other => format!("format tag {other}"),
            });
            info.channels = le16(head, i + 10);
            info.sample_rate = le32(head, i + 12);
            byte_rate = le32(head, i + 16).unwrap_or(0);
        } else if id == b"data" {
            data_len = Some(sz as u64);
        }
        i += 8 + sz + (sz & 1);
    }
    let data = data_len.unwrap_or_else(|| file_len.saturating_sub(44));
    if byte_rate > 0 {
        info.duration_s = Some(data as f64 / byte_rate as f64);
        info.bitrate_kbps = Some(byte_rate * 8 / 1000);
    }
    Some(info)
}

fn parse_flac(head: &[u8]) -> Option<MediaInfo> {
    // STREAMINFO is the first metadata block: 34 bytes at offset 8.
    let b = head.get(8..8 + 34)?;
    let sample_rate = ((b[10] as u32) << 12) | ((b[11] as u32) << 4) | ((b[12] as u32) >> 4);
    let channels = (((b[12] >> 1) & 0x07) + 1) as u16;
    let total: u64 = (((b[13] & 0x0f) as u64) << 32)
        | ((b[14] as u64) << 24)
        | ((b[15] as u64) << 16)
        | ((b[16] as u64) << 8)
        | (b[17] as u64);
    Some(MediaInfo {
        format: String::from("FLAC (lossless audio)"),
        codec: Some(String::from("FLAC")),
        duration_s: (sample_rate > 0 && total > 0).then(|| total as f64 / sample_rate as f64),
        sample_rate: Some(sample_rate),
        channels: Some(channels),
        ..Default::default()
    })
}

fn parse_mp3(head: &[u8], file_len: u64) -> Option<MediaInfo> {
    let mut info = MediaInfo {
        format: String::from("MP3 (MPEG audio)"),
        codec: Some(String::from("MPEG Layer III")),
        ..Default::default()
    };
    let mut off = 0usize;
    // The ten-byte ID3 header must be there before its size is read: a
    // truncated file starting `ID3` indexed past its end and crashed croft.
    if head.starts_with(b"ID3") && head.len() >= 10 {
        let size = (((head[6] & 0x7f) as usize) << 21)
            | (((head[7] & 0x7f) as usize) << 14)
            | (((head[8] & 0x7f) as usize) << 7)
            | ((head[9] & 0x7f) as usize);
        // Text frames: TIT2/TPE1/TALB, ID3v2.3+ layout.
        let mut i = 10usize;
        let end = (10 + size).min(head.len());
        while i + 10 < end {
            let id = &head[i..i + 4];
            let fsz = be32(head, i + 4)? as usize;
            if fsz == 0 || i + 10 + fsz > end {
                break;
            }
            if let Ok(name) = std::str::from_utf8(id)
                && matches!(name, "TIT2" | "TPE1" | "TALB")
            {
                let body = &head[i + 10..i + 10 + fsz];
                // The first byte declares the ENCODING (#202 review):
                // strip it unconditionally and decode accordingly, or a
                // UTF-8/UTF-16 tag keeps a stray control byte.
                let text = match body.split_first() {
                    Some((0, rest)) => encoding_rs::WINDOWS_1252.decode(rest).0.into_owned(),
                    Some((1, rest)) => {
                        let (enc, skip) = match rest.get(0..2) {
                            Some([0xfe, 0xff]) => (encoding_rs::UTF_16BE, 2),
                            Some([0xff, 0xfe]) => (encoding_rs::UTF_16LE, 2),
                            _ => (encoding_rs::UTF_16LE, 0),
                        };
                        enc.decode(&rest[skip..]).0.into_owned()
                    }
                    Some((2, rest)) => encoding_rs::UTF_16BE.decode(rest).0.into_owned(),
                    Some((3, rest)) => String::from_utf8_lossy(rest).into_owned(),
                    _ => String::new(),
                };
                let label = match name {
                    "TIT2" => "title",
                    "TPE1" => "artist",
                    _ => "album",
                };
                let clean = text.trim_matches('\0').trim().to_string();
                if !clean.is_empty() {
                    info.tags.push((label.to_string(), clean));
                }
            }
            i += 10 + fsz;
        }
        off = 10 + size;
    }
    // First MPEG frame header after the tag.
    const BITRATES: [u32; 16] = [
        0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0,
    ];
    const RATES: [u32; 4] = [44100, 48000, 32000, 0];
    while off + 4 <= head.len() {
        if head[off] == 0xff && (head[off + 1] & 0xe0) == 0xe0 {
            let bitrate = BITRATES[(head[off + 2] >> 4) as usize];
            let rate = RATES[((head[off + 2] >> 2) & 0x03) as usize];
            if bitrate > 0 && rate > 0 {
                info.bitrate_kbps = Some(bitrate);
                info.sample_rate = Some(rate);
                info.duration_s = Some(
                    (file_len.saturating_sub(off as u64)) as f64 * 8.0 / (bitrate as f64 * 1000.0),
                );
            }
            break;
        }
        off += 1;
    }
    Some(info)
}

fn parse_mp4(head: &[u8], brand: Option<String>) -> Option<MediaInfo> {
    let mut info = MediaInfo {
        format: String::from("MP4/MOV container"),
        codec: brand.map(|b| format!("brand {b}")),
        ..Default::default()
    };
    // Box walk: find moov/mvhd (timescale+duration) and any tkhd dims.
    fn walk(b: &[u8], info: &mut MediaInfo, depth: usize) {
        if depth > 6 {
            return;
        }
        let mut i = 0usize;
        while i + 8 <= b.len() {
            let sz = u32::from_be_bytes(b[i..i + 4].try_into().unwrap()) as usize;
            let typ = &b[i + 4..i + 8];
            if sz < 8 || i + sz > b.len() {
                break;
            }
            let body = &b[i + 8..i + sz];
            match typ {
                b"moov" | b"trak" => walk(body, info, depth + 1),
                // Version 1 reads up to byte 32, version 0 up to 20: one
                // shared `>= 20` guard let a short version-1 box crash croft.
                b"mvhd" if body.len() >= if body.first() == Some(&1) { 32 } else { 20 } => {
                    let (ts, dur) = if body[0] == 1 {
                        (
                            u32::from_be_bytes(body[20..24].try_into().unwrap()),
                            u64::from_be_bytes(body[24..32].try_into().unwrap()),
                        )
                    } else {
                        (
                            u32::from_be_bytes(body[12..16].try_into().unwrap()),
                            u32::from_be_bytes(body[16..20].try_into().unwrap()) as u64,
                        )
                    };
                    if ts > 0 {
                        info.duration_s = Some(dur as f64 / ts as f64);
                    }
                }
                b"tkhd" if body.len() >= 84 => {
                    let w = u32::from_be_bytes(
                        body[body.len() - 8..body.len() - 4].try_into().unwrap(),
                    ) >> 16;
                    let h = u32::from_be_bytes(body[body.len() - 4..].try_into().unwrap()) >> 16;
                    if w > 0 && h > 0 {
                        info.width = Some(w);
                        info.height = Some(h);
                    }
                }
                _ => {}
            }
            i += sz;
        }
    }
    walk(head, &mut info, 0);
    Some(info)
}

/// Render the info card as markdown, with a poster frame extracted via
/// ffmpeg when it exists on PATH (optional external tool, the pdftoppm
/// pattern; graceful absence).
pub fn to_markdown(path: &Path, info: &MediaInfo, scratch: &Path) -> String {
    let name = path
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_default();
    let mut md = format!(
        "# {name}

"
    );
    md.push_str(
        "|property|value|
|---|---|
",
    );
    md.push_str(&format!(
        "|format|{}|
",
        info.format
    ));
    let esc = |t: &str| t.replace('\\', "\\\\").replace('|', "\\|");
    if let Some(c) = &info.codec {
        md.push_str(&format!(
            "|codec|{}|
",
            esc(c)
        ));
    }
    if let Some(d) = info.duration_s {
        let m = (d / 60.0).floor() as u64;
        md.push_str(&format!(
            "|duration|{m}:{:04.1}|
",
            d - (m as f64) * 60.0
        ));
    }
    if let (Some(w), Some(h)) = (info.width, info.height) {
        md.push_str(&format!(
            "|dimensions|{w} x {h}|
"
        ));
    }
    if let Some(r) = info.sample_rate {
        md.push_str(&format!(
            "|sample rate|{r} Hz|
"
        ));
    }
    if let Some(c) = info.channels {
        md.push_str(&format!(
            "|channels|{c}|
"
        ));
    }
    if let Some(b) = info.bitrate_kbps {
        md.push_str(&format!(
            "|bitrate|{b} kbps|
"
        ));
    }
    if let Ok(meta) = std::fs::metadata(path) {
        md.push_str(&format!(
            "|size|{} bytes|
",
            meta.len()
        ));
    }
    for (k, v) in &info.tags {
        // Escape pipes/backslashes (#202 review): tag text is arbitrary
        // and must not restructure the synthesised table.
        md.push_str(&format!(
            "|{}|{}|
",
            esc(k),
            esc(v)
        ));
    }
    md.push('\n');
    // Poster frame for video files, when ffmpeg exists.
    if info.width.is_some()
        && let Some(poster) = poster_frame(path, scratch)
    {
        md.push_str(&format!(
            "![poster]({})

",
            poster.display()
        ));
    }
    md.push_str(
        "Playback is not available in a terminal. Media: Open in System Player (palette) hands the file to your OS.\n",
    );
    md
}

fn poster_frame(path: &Path, scratch: &Path) -> Option<std::path::PathBuf> {
    use std::hash::{Hash as _, Hasher as _};
    use std::process::{Command, Stdio};
    let mut h = std::collections::hash_map::DefaultHasher::new();
    path.hash(&mut h);
    let meta = std::fs::metadata(path).ok()?;
    meta.len().hash(&mut h);
    // mtime joins the key (#202 review): a re-encode landing at the
    // same byte length must not serve a stale poster forever.
    if let Ok(m) = meta.modified()
        && let Ok(d) = m.duration_since(std::time::UNIX_EPOCH)
    {
        d.as_nanos().hash(&mut h);
    }
    let out = scratch.join(format!("poster-{:016x}.png", h.finish()));
    if out.is_file() {
        return Some(out);
    }
    std::fs::create_dir_all(scratch).ok()?;
    let tmp = scratch.join(format!(
        "tmp-{}-{:016x}.png",
        std::process::id(),
        h.finish()
    ));
    // Bounded (#202 review): poll with try_wait and kill+reap on the
    // deadline, so a wedged ffmpeg cannot stall the open. First open of
    // a given video only - the poster is hash-cached thereafter.
    let mut child = Command::new("ffmpeg")
        .args(["-y", "-loglevel", "quiet", "-ss", "1", "-i"])
        .arg(path)
        .args(["-frames:v", "1"])
        .arg(&tmp)
        .stdin(Stdio::null())
        // Never the inherited tty: this runs mid-session, and anything a
        // child writes there paints raw bytes over the TUI frame.
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .ok()?;
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(3);
    let ok = loop {
        match child.try_wait() {
            Ok(Some(st)) => break st.success(),
            Ok(None) if std::time::Instant::now() < deadline => {
                std::thread::sleep(std::time::Duration::from_millis(30));
            }
            _ => {
                let _ = child.kill();
                let _ = child.wait();
                break false;
            }
        }
    };
    // Rename-on-success (#202 review): a killed or failed ffmpeg can
    // leave a partial file, which must never be served from the cache.
    if ok && tmp.is_file() && std::fs::rename(&tmp, &out).is_ok() {
        return Some(out);
    }
    let _ = std::fs::remove_file(&tmp);
    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write as _;

    #[test]
    fn truncated_mp3_and_mp4_headers_do_not_crash() {
        let tmp = tempfile::tempdir().unwrap();
        let mp3 = tmp.path().join("x.mp3");
        std::fs::write(&mp3, b"ID3\x03\x00").unwrap();
        assert!(probe(&mp3).is_some());
        // ftyp, then moov holding a version-1 mvhd with only 20 body bytes.
        let mut mp4 = Vec::new();
        mp4.extend_from_slice(&16u32.to_be_bytes());
        mp4.extend_from_slice(b"ftypisom\0\0\0\0");
        let mvhd_body = [&[1u8][..], &[0u8; 19][..]].concat();
        let mvhd_len = 8 + mvhd_body.len() as u32;
        mp4.extend_from_slice(&(8 + mvhd_len).to_be_bytes());
        mp4.extend_from_slice(b"moov");
        mp4.extend_from_slice(&mvhd_len.to_be_bytes());
        mp4.extend_from_slice(b"mvhd");
        mp4.extend_from_slice(&mvhd_body);
        let path = tmp.path().join("x.mp4");
        std::fs::write(&path, &mp4).unwrap();
        let info = probe(&path).unwrap();
        assert_eq!(info.duration_s, None);
    }

    #[test]
    fn wav_header_yields_duration_and_rates() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("a.wav");
        let mut f = std::fs::File::create(&p).unwrap();
        let sample_rate = 8000u32;
        let byte_rate = sample_rate * 2;
        let data = vec![0u8; (byte_rate * 2) as usize]; // 2 seconds
        let mut hdr = Vec::new();
        hdr.extend_from_slice(b"RIFF");
        hdr.extend_from_slice(&((36 + data.len()) as u32).to_le_bytes());
        hdr.extend_from_slice(b"WAVEfmt ");
        hdr.extend_from_slice(&16u32.to_le_bytes());
        hdr.extend_from_slice(&1u16.to_le_bytes());
        hdr.extend_from_slice(&1u16.to_le_bytes());
        hdr.extend_from_slice(&sample_rate.to_le_bytes());
        hdr.extend_from_slice(&byte_rate.to_le_bytes());
        hdr.extend_from_slice(&2u16.to_le_bytes());
        hdr.extend_from_slice(&16u16.to_le_bytes());
        hdr.extend_from_slice(b"data");
        hdr.extend_from_slice(&(data.len() as u32).to_le_bytes());
        f.write_all(&hdr).unwrap();
        f.write_all(&data).unwrap();
        let info = probe(&p).unwrap();
        assert_eq!(info.sample_rate, Some(8000));
        assert_eq!(info.channels, Some(1));
        assert!((info.duration_s.unwrap() - 2.0).abs() < 0.01);
    }

    #[test]
    fn flac_streaminfo_parses() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("a.flac");
        let mut b = Vec::new();
        b.extend_from_slice(b"fLaC");
        b.extend_from_slice(&[0x80, 0, 0, 34]); // last block, STREAMINFO, len 34
        let mut si = [0u8; 34];
        // sample rate 44100 = 0xAC44 -> bits [10..12]+high nibble of 12
        si[10] = 0x0A;
        si[11] = 0xC4;
        si[12] = 0x40 | ((2 - 1) << 1); // stereo
        // total samples = 44100 (1 second)
        si[14] = 0x00;
        si[15] = 0x00;
        si[16] = 0xAC;
        si[17] = 0x44;
        b.extend_from_slice(&si);
        std::fs::write(&p, &b).unwrap();
        let info = probe(&p).unwrap();
        assert_eq!(info.sample_rate, Some(44100));
        assert_eq!(info.channels, Some(2));
        assert!((info.duration_s.unwrap() - 1.0).abs() < 0.001);
    }

    #[test]
    fn mp3_id3_tags_and_frame_header_parse() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("a.mp3");
        let mut b = Vec::new();
        let title = b"Song Name";
        let frame_size = 1 + title.len();
        let tag_size = 10 + frame_size;
        b.extend_from_slice(b"ID3\x03\x00\x00");
        b.extend_from_slice(&[
            ((tag_size >> 21) & 0x7f) as u8,
            ((tag_size >> 14) & 0x7f) as u8,
            ((tag_size >> 7) & 0x7f) as u8,
            (tag_size & 0x7f) as u8,
        ]);
        b.extend_from_slice(b"TIT2");
        b.extend_from_slice(&(frame_size as u32).to_be_bytes());
        b.extend_from_slice(&[0, 0]);
        b.push(0);
        b.extend_from_slice(title);
        // pad to declared tag size then a 128kbps 44.1k frame header
        while b.len() < 10 + tag_size {
            b.push(0);
        }
        b.extend_from_slice(&[0xff, 0xfb, 0x90, 0x00]);
        b.extend_from_slice(&vec![0u8; 32000]); // ~2s at 128kbps
        std::fs::write(&p, &b).unwrap();
        let info = probe(&p).unwrap();
        assert_eq!(info.bitrate_kbps, Some(128));
        assert_eq!(info.sample_rate, Some(44100));
        assert!(
            info.tags
                .iter()
                .any(|(k, v)| k == "title" && v == "Song Name")
        );
        assert!((info.duration_s.unwrap() - 2.0).abs() < 0.1);
    }

    #[test]
    fn mp4_mvhd_and_tkhd_parse() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("a.mp4");
        let mut mvhd = vec![0u8; 100];
        mvhd[..4].copy_from_slice(&108u32.to_be_bytes());
        mvhd[4..8].copy_from_slice(b"mvhd");
        // version 0: timescale at body[12..16], duration at [16..20]
        mvhd[8 + 12..8 + 16].copy_from_slice(&1000u32.to_be_bytes());
        mvhd[8 + 16..8 + 20].copy_from_slice(&5000u32.to_be_bytes());
        let mut tkhd = vec![0u8; 100];
        tkhd[..4].copy_from_slice(&100u32.to_be_bytes());
        tkhd[4..8].copy_from_slice(b"tkhd");
        let bl = 100 - 8;
        tkhd[8 + bl - 8..8 + bl - 4].copy_from_slice(&(640u32 << 16).to_be_bytes());
        tkhd[8 + bl - 4..].copy_from_slice(&(480u32 << 16).to_be_bytes());
        mvhd.truncate(108);
        let trak_len = 8 + tkhd.len();
        let moov_len = 8 + mvhd.len() + trak_len;
        let mut b = Vec::new();
        b.extend_from_slice(&16u32.to_be_bytes());
        b.extend_from_slice(b"ftypisom");
        b.extend_from_slice(&[0u8; 4]);
        b.extend_from_slice(&(moov_len as u32).to_be_bytes());
        b.extend_from_slice(b"moov");
        b.extend_from_slice(&mvhd);
        b.extend_from_slice(&(trak_len as u32).to_be_bytes());
        b.extend_from_slice(b"trak");
        b.extend_from_slice(&tkhd);
        std::fs::write(&p, &b).unwrap();
        let info = probe(&p).unwrap();
        assert!((info.duration_s.unwrap() - 5.0).abs() < 0.001);
        assert_eq!(info.width, Some(640));
        assert_eq!(info.height, Some(480));
    }

    #[test]
    fn junk_is_not_media() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("x.mp3");
        std::fs::write(&p, b"nothing here").unwrap();
        assert!(probe(&p).is_none());
    }
}