forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

text_subs_ffmpeg.rs160 lines, 5.1 KiBHistoryRawDownload
1//! Text subtitles that ffmpeg writes, read back through edith's own subtitle
2//! call sites: an SRT, a WebVTT and an ASS muxed into Matroska with `-c:s copy`
3//! (or the matching codec), and a WebVTT written beside the media as a sidecar.
4//!
5//! The source of every fixture is one of the hand-written files in `tests/data`,
6//! so the cues the test expects are the ones those files state: start and end
7//! in microseconds, and the text with its markup resolved. The fixtures are
8//! generated at test time into a scratch directory, never checked in.
9//!
10//! Skipped loudly when ffmpeg is not on the box, the way the rest of the suite
11//! skips its reference tools.
12//!
13//! ```text
14//! cargo test -p engine --release --test text_subs_ffmpeg -- --nocapture
15//! ```
16
17use std::path::{Path, PathBuf};
18use std::process::Command;
19
20use engine::scratch::Scratch;
21use engine::subtitle::{self, Cue};
22
23fn data(name: &str) -> PathBuf {
24 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
25 .join("tests/data")
26 .join(name)
27}
28
29fn have_ffmpeg() -> bool {
30 Command::new("ffmpeg")
31 .arg("-version")
32 .output()
33 .is_ok_and(|o| o.status.success())
34}
35
36fn s(path: &Path) -> &str {
37 path.to_str().expect("a UTF-8 path")
38}
39
40/// Runs ffmpeg with `args` and fails the test with its stderr when it refuses.
41fn 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/// The three cues `data/test_subs.srt` and `data/test_subs.ass` both state, with
55/// the markup resolved: the italics of the `.ass` third line are not text, and
56/// the `\N` of its second one is a line break like the `.srt`'s.
57fn 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]
87fn 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 // The door a saved line comes back through reaches the same track.
104 let one = subtitle::open(&mkv, track.track);
105 assert_eq!(one.cues, expected());
106}
107
108#[test]
109fn 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]
124fn 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]
141fn 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}