rightkit-transcript 0.4.2

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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! 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();
                }
            }
        }
        let mut word = TimedWord::new(text, w.start_ms, w.end_ms);
        word.source_ms = w.source_ms;
        out.push(word);
    }
    out
}

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

/// Width callback & usable text width for deterministic measured line breaking.
///
/// `measure_width` receives whole candidate lines, including spaces, in rendered
/// case. Return widths in the same units as `max_width_px` (normally pixels).
/// Capture font face & size in the callback; this crate has no font dependency.
/// The callback should return identical widths for identical text. Errors pass
/// through [`wrap_lines`] & [`build_cues_with_metrics`] unchanged.
#[derive(Clone, Copy)]
pub struct LineMetrics<'a> {
    pub measure_width: &'a dyn Fn(&str) -> Result<u64, String>,
    pub max_width_px: u64,
    /// Measure uppercase text & count its expanded characters without changing
    /// transcript text or returned display lines.
    pub uppercase: bool,
}

impl std::fmt::Debug for LineMetrics<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LineMetrics")
            .field("max_width_px", &self.max_width_px)
            .field("uppercase", &self.uppercase)
            .finish_non_exhaustive()
    }
}

impl LineMetrics<'_> {
    fn width(&self, text: &str) -> Result<u64, String> {
        if self.uppercase {
            (self.measure_width)(&text.to_uppercase())
        } else {
            (self.measure_width)(text)
        }
    }
}

fn fits(text: &str, max: usize, metrics: Option<&LineMetrics<'_>>) -> Result<bool, String> {
    let count = if metrics.is_some_and(|m| m.uppercase) {
        chars(&text.to_uppercase())
    } else {
        chars(text)
    };
    Ok(count <= max
        && match metrics {
            Some(m) => m.width(text)? <= m.max_width_px,
            None => true,
        })
}

/// Wrap whitespace-separated text, preserving case & word order.
///
/// With metrics, both character count & measured width must fit. An overlong
/// word splits at Unicode scalar boundaries; an individual glyph that cannot
/// fit returns an error. Without metrics, overlong words stay on one line &
/// character-count wrapping is infallible. Empty text returns no lines.
/// Wrapping does not change cue timing or word indices.
pub fn wrap_lines(
    text: &str,
    max_chars: usize,
    metrics: Option<&LineMetrics<'_>>,
) -> Result<Vec<String>, String> {
    let mut lines = Vec::new();
    let mut cur = String::new();
    for word in text.split_whitespace() {
        let joined = if cur.is_empty() {
            word.to_string()
        } else {
            format!("{cur} {word}")
        };
        if fits(&joined, max_chars, metrics)? {
            cur = joined;
            continue;
        }
        if !cur.is_empty() {
            lines.push(std::mem::take(&mut cur));
        }
        if metrics.is_none() || fits(word, max_chars, metrics)? {
            cur = word.to_string();
            continue;
        }
        // An overlong word keeps its timing, but occupies multiple display lines.
        for c in word.chars() {
            let joined = format!("{cur}{c}");
            if !fits(&joined, max_chars, metrics)? {
                if cur.is_empty() || !fits(&c.to_string(), max_chars, metrics)? {
                    return Err(format!("caption glyph {c:?} exceeds the safe line width"));
                }
                lines.push(std::mem::take(&mut cur));
            }
            cur.push(c);
        }
    }
    if !cur.is_empty() {
        lines.push(cur);
    }
    Ok(lines)
}

fn line_count(
    group: &[TimedWord],
    max: usize,
    metrics: Option<&LineMetrics<'_>>,
) -> Result<usize, String> {
    let text = group
        .iter()
        .map(|w| w.text.as_str())
        .collect::<Vec<_>>()
        .join(" ");
    Ok(wrap_lines(&text, max, metrics)?.len())
}

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

/// Duration of a group, first start to last end, in milliseconds. A reversed span
/// (end before start) is zero length, as in `group_words`, and an empty group is zero.
/// Neither can underflow.
fn span(group: &[TimedWord]) -> u64 {
    match (group.first(), group.last()) {
        (Some(first), Some(last)) => last.end_ms.saturating_sub(first.start_ms),
        _ => 0,
    }
}

fn needs_split(
    group: &[TimedWord],
    cfg: &CaptionConfig,
    metrics: Option<&LineMetrics<'_>>,
) -> Result<bool, String> {
    if group.len() <= 1 {
        return Ok(false);
    }
    let dur = span(group);
    if dur > cfg.max_cue_ms {
        return Ok(true);
    }
    if line_count(group, cfg.max_chars_per_line, metrics)? > cfg.max_lines {
        return Ok(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;
    Ok(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,
    metrics: Option<&LineMetrics<'_>>,
    out: &mut Vec<Vec<TimedWord>>,
) -> Result<(), String> {
    if group.is_empty() {
        return Ok(());
    }
    if !needs_split(&group, cfg, metrics)? {
        out.push(group);
        return Ok(());
    }
    let at = split_index(&group);
    let mut left = group;
    let right = left.split_off(at);
    split_rec(left, cfg, metrics, out)?;
    split_rec(right, cfg, metrics, 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,
            // Mapped words can overlap at cut points: an overlap is no gap.
            Some(last) => {
                w.start_ms.saturating_sub(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,
    metrics: Option<&LineMetrics<'_>>,
) -> Result<Vec<Vec<TimedWord>>, String> {
    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.saturating_sub(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, metrics)? {
                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.saturating_sub(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, metrics)? {
                        *prev = joined;
                        continue;
                    }
                }
            }
        }
        out.push(cur);
    }
    Ok(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,
    metrics: Option<&LineMetrics<'_>>,
) -> Result<Vec<String>, String> {
    let w: Vec<&str> = group.iter().map(|w| w.text.as_str()).collect();
    let greedy = wrap_lines(&w.join(" "), cfg.max_chars_per_line, metrics)?;
    let join = |r: &[usize]| r.iter().map(|&i| w[i]).collect::<Vec<_>>().join(" ");
    if greedy.len() != 2 || cfg.max_lines < 2 {
        return Ok(greedy);
    }
    let all: Vec<usize> = (0..w.len()).collect();
    let mut best: Option<((u8, u64), usize)> = None;
    for k in 1..w.len() {
        let l = join(&all[..k]);
        let r = join(&all[k..]);
        if !fits(&l, cfg.max_chars_per_line, metrics)?
            || !fits(&r, cfg.max_chars_per_line, metrics)?
        {
            continue;
        }
        let size = |s: &str| match metrics {
            Some(m) => m.width(s),
            None => Ok(chars(s) as u64),
        };
        let (left, right) = (size(&l)?, size(&r)?);
        let diff = left.abs_diff(right);
        let short = left.min(right);
        let total = left + right + u64::from(metrics.is_none());
        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));
        }
    }
    Ok(match best {
        Some((_, k)) => vec![join(&all[..k]), join(&all[k..])],
        None => greedy,
    })
}

/// 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;
                // An inverted word (end before start) is a zero-length span, not an underflow.
                let span = c.end.saturating_sub(c.start);
                if gap_over || c.count >= rules.max_words || span >= 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> {
    build_cues_with_metrics(words, cfg, None).expect("character-count layout is infallible")
}

/// Builds cues using caller-measured widths, or character limits without metrics.
///
/// Measured limits apply to splitting, short-cue merging & line balancing.
/// Character limits remain an upper bound. Returned text keeps input case;
/// indices refer to [`normalize_words`] output & timings stay on word boundaries
/// within [`ALIGNMENT_TOLERANCE_MS`]. A single word may occupy more than
/// `max_lines` when it must split to fit. Measurement & overwide-glyph errors
/// propagate without returning partial cues.
pub fn build_cues_with_metrics(
    words: &[TimedWord],
    cfg: &CaptionConfig,
    metrics: Option<&LineMetrics<'_>>,
) -> Result<Vec<Cue>, String> {
    let words = normalize_words(words);
    let mut groups = Vec::new();
    for phrase in phrase_groups(&words, cfg) {
        split_rec(phrase, cfg, metrics, &mut groups)?;
    }
    let groups = merge_short(groups, cfg, metrics)?;

    // 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, metrics)?,
            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.saturating_sub(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);
        }
    }
    Ok(cues)
}