| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use std::path::Path; |
| 9 | use std::process::Command; |
| 10 | |
| 11 | use engine::decode::{Backend, probe}; |
| 12 | use engine::demux::{Codec, probe_media}; |
| 13 | use engine::scratch::Scratch; |
| 14 | |
| 15 | fn ffmpeg(out: &Path, args: &[&str]) { |
| 16 | let status = Command::new("ffmpeg") |
| 17 | .args(["-y", "-loglevel", "error", "-f", "lavfi", "-i", "sine=frequency=440:duration=1"]) |
| 18 | .args(args) |
| 19 | .arg(out) |
| 20 | .status() |
| 21 | .expect("run ffmpeg"); |
| 22 | assert!(status.success(), "ffmpeg failed for {}", out.display()); |
| 23 | } |
| 24 | |
| 25 | |
| 26 | fn mp4_words(text: &str) -> bool { |
| 27 | let text = text.to_ascii_lowercase(); |
| 28 | text.contains("ftyp") || text.contains("mp4") || text.contains("quicktime") |
| 29 | } |
| 30 | |
| 31 | #[test] |
| 32 | fn sound_files_and_mkv_are_never_answered_in_mp4_words() { |
| 33 | let cases: [(&str, &[&str]); 4] = [ |
| 34 | ("wavftyp_a", &["-ac", "2"]), |
| 35 | ("wavftyp_b", &["-c:a", "flac", "-ac", "2"]), |
| 36 | ("wavftyp_c", &["-c:a", "libvorbis", "-ac", "2"]), |
| 37 | ("wavftyp_d", &["-c:a", "libopus", "-ac", "2"]), |
| 38 | ]; |
| 39 | let exts = ["wav", "flac", "ogg", "mkv"]; |
| 40 | for ((name, args), ext) in cases.iter().zip(exts) { |
| 41 | let file = Scratch::file(name, ext); |
| 42 | ffmpeg(&file, args); |
| 43 | let path: &Path = &file; |
| 44 | |
| 45 | |
| 46 | match probe(path) { |
| 47 | Ok((codec, backend)) => { |
| 48 | if ext != "mkv" { |
| 49 | assert_eq!(codec, None, "{ext}: a sound file has no coded stream"); |
| 50 | assert_eq!(backend, Backend::Still, "{ext}"); |
| 51 | } |
| 52 | } |
| 53 | Err(e) => { |
| 54 | let text = e.to_string(); |
| 55 | assert!(!mp4_words(&text), "{ext}: the probe answered in mp4 words: {text}"); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | let facts = probe_media(path); |
| 61 | if let Some(container) = &facts.container { |
| 62 | assert!(!mp4_words(container), "{ext}: container {container}"); |
| 63 | } |
| 64 | assert!(facts.duration_secs.is_some_and(|d| d > 0.5), "{ext}: {:?}", facts.duration_secs); |
| 65 | assert!(facts.video.is_none(), "{ext}: a sound file has no picture"); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | #[test] |
| 70 | fn a_real_mp4_still_gets_its_picture_answer() { |
| 71 | let file = Scratch::file("wavftyp_mp4", "mp4"); |
| 72 | let status = Command::new("ffmpeg") |
| 73 | .args(["-y", "-loglevel", "error"]) |
| 74 | .args(["-f", "lavfi", "-i", "sine=frequency=440:duration=1"]) |
| 75 | .args(["-f", "lavfi", "-i", "testsrc=size=320x240:rate=25:duration=1"]) |
| 76 | .args(["-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", "-ac", "2", "-shortest"]) |
| 77 | .arg(&*file) |
| 78 | .status() |
| 79 | .expect("run ffmpeg"); |
| 80 | assert!(status.success(), "ffmpeg failed for the mp4 fixture"); |
| 81 | let path: &Path = &file; |
| 82 | let (codec, backend) = probe(path).expect("an mp4 with a picture probes"); |
| 83 | assert_eq!(codec, Some(Codec::H264)); |
| 84 | assert_ne!(backend, Backend::Still); |
| 85 | let facts = probe_media(path); |
| 86 | let video = facts.video.expect("the mp4 has its picture"); |
| 87 | assert_eq!(video.codec, Codec::H264); |
| 88 | assert_eq!((video.width, video.height), (320, 240)); |
| 89 | assert!(facts.streams.iter().any(|s| s.codec_id.as_deref() == Some("mp4a")), "{:?}", facts.streams); |
| 90 | } |