forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

ass_karaoke_export.rs111 lines, 4.9 KiBHistoryRawDownload
1//! An imported `.ass` with `\k` karaoke, burned into an export: the secondary colour before a
2//! syllable, the primary after it, in the pictures decoded back from the file.
3//!
4//! Set `KARA_OUT` to a directory to keep the exported file there.
5//!
6//! ```text
7//! cargo test -p engine --release --test ass_karaoke_export
8//! ```
9
10use std::path::{Path, PathBuf};
11use std::time::{Duration, Instant};
12
13use engine::decode::{DecodeSession, Frame};
14use engine::export::{self, ExportSettings, Format};
15use engine::project::Project;
16use engine::scratch::Scratch;
17use engine::subtitle;
18use engine::text::{Outline, TextStyle};
19
20const MEDIA: &str = "test_av.mp4";
21const FRAMES: u32 = 150;
22
23/// A cue from 0.5 s: `\k` syllables that start at 0.5 s and 1.5 s, then a one-second `\kf` sweep
24/// from 2.5 s; a green secondary and a white primary.
25const SCRIPT: &str = "\
26[Script Info]
27ScriptType: v4.00+
28PlayResX: 640
29PlayResY: 360
30
31[V4+ Styles]
32Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
33Style: Default,Arial,48,&H00FFFFFF,&H0000FF00,&H00000000,&H64000000,0,0,0,0,100,100,0,0,1,2,0,2,10,10,40,1
34
35[Events]
36Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
37Dialogue: 0,0:00:00.50,0:00:04.50,Default,,0,0,0,,{\\k100}MMMM {\\k100}WWWW {\\kf100}KKKK
38";
39
40fn asset(name: &str) -> PathBuf {
41 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../assets").join(name)
42}
43
44fn wait(handle: &engine::ExportHandle) -> engine::Result<()> {
45 let started = Instant::now();
46 while !handle.is_finished() {
47 assert!(started.elapsed() < Duration::from_secs(1800), "export did not finish");
48 std::thread::sleep(Duration::from_millis(20));
49 }
50 handle.result().expect("a finished export has an outcome")
51}
52
53/// Bottom-third pixels that are green, white in `f` and were not in `plain`: the caption's own.
54fn lit(f: &Frame, plain: &Frame) -> (usize, usize) {
55 let row = f.width as usize * 4;
56 let start = (f.height as usize * 2 / 3) * row;
57 let (mut g, mut w) = (0, 0);
58 let px = |f: &Frame| f.bgra[start..].as_chunks::<4>().0.iter().map(|p| (p[2], p[1], p[0])).collect::<Vec<_>>();
59 for ((r, gr, b), (pr, pg, pb)) in px(f).into_iter().zip(px(plain)) {
60 let green = |r: u8, g: u8, b: u8| g > 200 && r < 90 && b < 90;
61 let white = |r: u8, g: u8, b: u8| r > 235 && g > 235 && b > 235;
62 g += usize::from(green(r, gr, b) && !green(pr, pg, pb));
63 w += usize::from(white(r, gr, b) && !white(pr, pg, pb));
64 }
65 (g, w)
66}
67
68/// Exports the whole project and returns the frames `want` of the file, in order.
69fn exported(project: Project, burn: Option<(usize, TextStyle)>, keep: Option<&str>, want: &[u32]) -> Vec<Frame> {
70 let out = Scratch::file("ve_kara_export", "mp4");
71 let (meta, _) = engine::demux::Demuxer::open(&asset(MEDIA)).expect("probe the media");
72 let handle = export::start(
73 project,
74 meta,
75 &out,
76 &ExportSettings { format: Format::Mp4, burn_subtitles: burn, ..Default::default() },
77 None,
78 );
79 wait(&handle).expect("the export finishes");
80 if let (Some(name), Ok(dir)) = (keep, std::env::var("KARA_OUT")) {
81 std::fs::copy(&*out, Path::new(&dir).join(name)).expect("keep the export");
82 }
83 let (_, rx) = DecodeSession::open(&out).expect("decode the export back");
84 let mut got: Vec<Frame> = rx.into_iter().filter(|f| want.contains(&f.index)).take(want.len()).collect();
85 got.sort_by_key(|f| f.index);
86 assert_eq!(got.len(), want.len());
87 got
88}
89
90#[test]
91fn karaoke_is_burned_into_the_export() {
92 engine::envcfg::set("VE_SW", "1");
93 engine::envcfg::set("VE_SW_ENC", "1");
94 let script = Scratch::file("ve_kara_export", "ass");
95 std::fs::write(&*script, SCRIPT).expect("write the script");
96 let track = subtitle::open(Path::new(&*script), None);
97 assert_eq!(track.refused, None);
98 let project = Project::single(asset(MEDIA), FRAMES).with_subtitles(vec![track]);
99 let style = TextStyle { outline: Outline::Thin, boxed: false, ..TextStyle::default() };
100 // 30 fps: frame 27 is 0.9 s (the first syllable is sung, the other two not), 90 is 3.0 s
101 // (two sung, the sweep half way), 120 is 4.0 s (all sung).
102 let want = [27u32, 90, 120];
103 let plain = exported(project.clone(), None, None, &want);
104 let burned = exported(project, Some((0, style)), Some("kara.mp4"), &want);
105 let seen: Vec<_> = burned.iter().zip(&plain).map(|(f, p)| lit(f, p)).collect();
106 let (early, half, done) = (seen[0], seen[1], seen[2]);
107 assert!(early.0 > 100 && early.1 > 100, "one sung, two not: {seen:?}");
108 assert!(half.0 > 50 && half.1 > early.1, "more sung, the sweep still green on its right: {seen:?}");
109 assert!(half.0 < early.0, "less green than before: {seen:?}");
110 assert!(done.0 < 20 && done.1 > half.1, "all sung: {seen:?}");
111}