subtitler 2.6.1

Parse, convert, validate, edit, and generate subtitles in 15 formats (SRT, VTT, ASS/SSA, MicroDVD, SubViewer, TTML, SBV, LRC, SAMI, MPL2, SCC, EBU STL, DFXP, Whisper JSON). Full CLI included.
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
use crate::model::Subtitle;
use regex::Regex;
use std::sync::LazyLock;

static RE_MULTI_SPACE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r" {2,}").unwrap());

static RE_TRAILING_SPACE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[ \t]+$").unwrap());

static RE_MULTI_NEWLINE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\n{3,}").unwrap());

static RE_SPACE_BEFORE_PUNCT: LazyLock<Regex> =
  LazyLock::new(|| Regex::new(r" +([,!.;:?])").unwrap());

static RE_REPEATED_PUNCT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"([.!?,]){4,}").unwrap());

static RE_ELLIPSIS_SPACED: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\.\s*\.\s*\.").unwrap());

static RE_HI_PAREN: LazyLock<Regex> =
  LazyLock::new(|| Regex::new(r"\s*[\(\[][^)\]]{2,60}[\)\]]").unwrap());

static RE_HI_BRACKET: LazyLock<Regex> =
  LazyLock::new(|| Regex::new(r"\s*\[[^\]]{2,60}\]").unwrap());

static RE_SPEAKER_LABEL: LazyLock<Regex> =
  LazyLock::new(|| Regex::new(r"^(?:>>|>\s|-\s|[A-Z ]{2,20}:)\s*").unwrap());

static RE_MUSIC_NOTE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[♪♫♬]").unwrap());

static RE_OCR_PATTERNS: LazyLock<Vec<(Regex, &'static str)>> = LazyLock::new(|| {
  vec![
    (Regex::new(r"\brn\b").unwrap(), "m"),
    (Regex::new(r"(\d)rn(\w)").unwrap(), "${1}m${2}"),
    (Regex::new(r"(\d)O(\d)").unwrap(), "${1}0${2}"),
    (Regex::new(r"(\d)l(\d)").unwrap(), "${1}1${2}"),
    (Regex::new(r"([a-z])0([a-z])").unwrap(), "${1}o${2}"),
  ]
});

pub fn normalize_whitespace(text: &str) -> String {
  let lines: Vec<String> = text
    .lines()
    .map(|line| {
      let trimmed = line.trim();
      RE_MULTI_SPACE.replace_all(trimmed, " ").to_string()
    })
    .collect();
  let mut result = lines.join("\n");
  result = RE_MULTI_NEWLINE.replace_all(&result, "\n\n").to_string();
  result = RE_TRAILING_SPACE.replace_all(&result, "").to_string();
  result.trim().to_string()
}

pub fn normalize_quotes(text: &str) -> String {
  text
    .replace(['\u{201C}', '\u{201D}'], "\"")
    .replace(['\u{2018}', '\u{2019}'], "'")
    .replace('\u{2013}', "-")
    .replace('\u{2014}', "--")
}

pub fn normalize_punctuation(text: &str) -> String {
  let mut result = text.to_string();
  result = RE_SPACE_BEFORE_PUNCT.replace_all(&result, "$1").to_string();
  result = RE_REPEATED_PUNCT.replace_all(&result, "$1$1$1").to_string();
  result = RE_ELLIPSIS_SPACED.replace_all(&result, "").to_string();
  result = result.replace("....", "");
  result
}

pub fn fix_ocr_errors(text: &str) -> String {
  let mut result = text.to_string();
  for (re, rep) in RE_OCR_PATTERNS.iter() {
    result = re.replace_all(&result, *rep).to_string();
  }
  result
}

pub fn strip_hearing_impaired(text: &str) -> String {
  let mut result = text.to_string();
  result = RE_HI_PAREN.replace_all(&result, "").to_string();
  result = RE_HI_BRACKET.replace_all(&result, "").to_string();
  result = RE_SPEAKER_LABEL.replace_all(&result, "").to_string();
  result = RE_MUSIC_NOTE.replace_all(&result, "").to_string();
  result = result.trim().to_string();
  if result.is_empty() {
    return String::new();
  }
  normalize_whitespace(&result)
}

pub fn normalize_text(text: &str) -> String {
  let result = normalize_quotes(text);
  let result = normalize_punctuation(&result);
  normalize_whitespace(&result)
}

pub fn normalize_subtitle(sub: &mut Subtitle) {
  sub.text = normalize_text(&sub.text);
}

/// Optimize line breaks in subtitle text. Splits long lines at natural
/// boundaries (punctuation, conjunctions) to improve readability.
///
/// Each resulting line is at most `max_chars` characters, and line lengths
/// are balanced when possible.
pub fn optimize_line_breaks(text: &str, max_chars: usize) -> String {
  let mut result_parts: Vec<String> = Vec::new();
  let mut queue: Vec<String> = text.lines().map(|l| l.trim().to_string()).collect();
  // Process front-to-back (FIFO)
  let mut idx = 0;

  while idx < queue.len() {
    let line = std::mem::take(&mut queue[idx]);
    if line.chars().count() <= max_chars {
      result_parts.push(line);
      idx += 1;
      continue;
    }

    // Try to find natural break points
    let words: Vec<&str> = line.split_whitespace().collect();
    let best_break = find_best_split(&words, max_chars);

    match best_break {
      Some(split_idx) => {
        result_parts.push(words[..split_idx].join(" "));
        let remaining = words[split_idx..].join(" ");
        if remaining.is_empty() {
          idx += 1; // nothing left, advance
        } else {
          queue[idx] = remaining; // process remainder next iteration
        }
      }
      None => {
        // No natural break found, hard split at char boundary
        let first: String = line.chars().take(max_chars).collect();
        let rest: String = line.chars().skip(max_chars).collect();
        result_parts.push(first);
        if rest.is_empty() {
          idx += 1;
        } else {
          queue[idx] = rest;
        }
      }
    }
  }

  result_parts.join("\n")
}

/// Filter text to keep only characters from a specified language Unicode block.
///
/// Supported `lang`: `"en"` (Latin), `"zh"` (CJK Unified), `"ja"` (CJK +
/// Hiragana + Katakana), `"ko"` (CJK + Hangul), `"ar"` (Arabic), `"he"`
/// (Hebrew). Unknown `lang` returns the input unchanged.
pub fn filter_language(text: &str, lang: &str) -> String {
  let keep = match lang {
    "en" => |c: char| {
      c.is_ascii_alphabetic() || c.is_ascii_digit() || c.is_ascii_punctuation() || c == ' '
    },
    "zh" => |c: char| {
      ('\u{4E00}'..='\u{9FFF}').contains(&c) || c.is_ascii_digit() || c == ' ' || c == '\n'
    },
    "ja" => |c: char| {
      ('\u{4E00}'..='\u{9FFF}').contains(&c)
        || ('\u{3040}'..='\u{309F}').contains(&c)
        || ('\u{30A0}'..='\u{30FF}').contains(&c)
        || c.is_ascii_digit()
        || c == ' '
        || c == '\n'
    },
    "ko" => |c: char| {
      ('\u{AC00}'..='\u{D7AF}').contains(&c) || c.is_ascii_digit() || c == ' ' || c == '\n'
    },
    "ar" => |c: char| {
      ('\u{0600}'..='\u{06FF}').contains(&c) || c.is_ascii_digit() || c == ' ' || c == '\n'
    },
    "he" => |c: char| {
      ('\u{0590}'..='\u{05FF}').contains(&c) || c.is_ascii_digit() || c == ' ' || c == '\n'
    },
    _ => return text.to_string(),
  };
  text.chars().filter(|&c| keep(c)).collect()
}

/// Merge short lines (≤ `max_chars` characters) by removing newlines within
/// each subtitle's text. Lines longer than `max_chars` or containing explicit
/// breaks (double-newline) are preserved.
pub fn merge_short_lines(text: &str, max_chars: usize) -> String {
  let lines: Vec<&str> = text.lines().collect();
  let mut result = Vec::new();
  let mut buf = String::new();

  for line in lines {
    if line.is_empty() {
      // Double newline — paragraph break, flush buffer
      if !buf.is_empty() {
        result.push(std::mem::take(&mut buf));
      }
      result.push(String::new());
      continue;
    }
    if line.len() <= max_chars && !buf.is_empty() {
      buf.push(' ');
      buf.push_str(line);
    } else if line.len() <= max_chars {
      buf.push_str(line);
    } else {
      // Long line — keep as is
      if !buf.is_empty() {
        result.push(std::mem::take(&mut buf));
      }
      result.push(line.to_string());
    }
  }
  if !buf.is_empty() {
    result.push(buf);
  }
  result.join("\n")
}

/// Replace all newlines with spaces, collapsing multiple spaces.
pub fn remove_all_newlines(text: &str) -> String {
  let s = text.replace('\n', " ");
  let mut result = String::with_capacity(s.len());
  let mut prev_space = false;
  for c in s.chars() {
    if c == ' ' {
      if !prev_space {
        result.push(' ');
        prev_space = true;
      }
    } else {
      result.push(c);
      prev_space = false;
    }
  }
  result.trim().to_string()
}

/// Replace all newlines with a custom separator string.
pub fn replace_newlines(text: &str, separator: &str) -> String {
  text.lines().collect::<Vec<_>>().join(separator)
}

/// Find the best word boundary to split a sequence of words.
/// Returns the index after the last word that fits in `max_chars`.
fn find_best_split(words: &[&str], max_chars: usize) -> Option<usize> {
  if words.is_empty() {
    return None;
  }

  // Build cumulative character lengths
  let mut cum: Vec<usize> = Vec::with_capacity(words.len());
  let mut total = 0usize;
  for w in words {
    total += w.len() + 1; // +1 for space
    cum.push(total);
  }

  // Find the last word that fits in max_chars
  let mut last_fit = None;
  let mut preferred = None;

  for (i, &c) in cum.iter().enumerate() {
    let len = c.saturating_sub(1); // remove trailing space
    if len <= max_chars {
      last_fit = Some(i + 1); // index after this word
      // Check if this is a preferred break point
      let word = words[i];
      if word.ends_with(',') || word.ends_with(';') || word.ends_with(':') {
        preferred = Some(i + 1);
      }
      // Check for conjunctions that would start the next line
      if i + 1 < words.len() && ["and", "or", "but", "so", "yet", "for"].contains(&words[i + 1]) {
        preferred = Some(i + 1);
      }
    } else {
      break;
    }
  }

  // Prefer breaks at punctuation/conjunctions, fall back to last fitting word
  preferred.or(last_fit).filter(|&i| i < words.len())
}

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

  #[test]
  fn test_normalize_whitespace() {
    assert_eq!(normalize_whitespace("hello   world"), "hello world");
    assert_eq!(normalize_whitespace("  hello  "), "hello");
    assert_eq!(normalize_whitespace("a\n\n\n\nb"), "a\n\nb");
  }

  #[test]
  fn test_normalize_quotes() {
    assert_eq!(normalize_quotes("\u{201C}hello\u{201D}"), "\"hello\"");
    assert_eq!(normalize_quotes("\u{2018}it's\u{2019}"), "'it's'");
    assert_eq!(normalize_quotes("a\u{2013}b"), "a-b");
  }

  #[test]
  fn test_normalize_punctuation() {
    assert_eq!(normalize_punctuation("hello , world"), "hello, world");
    assert_eq!(normalize_punctuation("what????"), "what???");
    assert_eq!(normalize_punctuation(". . ."), "");
  }

  #[test]
  fn test_fix_ocr_errors() {
    assert_eq!(fix_ocr_errors("12O456"), "120456");
    assert_eq!(fix_ocr_errors("1l0"), "110");
    assert_eq!(fix_ocr_errors("w0rd"), "word");
  }

  #[test]
  fn test_strip_hearing_impaired() {
    assert_eq!(
      strip_hearing_impaired("Hello (LAUGHS) world"),
      "Hello world"
    );
    assert_eq!(strip_hearing_impaired("[APPLAUSE] Nice"), "Nice");
    assert_eq!(strip_hearing_impaired(">> Hello there"), "Hello there");
    assert_eq!(strip_hearing_impaired("JOHN: What's up?"), "What's up?");
    assert_eq!(strip_hearing_impaired("♪ Music ♪"), "Music");
  }

  #[test]
  fn test_normalize_subtitle() {
    let mut sub = Subtitle::new(0, 1000, "Hello   \u{201C}world\u{201D} !");
    normalize_subtitle(&mut sub);
    assert_eq!(sub.text, "Hello \"world\"!");
  }

  #[test]
  fn test_optimize_line_breaks_short() {
    // Short line stays unchanged
    assert_eq!(optimize_line_breaks("Hello World", 42), "Hello World");
  }

  #[test]
  fn test_optimize_line_breaks_long() {
    let long =
      "This is a very long subtitle line that definitely exceeds the maximum character limit";
    let result = optimize_line_breaks(long, 42);
    // Should be split into multiple lines
    assert!(result.contains('\n'));
    // Each line should be at most ~42 chars (allowing word boundaries)
    for line in result.lines() {
      assert!(
        line.chars().count() <= 42 + 10,
        "line too long: '{}' ({} chars)",
        line,
        line.chars().count()
      );
    }
  }

  #[test]
  fn test_optimize_line_breaks_preserves_content() {
    let input = "The quick brown fox jumps over the lazy dog and runs away";
    let result = optimize_line_breaks(input, 20);
    // All words should be present in the output
    for word in input.split_whitespace() {
      assert!(
        result.contains(word),
        "word '{}' lost in line break optimization",
        word
      );
    }
  }

  #[test]
  fn test_filter_language_english() {
    let input = "Hello 你好 World 世界";
    assert_eq!(filter_language(input, "en"), "Hello  World ");
  }

  #[test]
  fn test_filter_language_chinese() {
    let input = "Hello 你好 World 世界";
    assert_eq!(filter_language(input, "zh"), " 你好  世界");
  }

  #[test]
  fn test_merge_short_lines() {
    let input = "short\nline\nhere\nLONG_LINE_EXCEEDS_TEN_CHARS";
    let result = merge_short_lines(input, 10);
    assert!(result.contains("short line here"));
    assert!(result.contains("LONG_LINE_EXCEEDS_TEN_CHARS"));
  }

  #[test]
  fn test_remove_all_newlines() {
    assert_eq!(remove_all_newlines("a\nb\nc"), "a b c");
    assert_eq!(remove_all_newlines("a\n\nb"), "a b"); // double newline → single space
  }

  #[test]
  fn test_replace_newlines() {
    assert_eq!(replace_newlines("a\nb\nc", "|"), "a|b|c");
  }
}

#[cfg(test)]
#[test]
fn test_optimize_line_breaks_order() {
  let result = optimize_line_breaks("abc def ghijklmnop", 5);
  assert_eq!(
    result, "abc\ndef\nghijk\nlmnop",
    "got: {:?} — lines are in wrong order",
    result
  );
}