| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | use std::path::{Path, PathBuf}; |
| 18 | use std::process::Command; |
| 19 | |
| 20 | use engine::scratch::Scratch; |
| 21 | use engine::subtitle::{self, Cue}; |
| 22 | |
| 23 | fn data(name: &str) -> PathBuf { |
| 24 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 25 | .join("tests/data") |
| 26 | .join(name) |
| 27 | } |
| 28 | |
| 29 | fn have_ffmpeg() -> bool { |
| 30 | Command::new("ffmpeg") |
| 31 | .arg("-version") |
| 32 | .output() |
| 33 | .is_ok_and(|o| o.status.success()) |
| 34 | } |
| 35 | |
| 36 | fn s(path: &Path) -> &str { |
| 37 | path.to_str().expect("a UTF-8 path") |
| 38 | } |
| 39 | |
| 40 | |
| 41 | fn ffmpeg(args: &[&str]) { |
| 42 | let out = Command::new("ffmpeg") |
| 43 | .args(["-v", "error", "-y"]) |
| 44 | .args(args) |
| 45 | .output() |
| 46 | .expect("ffmpeg runs"); |
| 47 | assert!( |
| 48 | out.status.success(), |
| 49 | "ffmpeg {args:?}: {}", |
| 50 | String::from_utf8_lossy(&out.stderr) |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | fn expected() -> Vec<Cue> { |
| 58 | vec![ |
| 59 | Cue { |
| 60 | start_us: 500_000, |
| 61 | end_us: 1_500_000, |
| 62 | text: "first line".into(), |
| 63 | image: None, |
| 64 | words: Vec::new(), |
| 65 | karaoke: Vec::new(), |
| 66 | }, |
| 67 | Cue { |
| 68 | start_us: 2_000_000, |
| 69 | end_us: 3_250_000, |
| 70 | text: "second line\nwith a break".into(), |
| 71 | image: None, |
| 72 | words: Vec::new(), |
| 73 | karaoke: Vec::new(), |
| 74 | }, |
| 75 | Cue { |
| 76 | start_us: 4_000_000, |
| 77 | end_us: 4_750_000, |
| 78 | text: "third line".into(), |
| 79 | image: None, |
| 80 | words: Vec::new(), |
| 81 | karaoke: Vec::new(), |
| 82 | }, |
| 83 | ] |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn an_srt_muxed_into_matroska_reads_back_its_cues_and_timing() { |
| 88 | if !have_ffmpeg() { |
| 89 | eprintln!("skipped: no ffmpeg to mux the SRT with"); |
| 90 | return; |
| 91 | } |
| 92 | let src = data("test_subs.srt"); |
| 93 | let mkv = Scratch::file("ve_srt_in_mkv", "mkv"); |
| 94 | ffmpeg(&["-i", s(&src), "-c:s", "subrip", s(&mkv)]); |
| 95 | |
| 96 | let tracks = subtitle::of_matroska(&mkv).expect("the mkv is walked"); |
| 97 | assert_eq!(tracks.len(), 1, "one subtitle track"); |
| 98 | let track = &tracks[0]; |
| 99 | assert_eq!(track.refused, None, "the SRT track is readable"); |
| 100 | assert!(!track.is_bitmap(), "an SRT is text"); |
| 101 | assert_eq!(track.cues, expected(), "the cues and their timing survive"); |
| 102 | |
| 103 | |
| 104 | let one = subtitle::open(&mkv, track.track); |
| 105 | assert_eq!(one.cues, expected()); |
| 106 | } |
| 107 | |
| 108 | #[test] |
| 109 | fn a_webvtt_sidecar_written_by_ffmpeg_reads_the_same_cues() { |
| 110 | if !have_ffmpeg() { |
| 111 | eprintln!("skipped: no ffmpeg to write the WebVTT with"); |
| 112 | return; |
| 113 | } |
| 114 | let src = data("test_subs.srt"); |
| 115 | let vtt = Scratch::file("ve_sidecar", "vtt"); |
| 116 | ffmpeg(&["-i", s(&src), s(&vtt)]); |
| 117 | |
| 118 | let track = subtitle::open(&vtt, None); |
| 119 | assert_eq!(track.refused, None, "the WebVTT parses"); |
| 120 | assert_eq!(track.cues, expected(), "the sidecar's cues and timing"); |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn a_webvtt_muxed_into_matroska_reads_back_its_cues() { |
| 125 | if !have_ffmpeg() { |
| 126 | eprintln!("skipped: no ffmpeg to mux the WebVTT with"); |
| 127 | return; |
| 128 | } |
| 129 | let src = data("test_subs.srt"); |
| 130 | let mkv = Scratch::file("ve_vtt_in_mkv", "mkv"); |
| 131 | ffmpeg(&["-i", s(&src), "-c:s", "webvtt", s(&mkv)]); |
| 132 | |
| 133 | let tracks = subtitle::of_matroska(&mkv).expect("the mkv is walked"); |
| 134 | assert_eq!(tracks.len(), 1, "one subtitle track"); |
| 135 | let track = &tracks[0]; |
| 136 | assert_eq!(track.refused, None, "the WebVTT track is readable"); |
| 137 | assert_eq!(track.cues, expected(), "the cues and timing survive"); |
| 138 | } |
| 139 | |
| 140 | #[test] |
| 141 | fn an_ass_muxed_into_matroska_keeps_its_timing_and_resolves_its_markup() { |
| 142 | if !have_ffmpeg() { |
| 143 | eprintln!("skipped: no ffmpeg to mux the ASS with"); |
| 144 | return; |
| 145 | } |
| 146 | let src = data("test_subs.ass"); |
| 147 | let mkv = Scratch::file("ve_ass_in_mkv", "mkv"); |
| 148 | ffmpeg(&["-i", s(&src), "-c:s", "copy", s(&mkv)]); |
| 149 | |
| 150 | let tracks = subtitle::of_matroska(&mkv).expect("the mkv is walked"); |
| 151 | assert_eq!(tracks.len(), 1, "one subtitle track"); |
| 152 | let track = &tracks[0]; |
| 153 | assert_eq!(track.refused, None, "the ASS track is readable"); |
| 154 | assert!(!track.is_bitmap(), "an ASS is text"); |
| 155 | assert_eq!( |
| 156 | track.cues, |
| 157 | expected(), |
| 158 | "the override tags are resolved on the way out of the mkv" |
| 159 | ); |
| 160 | } |