forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

container_probe.rs90 lines, 3.7 KiBHistoryRawDownload
1//! What the per-file probes say about containers that are not mp4.
2//!
3//! The media panel asks `decode::probe` and `probe_media` of every file it shows. A wav, flac or
4//! ogg is not an mp4, so the mp4 reader must never be asked of it: its refusal ("ftyp not found:
5//! it is not an mp4 or QuickTime container") would be shown beside a file that plays. A real mp4
6//! keeps its picture answer.
7
8use std::path::Path;
9use std::process::Command;
10
11use engine::decode::{Backend, probe};
12use engine::demux::{Codec, probe_media};
13use engine::scratch::Scratch;
14
15fn ffmpeg(out: &Path, args: &[&str]) {
16 let status = Command::new("ffmpeg")
17 .args(["-y", "-loglevel", "error", "-f", "lavfi", "-i", "sine=frequency=440:duration=1"])
18 .args(args)
19 .arg(out)
20 .status()
21 .expect("run ffmpeg");
22 assert!(status.success(), "ffmpeg failed for {}", out.display());
23}
24
25/// Every word a panel could show for `result`, and whether any of them is the mp4 reader's.
26fn mp4_words(text: &str) -> bool {
27 let text = text.to_ascii_lowercase();
28 text.contains("ftyp") || text.contains("mp4") || text.contains("quicktime")
29}
30
31#[test]
32fn sound_files_and_mkv_are_never_answered_in_mp4_words() {
33 let cases: [(&str, &[&str]); 4] = [
34 ("wavftyp_a", &["-ac", "2"]),
35 ("wavftyp_b", &["-c:a", "flac", "-ac", "2"]),
36 ("wavftyp_c", &["-c:a", "libvorbis", "-ac", "2"]),
37 ("wavftyp_d", &["-c:a", "libopus", "-ac", "2"]),
38 ];
39 let exts = ["wav", "flac", "ogg", "mkv"];
40 for ((name, args), ext) in cases.iter().zip(exts) {
41 let file = Scratch::file(name, ext);
42 ffmpeg(&file, args);
43 let path: &Path = &file;
44
45 // The decoder answer the panel shows on the row's second line.
46 match probe(path) {
47 Ok((codec, backend)) => {
48 if ext != "mkv" {
49 assert_eq!(codec, None, "{ext}: a sound file has no coded stream");
50 assert_eq!(backend, Backend::Still, "{ext}");
51 }
52 }
53 Err(e) => {
54 let text = e.to_string();
55 assert!(!mp4_words(&text), "{ext}: the probe answered in mp4 words: {text}");
56 }
57 }
58
59 // The facts the panel draws: the container and the length, with no mp4 words in them.
60 let facts = probe_media(path);
61 if let Some(container) = &facts.container {
62 assert!(!mp4_words(container), "{ext}: container {container}");
63 }
64 assert!(facts.duration_secs.is_some_and(|d| d > 0.5), "{ext}: {:?}", facts.duration_secs);
65 assert!(facts.video.is_none(), "{ext}: a sound file has no picture");
66 }
67}
68
69#[test]
70fn a_real_mp4_still_gets_its_picture_answer() {
71 let file = Scratch::file("wavftyp_mp4", "mp4");
72 let status = Command::new("ffmpeg")
73 .args(["-y", "-loglevel", "error"])
74 .args(["-f", "lavfi", "-i", "sine=frequency=440:duration=1"])
75 .args(["-f", "lavfi", "-i", "testsrc=size=320x240:rate=25:duration=1"])
76 .args(["-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", "-ac", "2", "-shortest"])
77 .arg(&*file)
78 .status()
79 .expect("run ffmpeg");
80 assert!(status.success(), "ffmpeg failed for the mp4 fixture");
81 let path: &Path = &file;
82 let (codec, backend) = probe(path).expect("an mp4 with a picture probes");
83 assert_eq!(codec, Some(Codec::H264));
84 assert_ne!(backend, Backend::Still);
85 let facts = probe_media(path);
86 let video = facts.video.expect("the mp4 has its picture");
87 assert_eq!(video.codec, Codec::H264);
88 assert_eq!((video.width, video.height), (320, 240));
89 assert!(facts.streams.iter().any(|s| s.codec_id.as_deref() == Some("mp4a")), "{:?}", facts.streams);
90}