forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

aac_profile.rs133 lines, 3.7 KiBHistoryRawDownload
1//! The AAC profile a probe states (`StreamFacts::profile`), checked against
2//! ffprobe's `profile` field for every case: AAC-LC, HE-AAC v1 and HE-AACv2, each
3//! with explicit signalling (the config names SBR or PS) and implicit (a plain
4//! AAC-LC config whose bitstream carries them), in mp4 and in Matroska. The
5//! Matroska files are `-c copy` twins of the mp4 ones.
6
7use std::path::{Path, PathBuf};
8use std::process::Command;
9
10use engine::{StreamKind, probe_media};
11
12fn data(name: &str) -> PathBuf {
13 Path::new(env!("CARGO_MANIFEST_DIR"))
14 .join("tests/data")
15 .join(name)
16}
17
18fn run(program: &str, args: &[&str]) {
19 let status = Command::new(program)
20 .args(["-y", "-loglevel", "error"])
21 .args(args)
22 .status()
23 .unwrap_or_else(|e| panic!("run {program}: {e}"));
24 assert!(status.success(), "{program} failed");
25}
26
27/// The first audio stream's `profile` as ffprobe reports it.
28fn ffprobe_profile(path: &Path) -> String {
29 let out = Command::new("ffprobe")
30 .args([
31 "-v",
32 "error",
33 "-select_streams",
34 "a:0",
35 "-show_entries",
36 "stream=profile",
37 ])
38 .args(["-of", "csv=p=0"])
39 .arg(path)
40 .output()
41 .expect("run ffprobe");
42 assert!(
43 out.status.success(),
44 "ffprobe failed for {}",
45 path.display()
46 );
47 String::from_utf8_lossy(&out.stdout).trim().to_owned()
48}
49
50/// The first audio stream's profile as the engine's probe states it.
51fn probed_profile(path: &Path) -> Option<String> {
52 probe_media(path)
53 .streams
54 .into_iter()
55 .find(|s| s.kind == StreamKind::Audio)
56 .and_then(|s| s.profile)
57}
58
59/// Checks `file` (an mp4) and its mkv twin: the engine and ffprobe both say `expect`.
60fn check(file: &Path, expect: &str) {
61 let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("aac_profile-{}", std::process::id()));
62 std::fs::create_dir_all(&dir).expect("make the test dir");
63 let stem = file.file_stem().expect("input has a file stem");
64 let mkv = dir.join(format!("{}.mkv", stem.to_str().unwrap()));
65 run(
66 "ffmpeg",
67 &[
68 "-i",
69 file.to_str().unwrap(),
70 "-c",
71 "copy",
72 mkv.to_str().unwrap(),
73 ],
74 );
75 for path in [file, mkv.as_path()] {
76 assert_eq!(
77 ffprobe_profile(path),
78 expect,
79 "ffprobe on {}",
80 path.display()
81 );
82 assert_eq!(
83 probed_profile(path).as_deref(),
84 Some(expect),
85 "probe_media on {}",
86 path.display()
87 );
88 }
89}
90
91#[test]
92fn aac_lc_matches_ffprobe_in_mp4_and_mkv() {
93 let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("aac_profile-{}", std::process::id()));
94 std::fs::create_dir_all(&dir).expect("make the test dir");
95 let lc = dir.join("lc.m4a");
96 run(
97 "ffmpeg",
98 &[
99 "-f",
100 "lavfi",
101 "-i",
102 "sine=frequency=440:duration=1",
103 "-c:a",
104 "aac",
105 "-profile:a",
106 "aac_low",
107 "-b:a",
108 "64k",
109 lc.to_str().unwrap(),
110 ],
111 );
112 check(&lc, "LC");
113}
114
115#[test]
116fn explicit_he_aac_v1_matches_ffprobe_in_mp4_and_mkv() {
117 check(&data("aac_explicit_he_v1.m4a"), "HE-AAC");
118}
119
120#[test]
121fn explicit_he_aac_v2_matches_ffprobe_in_mp4_and_mkv() {
122 check(&data("aac_explicit_he_v2.m4a"), "HE-AACv2");
123}
124
125#[test]
126fn implicit_he_aac_v1_matches_ffprobe_in_mp4_and_mkv() {
127 check(&data("aac_implicit_he_v1.m4a"), "HE-AAC");
128}
129
130#[test]
131fn implicit_he_aac_v2_matches_ffprobe_in_mp4_and_mkv() {
132 check(&data("aac_he_v2_implicit_stereo.m4a"), "HE-AACv2");
133}