forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

cover_art.rs187 lines, 4.8 KiBHistoryRawDownload
1//! The cover art a probe reports (`MediaFacts::attached_pic`): an ID3v2 `APIC`,
2//! an mp4 `covr` atom, a FLAC `PICTURE` block and a Matroska `image/*`
3//! attachment. A cover is never a playable stream, so the audio file that
4//! carries one stays audio-only. Fixtures are made with ffmpeg at test time.
5
6use std::path::{Path, PathBuf};
7use std::process::Command;
8
9use engine::{StreamKind, probe_media};
10
11/// A fresh directory for one test's fixtures, under the system temp dir.
12fn fixture_dir(name: &str) -> PathBuf {
13 let dir = std::env::temp_dir().join(format!("edith-cover-{name}-{}", std::process::id()));
14 std::fs::create_dir_all(&dir).expect("fixture dir");
15 dir
16}
17
18/// Runs ffmpeg with `args`, failing the test with its stderr when it does not succeed.
19fn ffmpeg(args: &[&str]) {
20 let out = Command::new("ffmpeg")
21 .args(["-v", "error", "-y"])
22 .args(args)
23 .output()
24 .expect("ffmpeg runs");
25 assert!(
26 out.status.success(),
27 "ffmpeg {args:?}: {}",
28 String::from_utf8_lossy(&out.stderr)
29 );
30}
31
32/// A tiny generated png, the cover every fixture embeds.
33fn cover_png(dir: &Path) -> PathBuf {
34 let png = dir.join("cover.png");
35 ffmpeg(&[
36 "-f",
37 "lavfi",
38 "-i",
39 "color=c=red:s=64x64",
40 "-frames:v",
41 "1",
42 png.to_str().expect("utf-8 path"),
43 ]);
44 png
45}
46
47/// A one-second sine tone, the audio every fixture carries.
48const TONE: [&str; 4] = ["-f", "lavfi", "-i", "sine=frequency=440:duration=1"];
49
50#[test]
51fn an_mp3_with_an_id3_apic_frame_has_a_cover_and_stays_audio_only() {
52 let dir = fixture_dir("mp3-apic");
53 let png = cover_png(&dir);
54 let mp3 = dir.join("apic.mp3");
55 ffmpeg(&[
56 TONE[0],
57 TONE[1],
58 TONE[2],
59 TONE[3],
60 "-i",
61 png.to_str().unwrap(),
62 "-map",
63 "0",
64 "-map",
65 "1",
66 "-c:a",
67 "libmp3lame",
68 "-c:v",
69 "mjpeg",
70 "-disposition:v",
71 "attached_pic",
72 "-id3v2_version",
73 "3",
74 mp3.to_str().unwrap(),
75 ]);
76 let facts = probe_media(&mp3);
77 assert!(facts.attached_pic, "an APIC frame is cover art");
78 assert!(facts.video.is_none(), "a cover is not the picture track");
79 assert!(
80 facts.streams.iter().all(|s| s.kind != StreamKind::Video),
81 "a cover is not listed as a video stream: {:?}",
82 facts.streams
83 );
84}
85
86#[test]
87fn an_m4a_with_a_covr_atom_has_a_cover() {
88 let dir = fixture_dir("m4a-covr");
89 let png = cover_png(&dir);
90 let m4a = dir.join("covr.m4a");
91 ffmpeg(&[
92 TONE[0],
93 TONE[1],
94 TONE[2],
95 TONE[3],
96 "-i",
97 png.to_str().unwrap(),
98 "-map",
99 "0:a",
100 "-map",
101 "1",
102 "-c:a",
103 "aac",
104 "-c:v",
105 "mjpeg",
106 "-disposition:v",
107 "attached_pic",
108 m4a.to_str().unwrap(),
109 ]);
110 let facts = probe_media(&m4a);
111 assert!(facts.attached_pic, "a covr atom is cover art");
112 assert!(facts.video.is_none());
113 assert!(facts.streams.iter().all(|s| s.kind != StreamKind::Video));
114}
115
116#[test]
117fn a_flac_with_a_picture_block_has_a_cover() {
118 let dir = fixture_dir("flac-picture");
119 let png = cover_png(&dir);
120 let flac = dir.join("picture.flac");
121 ffmpeg(&[
122 TONE[0],
123 TONE[1],
124 TONE[2],
125 TONE[3],
126 "-i",
127 png.to_str().unwrap(),
128 "-map",
129 "0:a",
130 "-map",
131 "1",
132 "-c:a",
133 "flac",
134 "-c:v",
135 "png",
136 "-disposition:v",
137 "attached_pic",
138 flac.to_str().unwrap(),
139 ]);
140 let facts = probe_media(&flac);
141 assert!(facts.attached_pic, "a METADATA_BLOCK_PICTURE is cover art");
142 assert!(facts.video.is_none());
143 assert!(facts.streams.iter().all(|s| s.kind != StreamKind::Video));
144}
145
146#[test]
147fn a_matroska_image_attachment_is_a_cover() {
148 let dir = fixture_dir("mkv-attach");
149 let png = cover_png(&dir);
150 let mkv = dir.join("attach.mkv");
151 ffmpeg(&[
152 TONE[0],
153 TONE[1],
154 TONE[2],
155 TONE[3],
156 "-map",
157 "0:a",
158 "-c:a",
159 "libopus",
160 "-attach",
161 png.to_str().unwrap(),
162 "-metadata:s:t",
163 "mimetype=image/png",
164 mkv.to_str().unwrap(),
165 ]);
166 let facts = probe_media(&mkv);
167 assert!(facts.attached_pic, "an image/* attachment is cover art");
168 assert!(facts.streams.iter().all(|s| s.kind != StreamKind::Video));
169}
170
171#[test]
172fn a_plain_mp3_has_no_cover() {
173 let dir = fixture_dir("mp3-plain");
174 let mp3 = dir.join("plain.mp3");
175 ffmpeg(&[
176 TONE[0],
177 TONE[1],
178 TONE[2],
179 TONE[3],
180 "-c:a",
181 "libmp3lame",
182 mp3.to_str().unwrap(),
183 ]);
184 let facts = probe_media(&mp3);
185 assert!(!facts.attached_pic);
186 assert!(facts.video.is_none());
187}