youtube-legend-cli 0.4.0

Non-interactive Rust CLI that downloads YouTube subtitles through third-party providers, using a native Unix stdin/stdout interface.
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
//! SRT text extraction and YouTube URL / video-id parsing.

pub mod player_response;
pub mod srv3;
pub mod video_id;

use crate::error::{AppError, AppResult, NoSubtitleReason};
use crate::text::normalize_nfc;
use regex::Regex;
use std::sync::OnceLock;

static TIMESTAMP_RE: OnceLock<Regex> = OnceLock::new();
static INDEX_RE: OnceLock<Regex> = OnceLock::new();
// GAP-AUD-2026-038: noteey.com delivers transcripts as plain text with
// `MM:SS` or `HH:MM:SS` prefixes per line (no SRT framing, no arrow).
// The `OnceLock<Regex>` pattern mirrors `TIMESTAMP_RE` for consistency
// with the rest of this module.
static NOTEEY_TS_RE: OnceLock<Regex> = OnceLock::new();

fn timestamp_re() -> &'static Regex {
    TIMESTAMP_RE.get_or_init(|| {
        Regex::new(r"^\d{2}:\d{2}:\d{2}[,.]?\d{3}\s*-->\s*\d{2}:\d{2}:\d{2}[,.]?\d{3}")
            .expect("static SRT timestamp regex is valid")
    })
}

fn index_re() -> &'static Regex {
    INDEX_RE.get_or_init(|| Regex::new(r"^\d+$").expect("static SRT index regex is valid"))
}

fn noteey_ts_re() -> &'static Regex {
    NOTEEY_TS_RE.get_or_init(|| {
        // Matches `MM:SS` or `HH:MM:SS` followed by an optional
        // `.mmm`/`,mmm` fraction, optionally followed by whitespace.
        // Anchored at start of line so only leading timestamps are
        // stripped. Used for two purposes:
        //   1. Strip the inline timestamp prefix when timestamp and
        //      text share a line (`MM:SS texto`).
        //   2. Detect a standalone-timestamp line (matched without
        //      trailing text), which is paired with the next line.
        Regex::new(r"^\d{2}:\d{2}(?::\d{2})?[.,]?\d*")
            .expect("static noteey timestamp regex is valid")
    })
}

/// Convert a raw SRT body into plain text, one cue per paragraph.
///
/// Strips numeric indices, `HH:MM:SS,mmm --> HH:MM:SS,mmm` timestamp
/// lines, and joins multi-line cues with a single space. The output is
/// normalised to Unicode NFC.
///
/// # Errors
///
/// - [`AppError::InvalidInput`] when the body is empty or contains no
///   parseable cues.
/// - [`AppError::SubtitleTooLarge`] when the body exceeds 50 MiB.
///
/// # Examples
///
/// ```
/// use youtube_legend_cli::parse::srt_to_text;
///
/// let srt = "1\n00:00:01,000 --> 00:00:02,000\nHello world\n\n\
///            2\n00:00:03,000 --> 00:00:04,000\nSecond cue\n";
/// let text = srt_to_text(srt).unwrap();
/// assert_eq!(text, "Hello world\n\nSecond cue");
/// ```
#[tracing::instrument(level = "debug", err, skip(srt), fields(len_bytes = srt.len()))]
pub fn srt_to_text(srt: &str) -> AppResult<String> {
    if srt.is_empty() {
        return Err(AppError::InvalidInput("empty srt body".to_string()));
    }
    if srt.len() > 50 * 1024 * 1024 {
        return Err(AppError::SubtitleTooLarge(srt.len()));
    }

    let mut cues: Vec<String> = Vec::new();
    let mut current_lines: Vec<&str> = Vec::new();

    for line in logical_lines(srt) {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            if !current_lines.is_empty() {
                let text = join_cue_lines(&current_lines);
                if !text.is_empty() {
                    cues.push(normalize_nfc(&text));
                }
                current_lines.clear();
            }
            continue;
        }
        if index_re().is_match(trimmed) {
            continue;
        }
        if timestamp_re().is_match(trimmed) {
            continue;
        }
        current_lines.push(trimmed);
    }

    if !current_lines.is_empty() {
        let text = join_cue_lines(&current_lines);
        if !text.is_empty() {
            cues.push(normalize_nfc(&text));
        }
    }

    if cues.is_empty() {
        return Err(AppError::InvalidInput("srt has no valid cues".to_string()));
    }

    Ok(cues.join("\n\n"))
}

/// GAP-AUD-2026-038: clean a noteey.com transcript body.
///
/// Noteey returns the full transcript as plain text with `MM:SS` (or
/// `HH:MM:SS` for longer videos) leading each line, optionally with a
/// fractional `.mmm`/`,mmm` segment. Unlike SRT, there are no blank
/// lines, no arrow lines, and no numeric index — every line is a cue
/// with a single timestamp prefix.
///
/// This function:
/// 1. Normalises CRLF/CR to LF.
/// 2. Trims each line.
/// 3. Strips the leading timestamp via the internal regex.
/// 4. Drops marker-only lines (e.g. `[Music]`, `(Applause)`, empty
///    after strip) to reduce noise.
/// 5. Joins remaining lines with `\n` (single newline, not blank line
///    like SRT — noteey already separates cues).
/// 6. Normalises Unicode to NFC.
///
/// # Errors
///
/// - [`AppError::NoSubtitle`] when the body is empty or yields zero
///   lines after stripping.
/// - [`AppError::SubtitleTooLarge`] when the body exceeds 50 MiB.
#[tracing::instrument(level = "debug", err, skip(raw), fields(len_bytes = raw.len()))]
pub fn noteey_to_text(raw: &str) -> AppResult<String> {
    if raw.is_empty() {
        return Err(AppError::NoSubtitle(NoSubtitleReason::NotPublished));
    }
    if raw.len() > 50 * 1024 * 1024 {
        return Err(AppError::SubtitleTooLarge(raw.len()));
    }

    let mut out: Vec<String> = Vec::new();

    for line in logical_lines(raw) {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        // GAP-AUD-2026-047: noteey can render cues as alternating
        // timestamp-only and text-only lines (`00:00\ntexto`). A
        // standalone timestamp line is dropped; the text line below
        // it keeps the cue body. Lines that arrive with timestamp
        // AND text on the same line (`00:00 texto`) get the
        // timestamp stripped via `noteey_ts_re().replace`.
        // The regex is anchored at `^`, so a match always starts at
        // byte 0 and its end offset is the length of the timestamp
        // prefix. Slicing on that offset strips the prefix without
        // allocating, which `Regex::replace` + `to_owned` did not.
        let ts_end = noteey_ts_re().find(trimmed).map_or(0, |m| m.end());
        if ts_end == trimmed.len() {
            // Standalone timestamp with no text. Drop silently —
            // the next text line carries the cue body.
            continue;
        }
        // Text line. Strip any leading timestamp prefix.
        let stripped = trimmed[ts_end..].trim();
        // GAP-AUD-2026-062: strip leading `>>` speaker change markers
        // injected by YouTube auto-captions. These appear in interview
        // and podcast transcripts to indicate a new speaker.
        let stripped = stripped.strip_prefix(">>").map_or(stripped, str::trim);
        // GAP-AUD-2026-038 marker-line handling: after stripping
        // the leading timestamp, the line may consist only of a
        // parenthetical `(Applause)` or bracketed `[Music]` marker
        // with no spoken text. Drop those to avoid polluting the
        // transcript with stage directions.
        let is_marker_only = (stripped.starts_with('[') && stripped.ends_with(']'))
            || (stripped.starts_with('(') && stripped.ends_with(')'));
        if stripped.is_empty() || is_marker_only {
            continue;
        }
        out.push(normalize_nfc(stripped));
    }

    if out.is_empty() {
        return Err(AppError::NoSubtitle(NoSubtitleReason::NotPublished));
    }

    Ok(out.join("\n"))
}

fn join_cue_lines(lines: &[&str]) -> String {
    lines.join(" ")
}

/// Iterate the logical lines of `input` without copying the buffer.
///
/// Accepts `\n`, `\r\n`, and lone `\r` terminators, which is what the
/// former `input.replace("\r\n", "\n").replace('\r', "\n").lines()`
/// produced — at the cost of two full copies of the body. Splitting on
/// `\n` first and only then on any residual `\r` keeps `\r\n` from
/// yielding a spurious empty line (which the SRT parser would read as
/// a cue boundary).
fn logical_lines(input: &str) -> impl Iterator<Item = &str> {
    input
        .split('\n')
        .flat_map(|segment| segment.strip_suffix('\r').unwrap_or(segment).split('\r'))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_basic_srt() {
        let srt = "1\n00:00:01,000 --> 00:00:02,000\nHello world\n\n2\n00:00:03,000 --> 00:00:04,000\nSecond cue\n";
        let text = srt_to_text(srt).unwrap();
        assert_eq!(text, "Hello world\n\nSecond cue");
    }

    #[test]
    fn handles_crlf() {
        let srt = "1\r\n00:00:01,000 --> 00:00:02,000\r\nLine one\r\nLine two\r\n";
        let text = srt_to_text(srt).unwrap();
        assert_eq!(text, "Line one Line two");
    }

    // Line splitting is done in place, without normalising the body
    // into a second buffer, so the lone-`\r` (classic Mac) and mixed
    // terminator cases need explicit coverage.
    #[test]
    fn handles_lone_cr_line_endings() {
        let srt = "1\r00:00:01,000 --> 00:00:02,000\rLine one\r\r2\r00:00:03,000 --> 00:00:04,000\rLine two\r";
        let text = srt_to_text(srt).unwrap();
        assert_eq!(text, "Line one\n\nLine two");
    }

    #[test]
    fn handles_mixed_cr_crlf_and_lf() {
        let srt = "1\r\n00:00:01,000 --> 00:00:02,000\nLine one\rLine two\n";
        let text = srt_to_text(srt).unwrap();
        assert_eq!(text, "Line one Line two");
    }

    #[test]
    fn handles_single_cue_without_trailing_newline() {
        let srt = "1\n00:00:01,000 --> 00:00:02,000\nOnly cue";
        let text = srt_to_text(srt).unwrap();
        assert_eq!(text, "Only cue");
    }

    #[test]
    fn rejects_body_without_cues() {
        let err = srt_to_text("1\n00:00:01,000 --> 00:00:02,000\n").unwrap_err();
        assert!(matches!(err, AppError::InvalidInput(_)));
    }

    #[test]
    fn handles_multiline_cue() {
        let srt = "1\n00:00:01,000 --> 00:00:05,000\nLine one\nLine two\nLine three\n";
        let text = srt_to_text(srt).unwrap();
        assert_eq!(text, "Line one Line two Line three");
    }

    #[test]
    fn rejects_empty() {
        let err = srt_to_text("").unwrap_err();
        assert!(matches!(err, AppError::InvalidInput(_)));
    }

    #[test]
    fn rejects_over_50mb() {
        let big = "a".repeat(51 * 1024 * 1024);
        let err = srt_to_text(&big).unwrap_err();
        assert!(matches!(err, AppError::SubtitleTooLarge(_)));
    }

    #[test]
    fn handles_accented_text() {
        let srt = "1\n00:00:01,000 --> 00:00:02,000\nOlá mundo com acentuação\n";
        let text = srt_to_text(srt).unwrap();
        assert_eq!(text, "Olá mundo com acentuação");
    }

    // GAP-AUD-2026-038 / GAP-AUD-2026-047: noteey_to_text regression
    // tests. The function returns clean plain text — timestamps are
    // stripped and stage-direction markers (`[Music]`, `(Applause)`)
    // are dropped.
    #[test]
    fn noteey_clean_strips_leading_timestamp_prefix() {
        let raw = "00:00 hello\n00:03 world\n00:05 again\n";
        let text = noteey_to_text(raw).unwrap();
        assert_eq!(text, "hello\nworld\nagain");
    }

    #[test]
    fn noteey_clean_handles_milliseconds() {
        let raw = "00:00.123 hello\n00:03.456 world\n";
        let text = noteey_to_text(raw).unwrap();
        assert_eq!(text, "hello\nworld");
    }

    #[test]
    fn noteey_clean_handles_hh_mm_ss_format() {
        let raw = "01:02:03 long video cue\n01:02:08 next cue\n";
        let text = noteey_to_text(raw).unwrap();
        assert_eq!(text, "long video cue\nnext cue");
    }

    #[test]
    fn noteey_clean_skips_empty_lines() {
        let raw = "00:00 first\n\n00:05 second\n\n\n00:10 third\n";
        let text = noteey_to_text(raw).unwrap();
        assert_eq!(text, "first\nsecond\nthird");
    }

    #[test]
    fn noteey_clean_skips_marker_only_lines() {
        let raw = "00:00 [Music]\n00:03 hello\n00:05 (Applause)\n";
        let text = noteey_to_text(raw).unwrap();
        assert_eq!(text, "hello");
    }

    #[test]
    fn noteey_clean_handles_accented_text() {
        let raw = "00:00 Olá mundo\n00:03 ção não\n";
        let text = noteey_to_text(raw).unwrap();
        assert_eq!(text, "Olá mundo\nção não");
    }

    #[test]
    fn noteey_clean_handles_crlf() {
        let raw = "00:00 first\r\n00:03 second\r\n";
        let text = noteey_to_text(raw).unwrap();
        assert_eq!(text, "first\nsecond");
    }

    // GAP-AUD-2026-047: noteey renders cues as alternating timestamp
    // and text lines (live SPA layout). The parser silently drops
    // the standalone timestamp and keeps only the text.
    #[test]
    fn noteey_clean_joins_alternating_timestamp_and_text_lines() {
        let raw = "00:00\nhello\n00:03\nworld\n00:05\nagain\n";
        let text = noteey_to_text(raw).unwrap();
        assert_eq!(text, "hello\nworld\nagain");
    }

    #[test]
    fn noteey_clean_drops_pending_ts_without_followup_text() {
        // A trailing timestamp with no text is dropped silently.
        let raw = "00:00 hello\n00:03 world\n00:05\n";
        let text = noteey_to_text(raw).unwrap();
        assert_eq!(text, "hello\nworld");
    }

    #[test]
    fn noteey_clean_rejects_empty() {
        let err = noteey_to_text("").unwrap_err();
        assert!(matches!(err, AppError::NoSubtitle(_)));
    }

    #[test]
    fn noteey_clean_rejects_only_whitespace() {
        let err = noteey_to_text("   \n\n  \t\n").unwrap_err();
        assert!(matches!(err, AppError::NoSubtitle(_)));
    }

    #[test]
    fn noteey_clean_rejects_only_timestamps() {
        // No cue text after timestamps — purely marker lines.
        let raw = "00:00 [Music]\n00:05 (Applause)\n";
        let err = noteey_to_text(raw).unwrap_err();
        assert!(matches!(err, AppError::NoSubtitle(_)));
    }

    #[test]
    fn noteey_clean_respects_50mb_cap() {
        let big = format!("00:00 {}\n", "a".repeat(51 * 1024 * 1024));
        let err = noteey_to_text(&big).unwrap_err();
        assert!(matches!(err, AppError::SubtitleTooLarge(_)));
    }

    #[test]
    fn noteey_clean_handles_lone_cr() {
        let raw = "00:00 first\r00:03 second\r";
        let text = noteey_to_text(raw).unwrap();
        assert_eq!(text, "first\nsecond");
    }

    #[test]
    fn noteey_clean_handles_single_cue_without_trailing_newline() {
        let raw = "00:00 only cue";
        let text = noteey_to_text(raw).unwrap();
        assert_eq!(text, "only cue");
    }

    // GAP-AUD-2026-062: speaker change markers `>>` must be stripped.
    #[test]
    fn noteey_clean_strips_speaker_change_markers() {
        let raw = "00:00 hello\n00:03 >> world\n00:05 >> again\n";
        let text = noteey_to_text(raw).unwrap();
        assert_eq!(text, "hello\nworld\nagain");
    }

    #[test]
    fn noteey_clean_strips_speaker_marker_without_timestamp() {
        let raw = "hello\n>> world\n>> again\n";
        let text = noteey_to_text(raw).unwrap();
        assert_eq!(text, "hello\nworld\nagain");
    }

    #[test]
    fn noteey_clean_drops_speaker_marker_only_line() {
        let raw = "00:00 hello\n>>\n00:05 world\n";
        let text = noteey_to_text(raw).unwrap();
        assert_eq!(text, "hello\nworld");
    }
}