use crate::transcribe::Segment;
pub fn srt_time(ms: u64) -> String {
let (h, m, s, f) = parts(ms);
format!("{h:02}:{m:02}:{s:02},{f:03}")
}
pub fn vtt_time(ms: u64) -> String {
let (h, m, s, f) = parts(ms);
format!("{h:02}:{m:02}:{s:02}.{f:03}")
}
fn parts(ms: u64) -> (u64, u64, u64, u64) {
(ms / 3_600_000, ms / 60_000 % 60, ms / 1000 % 60, ms % 1000)
}
fn lines(text: &str) -> String {
text.lines().map(str::trim).filter(|l| !l.is_empty()).collect::<Vec<_>>().join("\n")
}
pub fn srt(segments: &[Segment]) -> String {
let mut out = String::new();
let mut n = 0;
for s in segments {
let text = lines(&s.text.replace("-->", "->"));
if text.is_empty() {
continue;
}
n += 1;
out.push_str(&format!("{n}\n{} --> {}\n{text}\n\n", srt_time(s.start_ms), srt_time(s.end_ms.max(s.start_ms))));
}
out
}
pub fn vtt(segments: &[Segment]) -> String {
let mut out = String::from("WEBVTT\n\n");
for s in segments {
let text = lines(&s.text.replace('&', "&").replace('<', "<").replace('>', ">"));
if text.is_empty() {
continue;
}
if s.coarse {
out.push_str("NOTE coarse: this cue spans a whole piece of audio\n\n");
}
out.push_str(&format!("{} --> {}\n{text}\n\n", vtt_time(s.start_ms), vtt_time(s.end_ms.max(s.start_ms))));
}
out
}