rightkit-transcript 0.2.0

Product-neutral word-timed transcript contract, cue segmentation, and SRT/VTT/JSON import and export for Right Suite applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! Cue segmentation: phrase grouping, splitting, merging, line breaking.
//!
//! Donor: CutRight `cutright-captions` (itself ported from `video-media`'s
//! caption model). Deterministic: identical words + config give identical cues.

use serde::{Deserialize, Serialize};

use crate::align::ALIGNMENT_TOLERANCE_MS;
use crate::words::TimedWord;

/// Named segmentation profile (replaces CutRight's timeline `Preset`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Profile {
    /// Landscape video (YouTube): 42 chars x 2 lines.
    Standard,
    /// Vertical feed (TikTok): 20 chars x 2 lines, short cues.
    Vertical,
    /// Reels / square feed: 24 chars x 2 lines.
    Compact,
}

/// Per-profile segmentation limits.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct CaptionConfig {
    pub max_chars_per_line: usize,
    pub max_lines: usize,
    /// Reading-speed ceiling: characters per second of cue time.
    pub max_chars_per_second: f64,
    /// Cues shorter than this are merged with a neighbour when possible, else
    /// held on screen up to [`ALIGNMENT_TOLERANCE_MS`] into the following gap.
    pub min_cue_ms: u64,
    /// A cue never spans longer than this (it splits instead).
    pub max_cue_ms: u64,
    /// A gap between words larger than this always starts a new cue.
    pub max_gap_ms: u64,
}

impl CaptionConfig {
    pub fn for_profile(profile: Profile) -> Self {
        match profile {
            Profile::Standard => Self {
                max_chars_per_line: 42,
                max_lines: 2,
                max_chars_per_second: 21.0,
                min_cue_ms: 600,
                max_cue_ms: 7000,
                max_gap_ms: 700,
            },
            Profile::Vertical => Self {
                max_chars_per_line: 20,
                max_lines: 2,
                max_chars_per_second: 18.0,
                min_cue_ms: 600,
                max_cue_ms: 4500,
                max_gap_ms: 700,
            },
            Profile::Compact => Self {
                max_chars_per_line: 24,
                max_lines: 2,
                max_chars_per_second: 18.0,
                min_cue_ms: 600,
                max_cue_ms: 4500,
                max_gap_ms: 700,
            },
        }
    }
}

impl Default for CaptionConfig {
    fn default() -> Self {
        Self::for_profile(Profile::Standard)
    }
}

/// One caption cue on the output timeline. Cues never overlap.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Cue {
    pub start_ms: u64,
    pub end_ms: u64,
    /// Display lines (already broken; at most `max_lines` unless a single word
    /// is longer than a line).
    pub lines: Vec<String>,
    /// Index range `[first_word, last_word]` into the normalized word list.
    pub first_word: usize,
    pub last_word: usize,
}

impl Cue {
    pub fn text(&self) -> String {
        self.lines.join("\n")
    }
}

fn sentence_end(t: &str) -> bool {
    t.ends_with(['.', '?', '!', '\u{2026}'])
}

fn clause_end(t: &str) -> bool {
    t.ends_with([',', ';', ':', '\u{2014}', '\u{2013}'])
}

/// Trims, drops empty/zero-length words, collapses inner whitespace and clamps
/// overlaps so each word ends no later than the next starts.
pub fn normalize_words(words: &[TimedWord]) -> Vec<TimedWord> {
    let mut out: Vec<TimedWord> = Vec::with_capacity(words.len());
    for w in words {
        let text = w.text.split_whitespace().collect::<Vec<_>>().join(" ");
        if text.is_empty() || w.end_ms <= w.start_ms {
            continue;
        }
        if let Some(prev) = out.last_mut() {
            if prev.end_ms > w.start_ms {
                prev.end_ms = w.start_ms;
                if prev.end_ms <= prev.start_ms {
                    out.pop();
                }
            }
        }
        out.push(TimedWord::new(text, w.start_ms, w.end_ms));
    }
    out
}

fn chars(s: &str) -> usize {
    s.chars().count()
}

fn greedy_wrap(words: &[&str], max: usize) -> Vec<Vec<usize>> {
    // Returns word-index ranges per line as index lists (start..end pairs flattened).
    let mut lines: Vec<Vec<usize>> = Vec::new();
    let mut cur: Vec<usize> = Vec::new();
    let mut len = 0usize;
    for (i, w) in words.iter().enumerate() {
        let wl = chars(w);
        if cur.is_empty() {
            cur.push(i);
            len = wl;
        } else if len + 1 + wl <= max {
            cur.push(i);
            len += 1 + wl;
        } else {
            lines.push(std::mem::take(&mut cur));
            cur.push(i);
            len = wl;
        }
    }
    if !cur.is_empty() {
        lines.push(cur);
    }
    lines
}

fn line_count(group: &[TimedWord], max: usize) -> usize {
    let w: Vec<&str> = group.iter().map(|w| w.text.as_str()).collect();
    greedy_wrap(&w, max).len()
}

fn text_chars(group: &[TimedWord]) -> usize {
    group
        .iter()
        .map(|w| chars(&w.text) + 1)
        .sum::<usize>()
        .saturating_sub(1)
}

fn span(group: &[TimedWord]) -> u64 {
    group.last().unwrap().end_ms - group.first().unwrap().start_ms
}

fn needs_split(group: &[TimedWord], cfg: &CaptionConfig) -> bool {
    if group.len() <= 1 {
        return false;
    }
    let dur = span(group);
    if dur > cfg.max_cue_ms {
        return true;
    }
    if line_count(group, cfg.max_chars_per_line) > cfg.max_lines {
        return true;
    }
    // Judge reading speed against the time the cue will actually be on screen.
    let shown = dur.max(cfg.min_cue_ms) as f64 / 1000.0;
    text_chars(group) as f64 > cfg.max_chars_per_second * shown
}

/// Split index (start of the right half): prefers sentence then clause ends
/// near the middle, then the largest silence, falling back to the character
/// midpoint.
fn split_index(group: &[TimedWord]) -> usize {
    let total = text_chars(group) as i64;
    let mut best: Option<(i64, i64, usize)> = None; // (punct rank, distance, idx)
    let mut run = 0i64;
    for (i, w) in group.iter().enumerate().take(group.len() - 1) {
        run += chars(&w.text) as i64 + 1;
        let dist = (run - total / 2).abs();
        // Punctuation only counts when it leaves a reasonable remainder.
        let balanced = run * 4 >= total && (total - run) * 4 >= total;
        let rank = if balanced && sentence_end(&w.text) {
            0
        } else if balanced && clause_end(&w.text) {
            1
        } else {
            2
        };
        if best.is_none_or(|(r, d, _)| (rank, dist) < (r, d)) {
            best = Some((rank, dist, i + 1));
        }
    }
    best.map(|b| b.2).unwrap_or(1).clamp(1, group.len() - 1)
}

fn split_rec(group: Vec<TimedWord>, cfg: &CaptionConfig, out: &mut Vec<Vec<TimedWord>>) {
    if group.is_empty() {
        return;
    }
    if !needs_split(&group, cfg) {
        out.push(group);
        return;
    }
    let at = split_index(&group);
    let mut left = group;
    let right = left.split_off(at);
    split_rec(left, cfg, out);
    split_rec(right, cfg, out);
}

fn phrase_groups(words: &[TimedWord], cfg: &CaptionConfig) -> Vec<Vec<TimedWord>> {
    let mut groups: Vec<Vec<TimedWord>> = Vec::new();
    for w in words {
        let new = match groups.last().and_then(|g| g.last()) {
            None => true,
            Some(last) => w.start_ms - last.end_ms > cfg.max_gap_ms || sentence_end(&last.text),
        };
        if new {
            groups.push(vec![w.clone()]);
        } else {
            groups.last_mut().unwrap().push(w.clone());
        }
    }
    groups
}

/// Merges a too-short cue into its neighbour when the pair still fits the
/// limits, the gap is small and no sentence ends between them.
fn merge_short(groups: Vec<Vec<TimedWord>>, cfg: &CaptionConfig) -> Vec<Vec<TimedWord>> {
    let mut out: Vec<Vec<TimedWord>> = Vec::new();
    let mut iter = groups.into_iter().peekable();
    while let Some(mut cur) = iter.next() {
        while span(&cur) < cfg.min_cue_ms {
            let Some(next) = iter.peek() else { break };
            let gap = next[0].start_ms - cur.last().unwrap().end_ms;
            if gap > cfg.max_gap_ms || sentence_end(&cur.last().unwrap().text) {
                break;
            }
            let mut joined = cur.clone();
            joined.extend(next.iter().cloned());
            if needs_split(&joined, cfg) {
                break;
            }
            cur = joined;
            iter.next();
        }
        // Also fold a short leftover into the previous cue when it fits.
        if span(&cur) < cfg.min_cue_ms {
            if let Some(prev) = out.last_mut() {
                let gap = cur[0].start_ms - prev.last().unwrap().end_ms;
                if gap <= cfg.max_gap_ms && !sentence_end(&prev.last().unwrap().text) {
                    let mut joined = prev.clone();
                    joined.extend(cur.iter().cloned());
                    if !needs_split(&joined, cfg) {
                        *prev = joined;
                        continue;
                    }
                }
            }
        }
        out.push(cur);
    }
    out
}

/// Breaks a cue's words into display lines: greedy wrap, then for two lines the
/// balanced break that prefers a clause boundary.
fn break_lines(group: &[TimedWord], cfg: &CaptionConfig) -> Vec<String> {
    let w: Vec<&str> = group.iter().map(|w| w.text.as_str()).collect();
    let greedy = greedy_wrap(&w, cfg.max_chars_per_line);
    let join = |r: &[usize]| r.iter().map(|&i| w[i]).collect::<Vec<_>>().join(" ");
    if greedy.len() != 2 || cfg.max_lines < 2 {
        return greedy.iter().map(|l| join(l)).collect();
    }
    let all: Vec<usize> = (0..w.len()).collect();
    let total: usize = all.iter().map(|&i| chars(w[i]) + 1).sum::<usize>() - 1;
    let mut best: Option<((u8, usize), usize)> = None;
    for k in 1..w.len() {
        let l = join(&all[..k]);
        let r = join(&all[k..]);
        if chars(&l) > cfg.max_chars_per_line || chars(&r) > cfg.max_chars_per_line {
            continue;
        }
        let diff = chars(&l).abs_diff(chars(&r));
        let short = chars(&l).min(chars(&r));
        let punct = (sentence_end(w[k - 1]) || clause_end(w[k - 1])) && short * 4 >= total;
        let key = (u8::from(!punct), diff);
        if best.is_none_or(|(b, _)| key < b) {
            best = Some((key, k));
        }
    }
    match best {
        Some((_, k)) => vec![join(&all[..k]), join(&all[k..])],
        None => greedy.iter().map(|l| join(l)).collect(),
    }
}

/// Rules for [`group_words`], the lightweight cue grouper for engines whose
/// words are already clean (no line breaking, no reading-speed limits). Use
/// [`build_cues`] for caption-grade segmentation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct GroupRules {
    /// A cue closes once it holds this many words.
    pub max_words: usize,
    /// A cue closes once its span reaches this length.
    pub max_span_ms: u64,
    /// A gap larger than this between words starts a new cue.
    pub max_gap_ms: u64,
    /// Sentence-final punctuation (`.`, `?`, `!`) closes a cue holding at
    /// least this many words.
    pub sentence_min_words: usize,
    /// Cues shorter than this are widened to it (zero-width cues never export).
    pub min_cue_ms: u64,
}

impl Default for GroupRules {
    /// The engine-side defaults `rightkit-asr` has always used: 12 words, 7 s,
    /// 1 s gap, sentence close from 3 words, 80 ms floor (one encoder frame).
    fn default() -> Self {
        Self {
            max_words: 12,
            max_span_ms: 7_000,
            max_gap_ms: 1_000,
            sentence_min_words: 3,
            min_cue_ms: 80,
        }
    }
}

fn single_line_cue(
    text: &str,
    start_ms: u64,
    end_ms: u64,
    first: usize,
    last: usize,
    min_cue_ms: u64,
) -> Cue {
    Cue {
        start_ms,
        end_ms: end_ms.max(start_ms + min_cue_ms),
        lines: vec![text.trim().to_string()],
        first_word: first,
        last_word: last,
    }
}

/// Groups words (in order, not re-normalised) into single-line cues by
/// [`GroupRules`]. `first_word`/`last_word` index the input slice. A cue closes
/// on sentence-final punctuation (after `sentence_min_words` words), at
/// `max_words`, at `max_span_ms`, or on a gap over `max_gap_ms`.
pub fn group_words(words: &[TimedWord], rules: &GroupRules) -> Vec<Cue> {
    struct Open {
        text: String,
        start: u64,
        end: u64,
        first: usize,
        last: usize,
        count: usize,
    }
    let close =
        |o: &Open| single_line_cue(&o.text, o.start, o.end, o.first, o.last, rules.min_cue_ms);
    let open = |i: usize, w: &TimedWord| Open {
        text: w.text.clone(),
        start: w.start_ms,
        end: w.end_ms,
        first: i,
        last: i,
        count: 1,
    };
    let mut cues = Vec::new();
    let mut current: Option<Open> = None;
    for (i, word) in words.iter().enumerate() {
        match current.as_mut() {
            Some(c) => {
                let gap_over = word.start_ms > c.end && word.start_ms - c.end > rules.max_gap_ms;
                if gap_over || c.count >= rules.max_words || c.end - c.start >= rules.max_span_ms {
                    cues.push(close(c));
                    current = Some(open(i, word));
                } else {
                    c.text.push(' ');
                    c.text.push_str(&word.text);
                    c.end = word.end_ms;
                    c.last = i;
                    c.count += 1;
                }
            }
            None => current = Some(open(i, word)),
        }
        if current.as_ref().is_some_and(|c| {
            c.text.ends_with(['.', '?', '!']) && c.count >= rules.sentence_min_words
        }) {
            cues.push(close(&current.take().expect("cue present")));
        }
    }
    if let Some(c) = current {
        cues.push(close(&c));
    }
    cues
}

/// One single-line cue per span, in order (`first_word == last_word ==` the
/// span's index), widened to `min_cue_ms`. For callers that already hold cues
/// as [`TimedWord`] spans (text = cue text) and only need export.
pub fn cues_from_spans(spans: &[TimedWord], min_cue_ms: u64) -> Vec<Cue> {
    spans
        .iter()
        .enumerate()
        .map(|(i, s)| single_line_cue(&s.text, s.start_ms, s.end_ms, i, i, min_cue_ms))
        .collect()
}

/// Builds non-overlapping cues from output-time words.
///
/// Every cue starts at a word start and ends at a word end, except that a cue
/// shorter than `min_cue_ms` is held on screen up to [`ALIGNMENT_TOLERANCE_MS`]
/// past its last word (never into the next cue).
pub fn build_cues(words: &[TimedWord], cfg: &CaptionConfig) -> Vec<Cue> {
    let words = normalize_words(words);
    let mut groups = Vec::new();
    for phrase in phrase_groups(&words, cfg) {
        split_rec(phrase, cfg, &mut groups);
    }
    let groups = merge_short(groups, cfg);

    // Word index of the start of each group.
    let mut cues: Vec<Cue> = Vec::with_capacity(groups.len());
    let mut idx = 0usize;
    for g in &groups {
        let first = idx;
        idx += g.len();
        cues.push(Cue {
            start_ms: g[0].start_ms,
            end_ms: g.last().unwrap().end_ms,
            lines: break_lines(g, cfg),
            first_word: first,
            last_word: idx - 1,
        });
    }
    for i in 0..cues.len() {
        let next_start = cues.get(i + 1).map(|c| c.start_ms);
        let c = &mut cues[i];
        if c.end_ms - c.start_ms < cfg.min_cue_ms {
            let mut end = (c.start_ms + cfg.min_cue_ms).min(c.end_ms + ALIGNMENT_TOLERANCE_MS);
            if let Some(n) = next_start {
                end = end.min(n);
            }
            c.end_ms = end.max(c.end_ms);
        }
    }
    cues
}