Files
craft-libs/crates/captions/src/srt.rs
T
aiongg 328af6782e craft-timeline: craft-time is the one time model
craft-timeline's Tick, TimeRange, FrameRate, TimeDisplay, SMPTE field conversion and timecode
formatting are craft-time's, re-exported from time.rs as storytold's 5e24ea380 does, so
craft_timeline::time::* paths keep working. The timeline keeps what is about a timeline: TimeView
and VIEW_EXTENT_PAST_END, and TimeFormat as its time display (frame offset, compact feet + frames,
the audio samples and seconds readers) over craft-time's formatters and parsers. FrameRate's
locale-digit menu label is rates::frame_rate_label.

captions and edl use craft-time's field tuples, as storytold's do. The time trials that repeat
craft-time's own tests go; five that test craft-time code it did not test yet are in
crates/time/tests/timeline.rs.
2026-11-13 17:44:40 +00:00

193 lines
7.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! SubRip (`.srt`).
//!
//! ```text
//! 1
//! 00:00:01,000 --> 00:00:03,500
//! First line
//! <i>second line</i>
//!
//! 2
//! …
//! ```
//!
//! Reading accepts what real files contain: missing or wrong indexes, `.` instead of `,`, 1–3
//! digit fractions, missing hours, extra spaces, trailing SSA position coordinates
//! (`X1:… Y2:…`), and cues not separated by a blank line (a new cue starts at a timing line, or at
//! an integer line directly followed by one). Writing renumbers from 1 and rounds to milliseconds.
use crate::time::Tick;
use crate::{Cue, Document, Error, Result, format_clock, parse_clock};
/// Parse `start --> end[ settings]` (SRT or VTT timing line). Returns (start, end, settings).
pub fn parse_timing_line(line: &str) -> Option<(Tick, Tick, &str)> {
let (a, rest) = line.split_once("-->")?;
let start = parse_clock(a.trim())?;
let rest = rest.trim_start();
let end_len = rest.find(char::is_whitespace).unwrap_or(rest.len());
let end = parse_clock(&rest[..end_len])?;
Some((start, end, rest[end_len..].trim()))
}
fn is_index(line: &str) -> bool {
let l = line.trim();
!l.is_empty() && l.len() <= 10 && l.bytes().all(|b| b.is_ascii_digit())
}
const ZWSP: char = '\u{200B}';
/// Keep a text line from reading as a timing line: such a line gains a leading zero-width space.
fn escape(line: &str) -> String {
if parse_timing_line(line.trim_start_matches(ZWSP)).is_some() { format!("{ZWSP}{line}") } else { line.to_string() }
}
/// Reverse [`escape`]: drop one leading zero-width space when the rest still looks like a timing line.
fn unescape_line(line: &str) -> &str {
match line.strip_prefix(ZWSP) {
Some(r) if parse_timing_line(r.trim_start_matches(ZWSP)).is_some() => r,
_ => line,
}
}
pub fn parse(text: &str) -> Result<Document> {
let text = text.strip_prefix('\u{feff}').unwrap_or(text);
let lines: Vec<&str> = text.lines().collect();
let mut doc = Document::default();
let mut i = 0;
let mut found_any = false;
while i < lines.len() {
let Some((start, end, _settings)) = parse_timing_line(lines[i]) else {
let l = lines[i].trim();
if !l.is_empty() && !is_index(l) && found_any {
doc.warnings.push(format!("line {}: ignored stray text \"{l}\"", i + 1));
}
i += 1;
continue;
};
found_any = true;
i += 1;
let mut text_lines: Vec<&str> = Vec::new();
while i < lines.len() {
let l = lines[i];
if l.trim().is_empty() {
break;
}
if parse_timing_line(l).is_some() {
break;
}
if is_index(l) && lines.get(i + 1).is_some_and(|n| parse_timing_line(n).is_some()) {
break;
}
text_lines.push(unescape_line(l.trim_end()));
i += 1;
}
if end <= start {
doc.warnings.push(format!("cue at {} has end before start; skipped", format_clock(start, ',')));
continue;
}
doc.cues.push(Cue { start, end, text: text_lines.join("\n"), ..Default::default() });
}
if !found_any && !text.trim().is_empty() {
return Err(Error::NotFormat("SubRip"));
}
Ok(doc)
}
pub fn write(doc: &Document) -> String {
let mut out = String::new();
for (n, c) in doc.cues.iter().enumerate() {
out.push_str(&format!("{}\n{} --> {}\n", n + 1, format_clock(c.start, ','), format_clock(c.end, ',')));
// blank lines inside text would end the cue: drop them
for l in c.text.lines().filter(|l| !l.trim().is_empty()) {
// stored entities stay as they are, so a literal bracket does not become a tag on reimport
out.push_str(&escape(l));
out.push('\n');
}
out.push('\n');
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::TICKS_PER_MS;
fn ms(v: i64) -> Tick {
Tick(v * TICKS_PER_MS)
}
#[test]
fn entities_stay_distinct_from_style_tags_after_reimport() {
let mut d = Document::default();
d.cues.push(Cue { start: ms(1000), end: ms(2000), text: "1 &lt; 2 &amp; <i>x</i>".into(), ..Default::default() });
let encoded = write(&d);
assert_eq!(encoded, "1\n00:00:01,000 --> 00:00:02,000\n1 &lt; 2 &amp; <i>x</i>\n\n");
let decoded = parse(&encoded).unwrap();
assert_eq!(decoded.cues, d.cues);
assert_eq!(crate::plain_text(&decoded.cues[0].text), "1 < 2 & x");
}
#[test]
fn basic() {
let d = parse("1\n00:00:01,000 --> 00:00:02,500\nHello\n<i>World</i>\n\n2\n00:00:03,000 --> 00:00:04,000\nBye\n").unwrap();
assert_eq!(d.cues.len(), 2);
assert_eq!(d.cues[0].start, ms(1000));
assert_eq!(d.cues[0].end, ms(2500));
assert_eq!(d.cues[0].text, "Hello\n<i>World</i>");
assert_eq!(write(&d), "1\n00:00:01,000 --> 00:00:02,500\nHello\n<i>World</i>\n\n2\n00:00:03,000 --> 00:00:04,000\nBye\n\n");
}
#[test]
fn sloppy() {
// no indexes, period ms, short fractions, no hours, coordinates, no blank line between cues
let src = "00:00:01.5 --> 00:00:02.25 X1:10 X2:20 Y1:1 Y2:2\nA\n7\n00:03,000 --> 00:04,000\nB\n00:00:05,000-->00:00:06,000\n42\n\n\n";
let d = parse(src).unwrap();
let got: Vec<(i64, i64, &str)> = d.cues.iter().map(|c| (c.start.0 / TICKS_PER_MS, c.end.0 / TICKS_PER_MS, c.text.as_str())).collect();
assert_eq!(got, vec![(1500, 2250, "A"), (3000, 4000, "B"), (5000, 6000, "42")]);
}
#[test]
fn round_trips_text_line_that_looks_like_timing() {
let mut d = parse("1\n00:00:01,000 --> 00:00:02,000\nx\n").unwrap();
d.cues[0].text = "Shift:\n9:00 --> 10:00".into();
let back = parse(&write(&d)).unwrap();
assert_eq!(back.cues.len(), 1);
assert_eq!(back.cues[0].text, "Shift:\n9:00 --> 10:00");
}
#[test]
fn round_trips_arrow_and_marker_text() {
for text in ["a --&gt; b", "a --&amp;gt; b", "a --> b", "Shift:\n9:00 --> 10:00\n\u{200B}9:00 --> 10:00", "5\n9:00 --> 10:00"] {
let mut d = parse("1\n00:00:01,000 --> 00:00:02,000\nx\n").unwrap();
d.cues[0].text = text.into();
let back = parse(&write(&d)).unwrap();
assert_eq!(back.cues.len(), 1, "{text:?}");
assert_eq!(back.cues[0].text, text);
}
}
#[test]
fn ignores_byte_order_mark() {
let d = parse("\u{feff}00:00:01,000 --> 00:00:02,000\nHi\n").unwrap();
assert_eq!(d.cues.len(), 1);
assert_eq!(d.cues[0].start, ms(1000));
assert_eq!(d.cues[0].text, "Hi");
let d = parse("\u{feff}00:00:01,000 --> 00:00:02,000\nHi\n\n2\n00:00:03,000 --> 00:00:04,000\nBye\n").unwrap();
assert_eq!(d.cues.len(), 2);
}
#[test]
fn rejects_garbage() {
assert!(parse("hello world\nthis is not srt").is_err());
assert!(parse("").unwrap().cues.is_empty());
}
#[test]
fn skips_inverted_cue() {
let d = parse("1\n00:00:05,000 --> 00:00:04,000\nX\n\n2\n00:00:06,000 --> 00:00:07,000\nY\n").unwrap();
assert_eq!(d.cues.len(), 1);
assert_eq!(d.warnings.len(), 1);
}
}