forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

video_profile.rs194 lines, 5.7 KiBHistoryRawDownload
1//! The video profile a probe states (`VideoFacts::profile`), checked against
2//! ffprobe's `stream=profile` for every video in `assets/` whose codec is H.264,
3//! HEVC, AV1, VP9 or VP8, and for a set of small streams this test encodes itself
4//! with ffmpeg: each one in Matroska and, where ffmpeg can remux it, in mp4.
5//!
6//! A file the engine refuses to open has no `VideoFacts` and is skipped. The
7//! generated files live under `CARGO_TARGET_TMPDIR`, never in the source tree.
8//!
9//! ```text
10//! cargo test --release -p engine --test video_profile
11//! ```
12
13use std::fs;
14use std::path::{Path, PathBuf};
15use std::process::Command;
16
17use engine::probe_media;
18
19/// The codecs whose profile this checks.
20const CODECS: [&str; 5] = ["h264", "hevc", "av1", "vp9", "vp8"];
21
22/// `(name, ffmpeg encoder, extra arguments)`: one half-second stream per case.
23const ENCODED: &[(&str, &str, &[&str])] = &[
24 (
25 "h264_baseline",
26 "libx264",
27 &["-profile:v", "baseline", "-pix_fmt", "yuv420p"],
28 ),
29 (
30 "h264_main",
31 "libx264",
32 &["-profile:v", "main", "-pix_fmt", "yuv420p"],
33 ),
34 (
35 "h264_high",
36 "libx264",
37 &["-profile:v", "high", "-pix_fmt", "yuv420p"],
38 ),
39 (
40 "h264_high10",
41 "libx264",
42 &["-profile:v", "high10", "-pix_fmt", "yuv420p10le"],
43 ),
44 (
45 "hevc_main",
46 "libx265",
47 &["-pix_fmt", "yuv420p", "-x265-params", "log-level=error"],
48 ),
49 (
50 "hevc_main10",
51 "libx265",
52 &["-pix_fmt", "yuv420p10le", "-x265-params", "log-level=error"],
53 ),
54 (
55 "av1_profile0",
56 "libaom-av1",
57 &[
58 "-profile:v",
59 "main",
60 "-pix_fmt",
61 "yuv420p",
62 "-cpu-used",
63 "8",
64 ],
65 ),
66 (
67 "vp9_profile0",
68 "libvpx-vp9",
69 &[
70 "-pix_fmt",
71 "yuv420p",
72 "-deadline",
73 "realtime",
74 "-cpu-used",
75 "8",
76 ],
77 ),
78 (
79 "vp9_profile2",
80 "libvpx-vp9",
81 &[
82 "-pix_fmt",
83 "yuv420p10le",
84 "-deadline",
85 "realtime",
86 "-cpu-used",
87 "8",
88 ],
89 ),
90];
91
92/// One ffprobe `stream=<field>` of the first video stream. `None` where ffprobe
93/// cannot read the file; an empty string where the stream states no such field.
94fn ffprobe_field(path: &Path, field: &str) -> Option<String> {
95 let out = Command::new("ffprobe")
96 .args(["-v", "error", "-select_streams", "v:0", "-show_entries"])
97 .arg(format!("stream={field}"))
98 .args(["-of", "csv=p=0"])
99 .arg(path)
100 .output()
101 .expect("run ffprobe");
102 out.status
103 .success()
104 .then(|| String::from_utf8_lossy(&out.stdout).trim().to_owned())
105}
106
107/// Runs ffmpeg quietly and says whether it succeeded.
108fn ffmpeg(args: &[&str]) -> bool {
109 Command::new("ffmpeg")
110 .args(["-y", "-loglevel", "error"])
111 .args(args)
112 .status()
113 .expect("run ffmpeg")
114 .success()
115}
116
117/// Checks `path`: the profile the probe states is the one ffprobe states, with
118/// an empty ffprobe answer meaning `None`.
119fn check(path: &Path) {
120 let facts = probe_media(path)
121 .video
122 .unwrap_or_else(|| panic!("{}: no video facts", path.display()));
123 let expect =
124 ffprobe_field(path, "profile").unwrap_or_else(|| panic!("ffprobe: {}", path.display()));
125 let expect = (!expect.is_empty()).then_some(expect);
126 assert_eq!(
127 facts.profile.map(str::to_owned),
128 expect,
129 "{}",
130 path.display()
131 );
132}
133
134/// Every video in `assets/` of one of [`CODECS`] that the engine opens.
135#[test]
136fn asset_videos_state_the_profile_ffprobe_states() {
137 let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../assets");
138 let mut paths: Vec<PathBuf> = fs::read_dir(&dir)
139 .unwrap_or_else(|e| panic!("{}: {e}", dir.display()))
140 .map(|entry| entry.expect("read assets entry").path())
141 .filter(|p| p.is_file())
142 .collect();
143 paths.sort();
144 let mut checked = 0;
145 for path in &paths {
146 let Some(codec) = ffprobe_field(path, "codec_name") else {
147 continue;
148 };
149 if !CODECS.contains(&codec.as_str()) {
150 continue;
151 }
152 if probe_media(path).video.is_none() {
153 eprintln!("skipped (engine refuses): {}", path.display());
154 continue;
155 }
156 check(path);
157 checked += 1;
158 }
159 eprintln!("checked {checked} asset videos");
160}
161
162/// Each generated stream, in Matroska and in mp4 (remuxed by ffmpeg from the
163/// Matroska one, skipped where ffmpeg cannot mux it).
164#[test]
165fn generated_streams_state_the_profile_ffprobe_states() {
166 let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("video_profile-{}", std::process::id()));
167 fs::create_dir_all(&dir).expect("create the output directory");
168 for (name, encoder, args) in ENCODED {
169 let mkv = dir.join(format!("{name}.mkv"));
170 let mkv_s = mkv.to_str().expect("utf-8 path");
171 let mut encode = vec![
172 "-f",
173 "lavfi",
174 "-i",
175 "testsrc=size=320x240:rate=30",
176 "-t",
177 "0.5",
178 "-c:v",
179 encoder,
180 ];
181 encode.extend_from_slice(args);
182 encode.push(mkv_s);
183 assert!(ffmpeg(&encode), "{name}: ffmpeg could not encode");
184 check(&mkv);
185
186 let mp4 = dir.join(format!("{name}.mp4"));
187 let mp4_s = mp4.to_str().expect("utf-8 path");
188 if ffmpeg(&["-i", mkv_s, "-c", "copy", mp4_s]) {
189 check(&mp4);
190 } else {
191 eprintln!("{name}: ffmpeg cannot mux it into mp4, mkv only");
192 }
193 }
194}