1use std::cmp::Reverse;
22use std::collections::{BTreeSet, BinaryHeap};
23
24use crate::{
25 CaptionCue, MAX_CLIPS_PER_TRACK, MAX_PROJECT_JSON_BYTES, MAX_RUN_CLASSES,
26 MAX_TEXT_CONTENT_BYTES, MAX_TEXT_RUNS, MAX_TRACKS, RunTime, TextContent, TextRun,
27 valid_class_name,
28};
29use alpha2_time::{MediaTime, TICKS_PER_SECOND, TimeRange};
30
31pub const MAX_CAPTION_FILE_BYTES: usize = MAX_PROJECT_JSON_BYTES;
34
35pub const MAX_CAPTION_FILE_CUES: usize = 65_536;
38
39pub const MAX_CUE_BLOCK_BYTES: usize = 65_536;
41
42pub const MAX_CUE_BLOCK_LINES: usize = 1_024;
44
45pub const MAX_OPEN_TAGS: usize = 32;
47
48pub const MAX_HELD_CLASSES: usize = 64;
50
51#[derive(Clone, Debug, Default, PartialEq)]
53pub struct ImportedCaptions {
54 pub cues: Vec<CaptionCue>,
56 pub skipped: usize,
58 pub notes: ImportNotes,
60}
61
62#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
64pub struct ImportNotes {
65 pub extra_voices: usize,
68 pub dropped_timestamps: usize,
70 pub dropped_classes: usize,
73 pub flattened: usize,
76}
77
78#[derive(Clone, Copy, Debug, Eq, PartialEq)]
80#[non_exhaustive]
81pub enum CaptionImportError {
82 MissingWebVttHeader,
84 FileTooLarge {
86 bytes: usize,
88 maximum: usize,
90 },
91 TooManyCues {
93 maximum: usize,
95 },
96 TooManyRows {
99 rows: usize,
101 maximum: usize,
103 },
104}
105
106impl std::fmt::Display for CaptionImportError {
107 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
108 match self {
109 Self::MissingWebVttHeader => formatter.write_str("a WebVTT file begins with WEBVTT"),
110 Self::FileTooLarge { bytes, maximum } => write!(
111 formatter,
112 "the subtitle file is {bytes} bytes; at most {maximum} are read"
113 ),
114 Self::TooManyCues { maximum } => write!(
115 formatter,
116 "the subtitle file holds more than {maximum} cues"
117 ),
118 Self::TooManyRows { rows, maximum } => write!(
119 formatter,
120 "the captions need {rows} caption tracks; a project holds at most {maximum} tracks"
121 ),
122 }
123 }
124}
125
126impl std::error::Error for CaptionImportError {}
127
128const TICKS_PER_MILLISECOND: i64 = TICKS_PER_SECOND / 1_000;
129
130pub fn parse_srt(text: &str) -> Result<ImportedCaptions, CaptionImportError> {
136 check_file_size(text)?;
137 let mut imported = ImportedCaptions::default();
138 for block in Blocks::new(text) {
139 let Some(block) = block else {
140 imported.skipped += 1;
141 continue;
142 };
143 let mut lines = block.iter().copied();
144 let Some(mut first) = lines.next() else {
145 continue;
146 };
147 if !first.contains("-->") {
149 match lines.next() {
150 Some(line) if first.trim().chars().all(|c| c.is_ascii_digit()) => first = line,
151 _ => {
152 imported.skipped += 1;
153 continue;
154 }
155 }
156 }
157 let body = lines.collect::<Vec<_>>();
158 let range = timing(first);
159 let marked = range.map(|_| srt_markup(&body));
160 imported.read(range, marked)?;
161 }
162 imported.cues.sort_by_key(|cue| cue.timeline_range.start());
163 Ok(imported)
164}
165
166pub fn parse_webvtt(text: &str) -> Result<ImportedCaptions, CaptionImportError> {
173 check_file_size(text)?;
174 let text = text.strip_prefix('\u{feff}').unwrap_or(text);
175 let header_ok = text
176 .strip_prefix("WEBVTT")
177 .is_some_and(|rest| rest.is_empty() || rest.starts_with([' ', '\t', '\n', '\r']));
178 if !header_ok {
179 return Err(CaptionImportError::MissingWebVttHeader);
180 }
181 let mut imported = ImportedCaptions::default();
182 let mut blocks = Blocks::new(text);
183 let header = blocks.next().flatten().unwrap_or_default();
186 let cue_under_header = header
187 .iter()
188 .position(|line| line.contains("-->"))
189 .map(|start| Some(header[start..].to_vec()));
190 for block in cue_under_header.into_iter().chain(blocks) {
191 let Some(block) = block else {
192 imported.skipped += 1;
193 continue;
194 };
195 let first = block[0];
196 if ["NOTE", "STYLE", "REGION"].iter().any(|keyword| {
197 first
198 .strip_prefix(keyword)
199 .is_some_and(|rest| rest.is_empty() || rest.starts_with([' ', '\t']))
200 }) {
201 continue;
202 }
203 let (timing_line, body) = if first.contains("-->") {
205 (first, &block[1..])
206 } else if block.len() > 1 && block[1].contains("-->") {
207 (block[1], &block[2..])
208 } else {
209 imported.skipped += 1;
210 continue;
211 };
212 let range = timing(timing_line);
213 let marked = range.map(|range| vtt_markup(&body.join("\n"), range));
214 imported.read(range, marked)?;
215 }
216 imported.cues.sort_by_key(|cue| cue.timeline_range.start());
217 Ok(imported)
218}
219
220impl ImportedCaptions {
221 fn read(
224 &mut self,
225 range: Option<TimeRange>,
226 marked: Option<Marked>,
227 ) -> Result<(), CaptionImportError> {
228 let (Some(range), Some(marked)) = (range, marked) else {
229 self.skipped += 1;
230 return Ok(());
231 };
232 let Some(text) = cue_text(&marked.text) else {
233 self.skipped += 1;
234 return Ok(());
235 };
236 if self.cues.len() == MAX_CAPTION_FILE_CUES {
237 return Err(CaptionImportError::TooManyCues {
238 maximum: MAX_CAPTION_FILE_CUES,
239 });
240 }
241 let mut cue = CaptionCue::new(range, text);
242 let notes = &mut self.notes;
243 notes.extra_voices += marked.notes.extra_voices;
244 notes.dropped_timestamps += marked.notes.dropped_timestamps;
245 notes.dropped_classes += marked.notes.dropped_classes;
246 notes.flattened += marked.notes.flattened;
247 cue.speaker = marked.speaker;
248 if let Some(runs) = marked.runs.filter(|runs| !runs.is_empty()) {
249 cue.text = TextContent::Runs(runs);
250 }
251 self.cues.push(cue);
252 Ok(())
253 }
254}
255
256fn check_file_size(text: &str) -> Result<(), CaptionImportError> {
257 if text.len() > MAX_CAPTION_FILE_BYTES {
258 return Err(CaptionImportError::FileTooLarge {
259 bytes: text.len(),
260 maximum: MAX_CAPTION_FILE_BYTES,
261 });
262 }
263 Ok(())
264}
265
266pub fn caption_rows(cues: &[CaptionCue]) -> Result<Vec<Vec<CaptionCue>>, CaptionImportError> {
274 let mut sorted = cues.to_vec();
275 sorted.sort_by_key(|cue| cue.timeline_range.start());
276 let mut rows: Vec<Vec<CaptionCue>> = Vec::new();
277 let mut ended = BTreeSet::<usize>::new();
280 let mut ending = BinaryHeap::<Reverse<(MediaTime, usize)>>::new();
281 for cue in sorted {
282 let start = cue.timeline_range.start();
283 while let Some(&Reverse((end, row))) = ending.peek()
284 && end <= start
285 {
286 ending.pop();
287 ended.insert(row);
288 }
289 let row = ended.pop_first().unwrap_or_else(|| {
290 rows.push(Vec::new());
291 rows.len() - 1
292 });
293 let end = cue.timeline_range.end();
294 rows[row].push(cue);
295 if rows[row].len() < MAX_CLIPS_PER_TRACK {
296 ending.push(Reverse((end, row)));
297 }
298 }
299 if rows.len() > MAX_TRACKS {
300 return Err(CaptionImportError::TooManyRows {
301 rows: rows.len(),
302 maximum: MAX_TRACKS,
303 });
304 }
305 Ok(rows)
306}
307
308struct Blocks<'a> {
311 lines: Lines<'a>,
312}
313
314impl<'a> Blocks<'a> {
315 fn new(text: &'a str) -> Self {
316 Self {
317 lines: Lines {
318 text: text.strip_prefix('\u{feff}').unwrap_or(text),
319 at: 0,
320 pending_blanks: 0,
321 done: false,
322 },
323 }
324 }
325}
326
327impl<'a> Iterator for Blocks<'a> {
328 type Item = Option<Vec<&'a str>>;
329
330 fn next(&mut self) -> Option<Self::Item> {
331 let mut block = Vec::new();
332 let mut bytes = 0_usize;
333 let mut oversized = false;
334 for line in self.lines.by_ref() {
335 if line.trim().is_empty() {
336 if oversized || !block.is_empty() {
337 break;
338 }
339 continue;
340 }
341 if oversized {
342 continue;
343 }
344 bytes += line.len() + 1;
345 if bytes > MAX_CUE_BLOCK_BYTES || block.len() == MAX_CUE_BLOCK_LINES {
346 oversized = true;
347 block = Vec::new();
348 continue;
349 }
350 block.push(line);
351 }
352 if oversized {
353 Some(None)
354 } else {
355 (!block.is_empty()).then_some(Some(block))
356 }
357 }
358}
359
360struct Lines<'a> {
363 text: &'a str,
364 at: usize,
365 pending_blanks: usize,
367 done: bool,
368}
369
370impl<'a> Iterator for Lines<'a> {
371 type Item = &'a str;
372
373 fn next(&mut self) -> Option<&'a str> {
374 if self.pending_blanks > 0 {
375 self.pending_blanks -= 1;
376 return Some("");
377 }
378 if self.done {
379 return None;
380 }
381 let bytes = self.text.as_bytes();
382 let start = self.at;
383 let Some(end) = bytes[start..]
384 .iter()
385 .position(|&byte| matches!(byte, b'\n' | b'\r'))
386 .map(|offset| start + offset)
387 else {
388 self.done = true;
389 return Some(&self.text[start..]);
390 };
391 if bytes[end] == b'\n' {
392 self.at = end + 1;
393 } else {
394 let returns = bytes[end..]
395 .iter()
396 .take_while(|&&byte| byte == b'\r')
397 .count();
398 self.at = end + returns;
399 if bytes.get(self.at) == Some(&b'\n') {
400 self.at += 1;
401 } else {
402 self.pending_blanks = returns - 1;
403 }
404 }
405 Some(&self.text[start..end])
406 }
407}
408
409fn timing(line: &str) -> Option<TimeRange> {
411 let (start, rest) = line.split_once("-->")?;
412 let end = rest.split_whitespace().next()?;
413 let (start, end) = (timestamp(start.trim())?, timestamp(end)?);
414 TimeRange::new(start, end)
415 .ok()
416 .filter(|range| !range.is_empty())
417}
418
419fn timestamp(text: &str) -> Option<MediaTime> {
422 let (clock, millis) = text.split_once([',', '.'])?;
423 if millis.len() != 3 || !millis.bytes().all(|byte| byte.is_ascii_digit()) {
424 return None;
425 }
426 let fields = clock.split(':').collect::<Vec<_>>();
427 let number = |field: &str| {
428 (!field.is_empty() && field.bytes().all(|byte| byte.is_ascii_digit()))
429 .then(|| field.parse::<i64>().ok())
430 .flatten()
431 };
432 let (hours, minutes, seconds) = match fields.as_slice() {
433 [hours, minutes, seconds] => (number(hours)?, number(minutes)?, number(seconds)?),
434 [minutes, seconds] => (0, number(minutes)?, number(seconds)?),
435 _ => return None,
436 };
437 if minutes > 59 || seconds > 59 {
438 return None;
439 }
440 let milliseconds = ((hours.checked_mul(60)?.checked_add(minutes)?)
441 .checked_mul(60)?
442 .checked_add(seconds)?)
443 .checked_mul(1_000)?
444 .checked_add(millis.parse::<i64>().ok()?)?;
445 let ticks = milliseconds.checked_mul(TICKS_PER_MILLISECOND)?;
446 (ticks <= alpha2_time::MAX_SERIALIZED_TICKS).then(|| MediaTime::from_ticks(ticks))
447}
448
449fn cue_text(text: &str) -> Option<String> {
451 let text = text.trim_matches('\n').to_owned();
452 (!text.trim().is_empty() && text.len() <= MAX_TEXT_CONTENT_BYTES).then_some(text)
453}
454
455fn decode_references(text: &str) -> String {
460 let mut output = String::with_capacity(text.len());
461 let mut rest = text;
462 while let Some(start) = rest.find('&') {
463 output.push_str(&rest[..start]);
464 rest = &rest[start + 1..];
465 let length = rest
466 .bytes()
467 .take_while(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'#' | b'+'))
468 .count();
469 let decoded = (rest.as_bytes().get(length) == Some(&b';'))
470 .then(|| reference(&rest[..length]))
471 .flatten();
472 match decoded {
473 Some(character) => {
474 output.push(character);
475 rest = &rest[length + 1..];
476 }
477 None => output.push('&'),
478 }
479 }
480 output.push_str(rest);
481 output
482}
483
484fn reference(name: &str) -> Option<char> {
486 match name {
487 "amp" => Some('&'),
488 "lt" => Some('<'),
489 "gt" => Some('>'),
490 "nbsp" => Some('\u{a0}'),
491 "lrm" => Some('\u{200e}'),
492 "rlm" => Some('\u{200f}'),
493 _ => name
494 .strip_prefix("#x")
495 .or_else(|| name.strip_prefix("#X"))
496 .and_then(|hex| u32::from_str_radix(hex, 16).ok())
497 .or_else(|| name.strip_prefix('#').and_then(|dec| dec.parse().ok()))
498 .and_then(char::from_u32),
499 }
500}
501
502struct Marked {
505 text: String,
506 speaker: Option<String>,
507 runs: Option<Vec<TextRun>>,
508 notes: ImportNotes,
509}
510
511struct Runs {
515 text: String,
516 pieces: Vec<(String, Vec<String>, usize)>,
517 tracking: bool,
518 notes: ImportNotes,
519}
520
521impl Runs {
522 fn new() -> Self {
523 Self {
524 text: String::new(),
525 pieces: Vec::new(),
526 tracking: true,
527 notes: ImportNotes::default(),
528 }
529 }
530
531 fn flatten(&mut self) {
533 if self.tracking {
534 self.tracking = false;
535 self.pieces = Vec::new();
536 self.notes.flattened += 1;
537 }
538 }
539
540 fn push(&mut self, text: &str, classes: &[String], segment: usize) {
541 self.text.push_str(text);
542 if !self.tracking {
543 return;
544 }
545 let text = if self.pieces.is_empty() {
547 text.trim_start_matches('\n')
548 } else {
549 text
550 };
551 if text.is_empty() {
552 return;
553 }
554 match self.pieces.last_mut() {
555 Some((held, open, at)) if open.as_slice() == classes && *at == segment => {
556 held.push_str(text);
557 }
558 _ => {
559 if self.pieces.len() > MAX_TEXT_RUNS + 1 {
561 self.flatten();
562 return;
563 }
564 self.pieces
565 .push((text.to_owned(), classes.to_vec(), segment));
566 }
567 }
568 }
569
570 fn finish(mut self, speaker: Option<String>, bounds: &[i64]) -> Marked {
574 while let Some(last) = self.pieces.last_mut() {
575 let kept = last.0.trim_end_matches('\n').len();
576 last.0.truncate(kept);
577 if last.0.is_empty() {
578 self.pieces.pop();
579 } else {
580 break;
581 }
582 }
583 if self.pieces.len() > MAX_TEXT_RUNS {
584 self.flatten();
585 }
586 let timed = bounds.len() > 2;
587 let runs = (self.tracking
588 && (timed
589 || self
590 .pieces
591 .iter()
592 .any(|(_, classes, _)| !classes.is_empty())))
593 .then(|| {
594 self.pieces
595 .into_iter()
596 .map(|(text, mut class, segment)| {
597 if class.len() > MAX_RUN_CLASSES {
598 self.notes.dropped_classes += class.len() - MAX_RUN_CLASSES;
599 class.truncate(MAX_RUN_CLASSES);
600 }
601 TextRun {
602 text,
603 class,
604 time: timed.then(|| RunTime {
605 start: MediaTime(bounds[segment]),
606 end: MediaTime(bounds[segment + 1]),
607 }),
608 }
609 })
610 .collect()
611 });
612 Marked {
613 text: self.text,
614 speaker,
615 runs,
616 notes: self.notes,
617 }
618 }
619}
620
621fn tag_classes(names: &str, notes: &mut ImportNotes) -> Vec<String> {
624 names
625 .split('.')
626 .filter(|name| !name.is_empty())
627 .filter_map(|name| {
628 if valid_class_name(name) {
629 Some(name.to_owned())
630 } else {
631 notes.dropped_classes += 1;
632 None
633 }
634 })
635 .collect()
636}
637
638fn vtt_markup(body: &str, range: TimeRange) -> Marked {
643 let start = range.start().ticks();
644 let duration = range.duration().map_or(0, MediaTime::ticks);
645 let mut runs = Runs::new();
646 let mut speaker = None;
647 let mut open: Vec<(&str, usize)> = Vec::new();
649 let mut classes: Vec<String> = Vec::new();
651 let mut bounds = vec![0_i64];
652 let mut rest = body;
653 loop {
654 let Some(at) = rest.find('<') else {
655 runs.push(&decode_references(rest), &classes, bounds.len() - 1);
656 break;
657 };
658 runs.push(&decode_references(&rest[..at]), &classes, bounds.len() - 1);
659 let Some(end) = rest[at..].find('>') else {
660 break;
661 };
662 let tag = &rest[at + 1..at + end];
663 rest = &rest[at + end + 1..];
664 if let Some(name) = tag.strip_prefix('/') {
665 let name = name.trim();
666 if let Some(index) = open.iter().rposition(|(open, _)| *open == name) {
667 classes.truncate(open[index].1);
668 open.truncate(index);
669 }
670 continue;
671 }
672 if tag.starts_with(|character: char| character.is_ascii_digit()) {
673 match timestamp(tag.trim()) {
675 Some(time)
676 if time.ticks() - start > *bounds.last().unwrap_or(&0)
677 && time.ticks() - start < duration =>
678 {
679 if runs.tracking {
680 bounds.push(time.ticks() - start);
681 }
682 }
683 _ => runs.notes.dropped_timestamps += 1,
684 }
685 continue;
686 }
687 let (head, annotation) = tag
689 .split_once([' ', '\t'])
690 .map_or((tag, ""), |(head, annotation)| (head, annotation.trim()));
691 let (name, names) = head.split_once('.').unwrap_or((head, ""));
692 let given = match name {
693 "v" => {
694 if speaker.is_none() && !annotation.is_empty() {
695 speaker = Some(annotation.to_owned());
696 } else if !annotation.is_empty() {
697 runs.notes.extra_voices += 1;
698 }
699 let mut given = vec!["voice".to_owned()];
700 given.extend(tag_classes(names, &mut runs.notes));
701 given
702 }
703 "c" => tag_classes(names, &mut runs.notes),
704 "b" | "i" | "u" => {
705 let mut given = vec![name.to_owned()];
706 given.extend(tag_classes(names, &mut runs.notes));
707 given
708 }
709 _ => Vec::new(),
710 };
711 if !runs.tracking {
712 continue;
713 }
714 if open.len() == MAX_OPEN_TAGS {
715 runs.flatten();
716 continue;
717 }
718 open.push((name, classes.len()));
719 for class in given {
720 if !classes.contains(&class) {
721 if classes.len() == MAX_HELD_CLASSES {
722 runs.flatten();
723 break;
724 }
725 classes.push(class);
726 }
727 }
728 }
729 bounds.push(duration);
730 let speaker = speaker.filter(|speaker| speaker.len() <= crate::MAX_CAPTION_NAME_BYTES);
731 runs.finish(speaker, &bounds)
732}
733
734fn srt_markup(body: &[&str]) -> Marked {
737 let mut runs = Runs::new();
738 let mut open: Vec<String> = Vec::new();
739 for (index, line) in body.iter().enumerate() {
740 if index > 0 {
741 runs.push("\n", &open, 0);
742 }
743 let line = strip_overrides(line);
744 let mut rest = line.as_str();
745 while let Some(start) = rest.find('<') {
746 runs.push(&rest[..start], &open, 0);
747 let tag = &rest[start + 1..];
748 let closing = tag.starts_with('/');
749 let name = tag.strip_prefix('/').unwrap_or(tag);
750 match tag.find(['<', '>']) {
751 Some(end)
752 if tag.as_bytes()[end] == b'>'
753 && name.starts_with(|character: char| character.is_ascii_alphabetic()) =>
754 {
755 let tag_name = name[..name.len().min(end - usize::from(closing))]
756 .split([' ', '\t'])
757 .next()
758 .unwrap_or_default()
759 .to_ascii_lowercase();
760 if matches!(tag_name.as_str(), "b" | "i" | "u") {
761 if closing {
762 if let Some(index) = open.iter().rposition(|open| *open == tag_name) {
763 open.remove(index);
764 }
765 } else if !open.contains(&tag_name) {
766 open.push(tag_name);
767 }
768 }
769 rest = &tag[end + 1..];
770 }
771 _ => {
772 runs.push("<", &open, 0);
773 rest = tag;
774 }
775 }
776 }
777 runs.push(rest, &open, 0);
778 }
779 runs.finish(None, &[0, 0])
780}
781
782fn strip_overrides(line: &str) -> String {
784 let mut output = String::with_capacity(line.len());
785 let mut rest = line;
786 while let Some(start) = rest.find("{\\") {
787 output.push_str(&rest[..start]);
788 match rest[start..].find('}') {
789 Some(end) => rest = &rest[start + end + 1..],
790 None => {
791 rest = "";
792 }
793 }
794 }
795 output.push_str(rest);
796 output
797}
798
799#[cfg(test)]
800#[path = "caption_import_tests.rs"]
801mod tests;