Skip to main content

rightkit_transcript/
parse.rs

1//! SRT / WebVTT / timing-JSON import. Cues carry no word timings, so
2//! [`words_from_cues`] spreads each cue's words across its span by character
3//! count (flagged `EndSource::Interpolated` when put into a `Transcript`).
4
5use crate::words::TimedWord;
6
7#[derive(Debug, thiserror::Error, PartialEq, Eq)]
8pub enum ParseError {
9    #[error("bad timestamp {0:?}")]
10    Timestamp(String),
11    #[error("invalid timing JSON: {0}")]
12    Json(String),
13}
14
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct ParsedCue {
17    pub start_ms: u64,
18    pub end_ms: u64,
19    /// Cue text lines with markup tags removed and entities unescaped.
20    pub lines: Vec<String>,
21}
22
23fn parse_stamp(s: &str) -> Result<u64, ParseError> {
24    let bad = || ParseError::Timestamp(s.to_string());
25    let s = s.trim().replace(',', ".");
26    let (hms, frac) = s.split_once('.').ok_or_else(bad)?;
27    let parts: Vec<&str> = hms.split(':').collect();
28    let nums: Result<Vec<u64>, _> = parts.iter().map(|p| p.parse::<u64>()).collect();
29    let nums = nums.map_err(|_| bad())?;
30    let (h, m, sec) = match nums.as_slice() {
31        [h, m, s] => (*h, *m, *s),
32        [m, s] => (0, *m, *s),
33        _ => return Err(bad()),
34    };
35    let mut f = frac.to_string();
36    f.truncate(3);
37    while f.len() < 3 {
38        f.push('0');
39    }
40    let ms: u64 = f.parse().map_err(|_| bad())?;
41    Ok(((h * 60 + m) * 60 + sec) * 1000 + ms)
42}
43
44fn timing_line(line: &str) -> Option<Result<(u64, u64), ParseError>> {
45    let (a, rest) = line.split_once("-->")?;
46    // VTT allows cue settings after the end stamp.
47    let b = rest.split_whitespace().next().unwrap_or("");
48    Some(parse_stamp(a).and_then(|s| parse_stamp(b).map(|e| (s, e))))
49}
50
51fn clean_line(l: &str) -> String {
52    let mut out = String::new();
53    let mut in_tag = false;
54    for c in l.chars() {
55        match c {
56            '<' => in_tag = true,
57            '>' if in_tag => in_tag = false,
58            _ if !in_tag => out.push(c),
59            _ => {}
60        }
61    }
62    out.replace("&lt;", "<")
63        .replace("&gt;", ">")
64        .replace("&nbsp;", " ")
65        .replace("&amp;", "&")
66}
67
68fn parse_blocks(text: &str, vtt: bool) -> Result<Vec<ParsedCue>, ParseError> {
69    let text = text
70        .trim_start_matches('\u{feff}')
71        .replace("\r\n", "\n")
72        .replace('\r', "\n");
73    let mut cues = Vec::new();
74    for block in text.split("\n\n") {
75        let lines: Vec<&str> = block.lines().collect();
76        let Some(ti) = lines.iter().position(|l| l.contains("-->")) else {
77            continue;
78        };
79        if vtt
80            && lines.first().is_some_and(|l| {
81                l.starts_with("NOTE") || l.starts_with("STYLE") || l.starts_with("REGION")
82            })
83        {
84            continue;
85        }
86        let (start_ms, end_ms) = timing_line(lines[ti]).expect("line contains -->")?;
87        let body: Vec<String> = lines[ti + 1..]
88            .iter()
89            .map(|l| clean_line(l))
90            .filter(|l| !l.trim().is_empty())
91            .collect();
92        if body.is_empty() {
93            continue;
94        }
95        cues.push(ParsedCue {
96            start_ms,
97            end_ms,
98            lines: body,
99        });
100    }
101    Ok(cues)
102}
103
104/// Parse SubRip (CRLF/LF, BOM, optional numbering, `,` or `.` decimals).
105pub fn parse_srt(text: &str) -> Result<Vec<ParsedCue>, ParseError> {
106    parse_blocks(text, false)
107}
108
109/// Parse WebVTT (header, NOTE/STYLE blocks, cue ids/settings, `MM:SS.mmm` stamps).
110pub fn parse_vtt(text: &str) -> Result<Vec<ParsedCue>, ParseError> {
111    parse_blocks(text, true)
112}
113
114/// Spread each cue's whitespace-separated words across `[start, end)` in
115/// proportion to character count. Words never overlap and tile the cue.
116pub fn words_from_cues(cues: &[ParsedCue]) -> Vec<TimedWord> {
117    let mut out = Vec::new();
118    for c in cues {
119        let toks: Vec<&str> = c.lines.iter().flat_map(|l| l.split_whitespace()).collect();
120        let total: usize = toks.iter().map(|t| t.chars().count()).sum();
121        if toks.is_empty() || c.end_ms <= c.start_ms || total == 0 {
122            continue;
123        }
124        let span = c.end_ms - c.start_ms;
125        let mut acc = 0usize;
126        for t in toks {
127            let s = c.start_ms + span * acc as u64 / total as u64;
128            acc += t.chars().count();
129            let e = c.start_ms + span * acc as u64 / total as u64;
130            if e > s {
131                out.push(TimedWord::new(t, s, e));
132            }
133        }
134    }
135    out
136}
137
138/// Parse the shared timing JSON (`{"segments":[..],"words":[..]}`, seconds).
139/// Returns the word timings.
140pub fn parse_timing_json(s: &str) -> Result<Vec<TimedWord>, ParseError> {
141    let v: serde_json::Value =
142        serde_json::from_str(s).map_err(|e| ParseError::Json(e.to_string()))?;
143    let arr = v
144        .get("words")
145        .and_then(|w| w.as_array())
146        .ok_or_else(|| ParseError::Json("missing words array".into()))?;
147    let ms = |x: &serde_json::Value| x.as_f64().map(|f| (f.max(0.0) * 1000.0).round() as u64);
148    let mut out = Vec::with_capacity(arr.len());
149    for w in arr {
150        let (Some(t), Some(s), Some(e)) = (
151            w.get("text").and_then(|t| t.as_str()),
152            w.get("start").and_then(ms),
153            w.get("end").and_then(ms),
154        ) else {
155            return Err(ParseError::Json("word needs text/start/end".into()));
156        };
157        out.push(TimedWord::new(t, s, e));
158    }
159    Ok(out)
160}