| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | use std::path::{Path, PathBuf}; |
| 8 | use std::process::Command; |
| 9 | |
| 10 | use engine::{StreamKind, probe_media}; |
| 11 | |
| 12 | fn data(name: &str) -> PathBuf { |
| 13 | Path::new(env!("CARGO_MANIFEST_DIR")) |
| 14 | .join("tests/data") |
| 15 | .join(name) |
| 16 | } |
| 17 | |
| 18 | fn 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 | |
| 28 | fn 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 | |
| 51 | fn 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 | |
| 60 | fn 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] |
| 92 | fn 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] |
| 116 | fn explicit_he_aac_v1_matches_ffprobe_in_mp4_and_mkv() { |
| 117 | check(&data("aac_explicit_he_v1.m4a"), "HE-AAC"); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn explicit_he_aac_v2_matches_ffprobe_in_mp4_and_mkv() { |
| 122 | check(&data("aac_explicit_he_v2.m4a"), "HE-AACv2"); |
| 123 | } |
| 124 | |
| 125 | #[test] |
| 126 | fn implicit_he_aac_v1_matches_ffprobe_in_mp4_and_mkv() { |
| 127 | check(&data("aac_implicit_he_v1.m4a"), "HE-AAC"); |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn implicit_he_aac_v2_matches_ffprobe_in_mp4_and_mkv() { |
| 132 | check(&data("aac_he_v2_implicit_stereo.m4a"), "HE-AACv2"); |
| 133 | } |