| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use std::path::{Path, PathBuf}; |
| 7 | use std::process::Command; |
| 8 | |
| 9 | use engine::{StreamKind, probe_media}; |
| 10 | |
| 11 | |
| 12 | fn 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 | |
| 19 | fn 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 | |
| 33 | fn 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 | |
| 48 | const TONE: [&str; 4] = ["-f", "lavfi", "-i", "sine=frequency=440:duration=1"]; |
| 49 | |
| 50 | #[test] |
| 51 | fn 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] |
| 87 | fn 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] |
| 117 | fn 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] |
| 147 | fn 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] |
| 172 | fn 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 | } |