craft-captions: PJS, RealText and JACOsub subtitle formats (#1557) (#1788)

This commit is contained in:
el-j authored and GitHub committed 2026-11-16 21:30:57 -05:00
1 parent 996a6b5042
commit cfad8f0bcc
7 files changed
+860 -2

No files matched your search

+242
View File
@@ -0,0 +1,242 @@
//! JACOsub (`.jss`, `.js`): multi-feature scriptable subtitle format.
//!
//! ```text
//! # JACOsub file
//! #TimeRes 100
//! 0:00:01.00 0:00:03.00 D0 First line\nsecond line
//! ```
//!
//! Lines beginning with `#` are comments or header directives. Timed caption lines begin with
//! `START STOP [DIRECTIVE] Text`, where timestamps are `H:MM:SS.FF` (or `@units`) relative to
//! the declared `#TimeRes` (default 30). Inside the text, `\n` represents a newline.
use crate::text::{escape_markup, ms_to_tick, plain_text, tick_to_ms};
use crate::{Cue, Document, Error, Result};
/// Parse a JACOsub timestamp `H:MM:SS.FF` or `@units` into milliseconds.
pub(crate) fn parse_time(s: &str, time_res: i64) -> Option<i64> {
let s = s.trim();
if s.is_empty() {
return None;
}
if let Some(units_str) = s.strip_prefix('@') {
let units: i64 = units_str.trim().parse().ok()?;
let time_res = if time_res > 0 { time_res } else { 30 };
return Some(units.saturating_mul(1000) / time_res);
}
let (h_str, rest) = s.split_once(':')?;
let (m_str, rest) = rest.split_once(':')?;
let (s_str, f_str) = rest.split_once('.')?;
let h: i64 = h_str.trim().parse().ok()?;
let m: i64 = m_str.trim().parse().ok()?;
let sec: i64 = s_str.trim().parse().ok()?;
let frac: i64 = f_str.trim().parse().ok()?;
let time_res = if time_res > 0 { time_res } else { 30 };
let frac_ms = frac.saturating_mul(1000) / time_res;
let total_secs = h.saturating_mul(3600).saturating_add(m.saturating_mul(60)).saturating_add(sec);
Some(total_secs.saturating_mul(1000).saturating_add(frac_ms))
}
/// Format milliseconds into JACOsub timestamp string `H:MM:SS.cc` (at 100 centiseconds/sec).
pub(crate) fn format_time(ms: i64) -> String {
let ms = ms.max(0);
let total_secs = ms / 1000;
let centis = (ms % 1000) / 10;
let hours = total_secs / 3600;
let mins = (total_secs % 3600) / 60;
let secs = total_secs % 60;
format!("{hours}:{mins:02}:{secs:02}.{centis:02}")
}
/// Check if a line is a JACOsub timed caption line: `H:MM:SS.FF H:MM:SS.FF ...` or `@units @units ...`.
pub(crate) fn is_timed_line(line: &str) -> bool {
let line = line.trim();
if line.starts_with('#') {
return false;
}
let mut parts = line.split_whitespace();
let Some(t0) = parts.next() else { return false };
let Some(t1) = parts.next() else { return false };
(t0.contains(':') && t0.contains('.') && t1.contains(':') && t1.contains('.')) || (t0.starts_with('@') && t1.starts_with('@'))
}
pub fn parse(text: &str) -> Result<Document> {
let mut doc = Document::default();
let mut time_res: i64 = 30; // standard default
let mut shift_ms: i64 = 0;
let mut found = false;
for (n, line) in text.lines().enumerate() {
let line = line.trim();
if line.is_empty() {
continue;
}
if let Some(cmd) = line.strip_prefix('#') {
let cmd_lower = cmd.to_ascii_lowercase();
if cmd_lower.starts_with("timeres") || cmd_lower.starts_with('t') {
let parts: Vec<&str> = cmd.split_whitespace().collect();
if parts.len() >= 2
&& let Ok(res) = parts[1].parse::<i64>()
&& res > 0
{
time_res = res;
}
} else if cmd_lower.starts_with("shift") || cmd_lower.starts_with('s') {
let parts: Vec<&str> = cmd.split_whitespace().collect();
if parts.len() >= 2 {
let s_val = parts[1].trim();
let (neg, s_raw) = if let Some(stripped) = s_val.strip_prefix('-') {
(true, stripped)
} else if let Some(stripped) = s_val.strip_prefix('+') {
(false, stripped)
} else {
(false, s_val)
};
if let Some(ms) = parse_time(s_raw, time_res) {
shift_ms = if neg { -ms } else { ms };
}
}
}
continue;
}
if !is_timed_line(line) {
continue;
}
let mut parts = line.split_whitespace();
let Some(start_str) = parts.next() else { continue };
let Some(end_str) = parts.next() else { continue };
let start_ms = match parse_time(start_str, time_res) {
Some(ms) => ms.saturating_add(shift_ms).max(0),
None => {
doc.warnings.push(format!("line {}: invalid start timestamp, skipped", n + 1));
continue;
}
};
let end_ms = match parse_time(end_str, time_res) {
Some(ms) => ms.saturating_add(shift_ms).max(0),
None => {
doc.warnings.push(format!("line {}: invalid end timestamp, skipped", n + 1));
continue;
}
};
found = true;
// Skip to after the end timestamp in the original line to preserve exact spaces
let after_end_pos = match line.find(end_str) {
Some(idx) => idx + end_str.len(),
None => continue,
};
let remainder = &line[after_end_pos..];
let trimmed_rem = remainder.trim_start();
// Check if there is a directive prefix: either [Directive] or a single non-space word starting with D
let text_part = if let Some(stripped) = trimmed_rem.strip_prefix('[') {
match stripped.find(']') {
Some(end_bracket) => stripped[end_bracket + 1..].trim_start(),
None => trimmed_rem,
}
} else if let Some(space_idx) = trimmed_rem.find(' ') {
let first_token = &trimmed_rem[..space_idx];
if first_token.starts_with('D') && first_token[1..].chars().all(|c| c.is_ascii_digit()) {
trimmed_rem[space_idx + 1..].trim_start()
} else {
trimmed_rem
}
} else {
trimmed_rem
};
// Text parsing: `\n` represents newline
let raw_lines: Vec<&str> = text_part.split(r"\n").map(str::trim).filter(|l| !l.is_empty()).collect();
let lines: Vec<String> = raw_lines.into_iter().map(escape_markup).collect();
let start = ms_to_tick(start_ms);
let end = ms_to_tick(end_ms);
if end <= start || lines.is_empty() {
doc.warnings.push(format!("line {}: empty or ends before it starts; skipped", n + 1));
continue;
}
doc.cues.push(Cue { start, end, text: lines.join("\n"), ..Default::default() });
}
if !found && !text.trim().is_empty() {
return Err(Error::NotFormat("JACOsub"));
}
Ok(doc)
}
pub fn write(doc: &Document) -> String {
let mut out = String::new();
out.push_str("# JACOsub file\n");
out.push_str("#TimeRes 100\n\n");
for c in &doc.cues {
let start_ms = tick_to_ms(c.start);
let end_ms = tick_to_ms(c.end).max(start_ms + 10);
let raw_lines = plain_text(&c.text);
let lines: Vec<&str> = raw_lines.lines().map(str::trim).filter(|l| !l.is_empty()).collect();
if !lines.is_empty() {
let text = lines.join(r"\n");
out.push_str(&format!("{} {} D0 {}\n", format_time(start_ms), format_time(end_ms), text));
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_and_write_roundtrip() {
let jss = "# JACOsub file\n#TimeRes 100\n\n0:00:01.00 0:00:03.00 D0 First line\\nsecond line\n0:00:04.50 0:00:06.00 D0 Second cue\n";
let doc = parse(jss).expect("parse");
assert_eq!(doc.cues.len(), 2);
assert_eq!(doc.cues[0].start, ms_to_tick(1000));
assert_eq!(doc.cues[0].end, ms_to_tick(3000));
assert_eq!(doc.cues[0].text, "First line\nsecond line");
assert_eq!(doc.cues[1].start, ms_to_tick(4500));
assert_eq!(doc.cues[1].end, ms_to_tick(6000));
assert_eq!(doc.cues[1].text, "Second cue");
let written = write(&doc);
assert_eq!(written, jss);
}
#[test]
fn parse_bracketed_directive() {
let jss = "#TimeRes 100\n0:00:01.00 0:00:03.00 [Style1] Bracketed style text\n";
let doc = parse(jss).expect("parse");
assert_eq!(doc.cues.len(), 1);
assert_eq!(doc.cues[0].text, "Bracketed style text");
}
#[test]
fn negative_shift_under_one_hour() {
let jss = "#TimeRes 100\n#Shift -0:00:01.00\n0:00:02.00 0:00:04.00 D0 Shifted line\n";
let doc = parse(jss).expect("parse");
assert_eq!(doc.cues.len(), 1);
assert_eq!(doc.cues[0].start, ms_to_tick(1000));
assert_eq!(doc.cues[0].end, ms_to_tick(3000));
}
#[test]
fn rejects_non_jacosub() {
assert!(parse("1\n00:00:01,000 --> 00:00:02,000\nHello\n").is_err());
}
}
+48 -2
View File
@@ -34,7 +34,10 @@ struct ReadmeExamples;
pub mod ass;
pub mod cea608;
pub mod jacosub;
pub mod mcc;
pub mod pjs;
pub mod realtext;
pub mod scc;
pub mod srt;
pub mod stl;
@@ -72,10 +75,28 @@ pub enum Format {
Dfxp,
/// Advanced SubStation Alpha (also reads SubStation Alpha).
Ass,
/// Phoenix Japanimation Society (PJS) subtitles.
Pjs,
/// RealText streaming timed text markup.
RealText,
/// JACOsub scriptable subtitles.
JacoSub,
}
impl Format {
pub const ALL: [Format; 8] = [Format::Srt, Format::WebVtt, Format::Scc, Format::Mcc, Format::Stl, Format::Ttml, Format::Dfxp, Format::Ass];
pub const ALL: [Format; 11] = [
Format::Srt,
Format::WebVtt,
Format::Scc,
Format::Mcc,
Format::Stl,
Format::Ttml,
Format::Dfxp,
Format::Ass,
Format::Pjs,
Format::RealText,
Format::JacoSub,
];
pub fn name(self) -> &'static str {
match self {
Format::Srt => "SubRip (SRT)",
@@ -86,6 +107,9 @@ impl Format {
Format::Ttml => "TTML (IMSC1)",
Format::Dfxp => "DFXP (TTML1)",
Format::Ass => "Advanced SubStation Alpha (ASS)",
Format::Pjs => "PJS",
Format::RealText => "RealText",
Format::JacoSub => "JACOsub",
}
}
pub fn extension(self) -> &'static str {
@@ -98,6 +122,9 @@ impl Format {
Format::Ttml => "ttml",
Format::Dfxp => "dfxp",
Format::Ass => "ass",
Format::Pjs => "pjs",
Format::RealText => "rt",
Format::JacoSub => "jss",
}
}
pub fn from_name(s: &str) -> Option<Format> {
@@ -110,6 +137,9 @@ impl Format {
"ttml" | "imsc" | "imsc1" | "xml" => Some(Format::Ttml),
"dfxp" => Some(Format::Dfxp),
"ass" | "ssa" => Some(Format::Ass),
"pjs" => Some(Format::Pjs),
"rt" | "realtext" => Some(Format::RealText),
"jss" | "js" | "jacosub" => Some(Format::JacoSub),
_ => None,
}
}
@@ -260,6 +290,16 @@ pub fn detect(bytes: &[u8], ext: Option<&str>) -> Option<Format> {
if head.starts_with("[Script Info]") {
return Some(Format::Ass);
}
if realtext::is_realtext(head) {
return Some(Format::RealText);
}
let first_lines: Vec<&str> = head.lines().map(str::trim).filter(|l| !l.is_empty()).take(3).collect();
if first_lines.first().is_some_and(|l| pjs::split_cue(l).is_some()) {
return Some(Format::Pjs);
}
if first_lines.iter().any(|l| jacosub::is_timed_line(l)) || (head.starts_with('#') && (head.contains("TimeRes") || head.contains("#T"))) {
return Some(Format::JacoSub);
}
let ext = ext.map(|e| e.trim_start_matches('.').to_ascii_lowercase());
if (head.starts_with('<') && (head.contains("<tt ") || head.contains("<tt>") || head.contains(":tt ")))
&& (head.contains("ns/ttml") || head.contains("ttaf1"))
@@ -271,7 +311,7 @@ pub fn detect(bytes: &[u8], ext: Option<&str>) -> Option<Format> {
match ext.as_deref() {
Some("srt") if has_arrow_timing || head.is_empty() => Some(Format::Srt),
Some("vtt") if has_arrow_timing => Some(Format::WebVtt),
Some("srt" | "vtt" | "scc" | "mcc" | "stl" | "ttml" | "dfxp" | "xml" | "ass" | "ssa") => None,
Some("srt" | "vtt" | "scc" | "mcc" | "stl" | "ttml" | "dfxp" | "xml" | "ass" | "ssa" | "pjs" | "rt" | "jss" | "js") => None,
_ if has_arrow_timing => Some(Format::Srt),
_ => None,
}
@@ -306,6 +346,9 @@ fn parse_unbounded(bytes: &[u8], format: Format) -> Result<Document> {
Format::Mcc => mcc::parse(&text),
Format::Ttml | Format::Dfxp => ttml::parse(&text),
Format::Ass => ass::parse(&text),
Format::Pjs => pjs::parse(&text),
Format::RealText => realtext::parse(&text),
Format::JacoSub => jacosub::parse(&text),
Format::Stl => stl::parse(bytes),
}
}
@@ -344,6 +387,9 @@ pub fn write(doc: &Document, format: Format, opts: WriteOptions) -> Vec<u8> {
Format::Ttml => ttml::write(doc, ttml::Flavor::Imsc1, opts.rate, doc.language.as_deref().unwrap_or("")).into_bytes(),
Format::Dfxp => ttml::write(doc, ttml::Flavor::Dfxp, opts.rate, doc.language.as_deref().unwrap_or("")).into_bytes(),
Format::Ass => ass::write(doc).into_bytes(),
Format::Pjs => pjs::write(doc).into_bytes(),
Format::RealText => realtext::write(doc).into_bytes(),
Format::JacoSub => jacosub::write(doc).into_bytes(),
}
}
+102
View File
@@ -0,0 +1,102 @@
//! Phoenix Japanimation Society (PJS) subtitles (`.pjs`): times in tenths of a second.
//!
//! ```text
//! 10,50,"First line"
//! 60,120,"Second line|with break"
//! ```
//!
//! Lines are comma-separated `start,end,"text"` with times in deciseconds (tenths of a second).
//! Inside the quotes, `|` breaks a line.
use crate::text::{escape_markup, ms_to_tick, number, plain_lines, tick_to_ms};
use crate::{Cue, Document, Error, Result};
/// `start,end,"text"` as its parts.
pub(crate) fn split_cue(line: &str) -> Option<(i64, i64, &str)> {
let (start_str, rest) = line.split_once(',')?;
let (end_str, text_str) = rest.split_once(',')?;
let start = number(start_str.trim())?;
let end = number(end_str.trim())?;
let text = text_str.trim();
let text = if let Some(stripped) = text.strip_prefix('"').and_then(|t| t.strip_suffix('"')) { stripped } else { text };
Some((start, end, text))
}
/// Parse a PJS subtitle document.
pub fn parse(text: &str) -> Result<Document> {
let mut doc = Document::default();
let mut found = false;
for (n, line) in text.lines().enumerate() {
let line = line.trim();
if line.is_empty() {
continue;
}
let Some((start_ds, end_ds, raw)) = split_cue(line) else {
doc.warnings.push(format!("line {}: not a PJS cue, skipped", n + 1));
continue;
};
found = true;
let mut lines = Vec::new();
for l in raw.split('|') {
let trimmed = l.trim();
if !trimmed.is_empty() {
let unquoted = trimmed.replace("\"\"", "\"");
lines.push(escape_markup(&unquoted));
}
}
let start = ms_to_tick(start_ds.saturating_mul(100));
let end = ms_to_tick(end_ds.saturating_mul(100));
if end <= start || lines.is_empty() {
doc.warnings.push(format!("line {}: empty or ends before it starts; skipped", n + 1));
continue;
}
doc.cues.push(Cue { start, end, text: lines.join("\n"), ..Default::default() });
}
if !found && !text.trim().is_empty() {
return Err(Error::NotFormat("PJS"));
}
Ok(doc)
}
/// Serialize a document into PJS format.
pub fn write(doc: &Document) -> String {
let mut out = String::new();
let ds = |t| (tick_to_ms(t) + 50) / 100;
for c in &doc.cues {
let start = ds(c.start);
let end = ds(c.end).max(start + 1);
let lines: Vec<String> =
plain_lines(&c.text).into_iter().map(|l| l.replace('"', "\"\"").replace('|', "|")).filter(|l| !l.is_empty()).collect();
if !lines.is_empty() {
out.push_str(&format!("{start},{end},\"{}\"\n", lines.join("|")));
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_and_write_roundtrip() {
let pjs = "10,50,\"First line\"\n60,120,\"Second line|with break\"\n130,150,\"Say \"\"Hi\"\"\"\n";
let doc = parse(pjs).expect("parse");
assert_eq!(doc.cues.len(), 3);
assert_eq!(doc.cues[0].start, ms_to_tick(1000));
assert_eq!(doc.cues[0].end, ms_to_tick(5000));
assert_eq!(doc.cues[0].text, "First line");
assert_eq!(doc.cues[1].start, ms_to_tick(6000));
assert_eq!(doc.cues[1].end, ms_to_tick(12000));
assert_eq!(doc.cues[1].text, "Second line\nwith break");
assert_eq!(doc.cues[2].text, "Say \"Hi\"");
let written = write(&doc);
assert_eq!(written, pjs);
}
#[test]
fn rejects_non_pjs() {
assert!(parse("1\n00:00:01,000 --> 00:00:02,000\nHello\n").is_err());
}
}
+247
View File
@@ -0,0 +1,247 @@
//! RealText (`.rt`): streaming HTML-like timed text markup.
//!
//! ```text
//! <window type="generic" width="320" height="60" wordwrap="true">
//! <time begin="00:01.00" end="00:03.00"/>First line<br/>second line
//! </window>
//! ```
//!
//! RealText documents encapsulate timed text inside a `<window>` container with `<time begin="…" [end="…"]/>`
//! markers. Inside each caption block, `<br/>` and `<br>` represent line breaks.
use crate::text::{escape_markup, last_cue_end, ms_to_tick, plain_lines, plain_text, tick_to_ms};
use crate::{Cue, Document, Error, Result};
/// Parse a RealText timestamp string into milliseconds.
/// Supports `SS[.fff]`, `MM:SS[.fff]`, and `HH:MM:SS[.fff]`.
pub(crate) fn parse_time(s: &str) -> Option<i64> {
let s = s.trim();
if s.is_empty() {
return None;
}
let parts: Vec<&str> = s.split(':').collect();
let (hours, mins, secs_str) = match parts.len() {
1 => (0i64, 0i64, parts[0]),
2 => (0i64, parts[0].parse().ok()?, parts[1]),
3 => (parts[0].parse().ok()?, parts[1].parse().ok()?, parts[2]),
_ => return None,
};
let (secs, ms) = if let Some((sec_p, frac_p)) = secs_str.split_once('.') {
let sec: i64 = sec_p.parse().ok()?;
let frac_str = frac_p.trim_end();
let ms = match frac_str.len() {
0 => 0i64,
1 => frac_str.parse::<i64>().ok()?.saturating_mul(100),
2 => frac_str.parse::<i64>().ok()?.saturating_mul(10),
3 => frac_str.parse::<i64>().ok()?,
_ => {
let first3 = frac_str.get(..3)?;
first3.parse::<i64>().ok()?
}
};
(sec, ms)
} else {
(secs_str.parse().ok()?, 0i64)
};
let total_ms = hours.saturating_mul(3600).saturating_add(mins.saturating_mul(60)).saturating_add(secs).saturating_mul(1000).saturating_add(ms);
Some(total_ms)
}
/// Format milliseconds into RealText timestamp string `MM:SS.ss` or `HH:MM:SS.ss`.
pub(crate) fn format_time(ms: i64) -> String {
let ms = ms.max(0);
let total_secs = ms / 1000;
let centis = (ms % 1000) / 10;
let hours = total_secs / 3600;
let mins = (total_secs % 3600) / 60;
let secs = total_secs % 60;
if hours > 0 { format!("{hours:02}:{mins:02}:{secs:02}.{centis:02}") } else { format!("{mins:02}:{secs:02}.{centis:02}") }
}
/// Extract attribute value from a tag (e.g., `begin="00:01.00"`).
fn attr_value<'a>(tag: &'a str, attr_name: &str) -> Option<&'a str> {
let mut rest = tag;
while let Some(idx) = rest.find(attr_name) {
let candidate = &rest[idx + attr_name.len()..];
let trimmed = candidate.trim_start();
if let Some(after_eq) = trimmed.strip_prefix('=') {
let after_eq = after_eq.trim_start();
let quote = after_eq.chars().next()?;
if quote == '"' || quote == '\'' {
let val_start = 1;
let val_end = after_eq[1..].find(quote)?;
return Some(&after_eq[val_start..1 + val_end]);
}
}
rest = &rest[idx + attr_name.len()..];
}
None
}
fn find_case_insensitive(haystack: &str, needle: &str) -> Option<usize> {
let needle_bytes = needle.as_bytes();
if needle_bytes.is_empty() {
return Some(0);
}
let haystack_bytes = haystack.as_bytes();
if haystack_bytes.len() < needle_bytes.len() {
return None;
}
haystack_bytes.windows(needle_bytes.len()).position(|window| window.iter().zip(needle_bytes).all(|(&h, &n)| h.eq_ignore_ascii_case(&n)))
}
/// Check if text is a RealText file.
pub(crate) fn is_realtext(text: &str) -> bool {
let trimmed = text.trim_start();
let lower_prefix: String = trimmed.chars().take(100).flat_map(|c| c.to_lowercase()).collect();
if lower_prefix.starts_with("<window") {
return true;
}
find_case_insensitive(text, "<window").is_some() && find_case_insensitive(text, "<time").is_some()
}
pub fn parse(text: &str) -> Result<Document> {
if !is_realtext(text) {
return Err(Error::NotFormat("RealText"));
}
let mut doc = Document::default();
struct RawCue<'a> {
begin: i64,
end: Option<i64>,
body: &'a str,
}
let mut raw_cues: Vec<RawCue> = Vec::new();
let mut search_pos = 0;
while let Some(time_idx) = find_case_insensitive(&text[search_pos..], "<time") {
let actual_idx = search_pos + time_idx;
let tag_end = match text[actual_idx..].find('>') {
Some(end) => actual_idx + end + 1,
None => break,
};
let tag_str = &text[actual_idx..tag_end];
let begin = attr_value(tag_str, "begin").and_then(parse_time);
let end = attr_value(tag_str, "end").and_then(parse_time);
if let Some(begin_ms) = begin {
// Text runs from tag_end to next '<time' or '</window>' or EOF
let remaining = &text[tag_end..];
let body_end =
find_case_insensitive(remaining, "<time").or_else(|| find_case_insensitive(remaining, "</window>")).unwrap_or(remaining.len());
let body = &text[tag_end..tag_end + body_end];
raw_cues.push(RawCue { begin: begin_ms, end, body });
search_pos = tag_end;
} else {
search_pos = tag_end;
}
}
for i in 0..raw_cues.len() {
let rc = &raw_cues[i];
let start = ms_to_tick(rc.begin);
let end = if let Some(explicit_end) = rc.end {
ms_to_tick(explicit_end)
} else if i + 1 < raw_cues.len() {
ms_to_tick(raw_cues[i + 1].begin)
} else {
last_cue_end(start)
};
// Convert <br/> and <br> to newlines, decode markup to clean text
let normalized = rc.body.replace("<br/>", "\n").replace("<br>", "\n").replace("<BR/>", "\n").replace("<BR>", "\n");
let plain = plain_text(&normalized);
let lines: Vec<&str> = plain.lines().map(str::trim).filter(|l| !l.is_empty()).collect();
if end <= start || lines.is_empty() {
continue;
}
let escaped_lines: Vec<String> = lines.into_iter().map(escape_markup).collect();
doc.cues.push(Cue { start, end, text: escaped_lines.join("\n"), ..Default::default() });
}
if doc.cues.is_empty() && !text.trim().is_empty() {
return Err(Error::NotFormat("RealText"));
}
Ok(doc)
}
pub fn write(doc: &Document) -> String {
let mut out = String::new();
out.push_str("<window type=\"generic\" width=\"320\" height=\"60\" wordwrap=\"true\">\n");
for c in &doc.cues {
let start_ms = tick_to_ms(c.start);
let mut end_ms = tick_to_ms(c.end);
if (end_ms / 10) <= (start_ms / 10) {
end_ms = start_ms.saturating_add(10);
}
let lines: Vec<String> = plain_lines(&c.text).into_iter().map(|l| escape_markup(&l)).filter(|l| !l.is_empty()).collect();
if !lines.is_empty() {
let body = lines.join("<br/>");
out.push_str(&format!("<time begin=\"{}\" end=\"{}\"/>{}\n", format_time(start_ms), format_time(end_ms), body));
}
}
out.push_str("</window>\n");
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_and_write_roundtrip() {
let rt = "<window type=\"generic\" width=\"320\" height=\"60\" wordwrap=\"true\">\n<time begin=\"00:01.00\" end=\"00:03.00\"/>Hello world\n<time begin=\"00:04.50\" end=\"00:06.25\"/>Second cue<br/>with newline\n</window>\n";
let doc = parse(rt).expect("parse");
assert_eq!(doc.cues.len(), 2);
assert_eq!(doc.cues[0].start, ms_to_tick(1000));
assert_eq!(doc.cues[0].end, ms_to_tick(3000));
assert_eq!(doc.cues[0].text, "Hello world");
assert_eq!(doc.cues[1].start, ms_to_tick(4500));
assert_eq!(doc.cues[1].end, ms_to_tick(6250));
assert_eq!(doc.cues[1].text, "Second cue\nwith newline");
let written = write(&doc);
let doc2 = parse(&written).expect("parse written");
assert_eq!(doc2.cues.len(), 2);
assert_eq!(doc2.cues[0].text, doc.cues[0].text);
assert_eq!(doc2.cues[1].text, doc.cues[1].text);
}
#[test]
fn parse_timestamps() {
assert_eq!(parse_time("5"), Some(5000));
assert_eq!(parse_time("5.5"), Some(5500));
assert_eq!(parse_time("00:02.50"), Some(2500));
assert_eq!(parse_time("01:02.500"), Some(62500));
assert_eq!(parse_time("01:02:03.40"), Some(3723400));
}
#[test]
fn does_not_misidentify_embedded_window_in_pjs() {
let pjs = "10,50,\"Use <window> here\"\n";
assert!(!is_realtext(pjs));
}
#[test]
fn short_cue_quantization_roundtrip() {
let mut doc = Document::default();
doc.cues.push(Cue { start: ms_to_tick(1000), end: ms_to_tick(1001), text: "Short".into(), ..Default::default() });
let written = write(&doc);
let parsed = parse(&written).expect("parse short cue document");
assert_eq!(parsed.cues.len(), 1);
assert_eq!(parsed.cues[0].text, "Short");
}
}
+46
View File
@@ -85,6 +85,52 @@ pub fn plain_text(s: &str) -> String {
out
}
use crate::TICKS_PER_MS;
use crate::time::{TICKS_PER_SECOND, Tick};
/// How long the last cue of a format that gives only start times lasts: such a file says when a
/// caption appears, and the next one ends it.
pub(crate) const LAST_CUE: Tick = Tick(4 * TICKS_PER_SECOND);
/// `start + LAST_CUE`, not overflowing.
pub(crate) fn last_cue_end(start: Tick) -> Tick {
Tick(start.0.saturating_add(LAST_CUE.0))
}
/// Milliseconds as a time.
pub(crate) fn ms_to_tick(ms: i64) -> Tick {
Tick(ms.saturating_mul(TICKS_PER_MS))
}
/// A time in whole milliseconds, rounded to the nearest; negative times read as zero.
pub(crate) fn tick_to_ms(t: Tick) -> i64 {
let per = i128::from(TICKS_PER_MS);
((i128::from(t.0.max(0)) + per / 2) / per) as i64
}
/// A decimal number of at most nine digits (so that times built from it cannot overflow).
pub(crate) fn number(s: &str) -> Option<i64> {
if s.is_empty() || s.len() > 9 || !s.bytes().all(|b| b.is_ascii_digit()) { None } else { s.parse().ok() }
}
/// Plain text as cue text: a literal `&` or `<` becomes an entity so that it is not read as markup.
pub(crate) fn escape_markup(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for c in s.chars() {
match c {
'&' => out.push_str("&amp;"),
'<' => out.push_str("&lt;"),
_ => out.push(c),
}
}
out
}
/// The cue's text as plain lines (tags gone, entities decoded, empty lines dropped).
pub(crate) fn plain_lines(text: &str) -> Vec<String> {
text.lines().map(plain_text).map(|l| l.trim().to_string()).filter(|l| !l.is_empty()).collect()
}
#[cfg(test)]
mod tests {
use super::*;
+1
View File
@@ -2,6 +2,7 @@ mod ass;
mod limits;
mod literal_roundtrips;
mod mcc_708_timing;
mod pjs_realtext_jacosub;
mod roundtrip;
mod ttml_body_styles;
mod ttml_body_timing;
@@ -0,0 +1,174 @@
//! Integration and FFmpeg oracle tests for PJS, RealText, and JACOsub legacy caption formats.
use craft_captions::{Cue, Document, Format, TICKS_PER_SECOND, Tick, WriteOptions, detect, parse, write};
fn ms(millis: i64) -> Tick {
Tick(millis.saturating_mul(TICKS_PER_SECOND / 1000))
}
#[test]
fn test_detect_legacy_formats() {
let pjs = b"10,50,\"Hello world\"\n60,120,\"Line one|Line two\"\n";
assert_eq!(detect(pjs, Some("pjs")), Some(Format::Pjs));
assert_eq!(detect(pjs, None), Some(Format::Pjs));
let rt = b"<window type=\"generic\" width=\"320\" height=\"60\">\n<time begin=\"00:01.00\" end=\"00:03.00\"/>Hello\n</window>\n";
assert_eq!(detect(rt, Some("rt")), Some(Format::RealText));
assert_eq!(detect(rt, None), Some(Format::RealText));
let jss = b"# JACOsub file\n#TimeRes 100\n0:00:01.00 0:00:03.00 D0 Hello\n";
assert_eq!(detect(jss, Some("jss")), Some(Format::JacoSub));
assert_eq!(detect(jss, Some("js")), Some(Format::JacoSub));
assert_eq!(detect(jss, None), Some(Format::JacoSub));
}
#[test]
fn test_pjs_roundtrip_and_edge_cases() {
let doc = Document {
cues: vec![
Cue { start: ms(1000), end: ms(4500), text: "First caption".into(), ..Default::default() },
Cue { start: ms(6000), end: ms(10200), text: "Line 1\nLine 2".into(), ..Default::default() },
],
..Default::default()
};
let bytes = write(&doc, Format::Pjs, WriteOptions::default());
let parsed = parse(&bytes, Format::Pjs).expect("parse pjs");
assert_eq!(parsed.cues.len(), 2);
assert_eq!(parsed.cues[0].start, ms(1000));
assert_eq!(parsed.cues[0].end, ms(4500));
assert_eq!(parsed.cues[0].text, "First caption");
assert_eq!(parsed.cues[1].start, ms(6000));
assert_eq!(parsed.cues[1].end, ms(10200));
assert_eq!(parsed.cues[1].text, "Line 1\nLine 2");
}
#[test]
fn test_realtext_roundtrip_and_edge_cases() {
let doc = Document {
cues: vec![
Cue { start: ms(2000), end: ms(5500), text: "Welcome to RealText".into(), ..Default::default() },
Cue { start: ms(7000), end: ms(12000), text: "Multi-line\nRealText caption".into(), ..Default::default() },
],
..Default::default()
};
let bytes = write(&doc, Format::RealText, WriteOptions::default());
let parsed = parse(&bytes, Format::RealText).expect("parse realtext");
assert_eq!(parsed.cues.len(), 2);
assert_eq!(parsed.cues[0].start, ms(2000));
assert_eq!(parsed.cues[0].end, ms(5500));
assert_eq!(parsed.cues[0].text, "Welcome to RealText");
assert_eq!(parsed.cues[1].start, ms(7000));
assert_eq!(parsed.cues[1].end, ms(12000));
assert_eq!(parsed.cues[1].text, "Multi-line\nRealText caption");
}
#[test]
fn test_jacosub_roundtrip_and_edge_cases() {
let doc = Document {
cues: vec![
Cue { start: ms(1000), end: ms(4000), text: "First JACOsub cue".into(), ..Default::default() },
Cue { start: ms(5500), end: ms(9200), text: "Line A\nLine B".into(), ..Default::default() },
],
..Default::default()
};
let bytes = write(&doc, Format::JacoSub, WriteOptions::default());
let parsed = parse(&bytes, Format::JacoSub).expect("parse jacosub");
assert_eq!(parsed.cues.len(), 2);
assert_eq!(parsed.cues[0].start, ms(1000));
assert_eq!(parsed.cues[0].end, ms(4000));
assert_eq!(parsed.cues[0].text, "First JACOsub cue");
assert_eq!(parsed.cues[1].start, ms(5500));
assert_eq!(parsed.cues[1].end, ms(9200));
assert_eq!(parsed.cues[1].text, "Line A\nLine B");
}
#[test]
fn test_hostile_inputs_never_panic() {
for format in [Format::Pjs, Format::RealText, Format::JacoSub] {
let _ = parse(b"", format);
let _ = parse(b" \n\r\n\t ", format);
let _ = parse(b"invalid text without any headers or timestamps\nfoo bar baz", format);
let _ = parse(b"9999999999999999999999999999999999,99999999999999999999,\"overflow\"", format);
let _ = parse(b"<window><time begin=\"invalid\" end=\"NaN\"/>unclosed", format);
let _ = parse(b"#TimeRes 0\n@9999999999999999999999 @0 D0 test", format);
for len in [1, 5, 20, 100, 500] {
let garbage = vec![0xFE; len];
let _ = parse(&garbage, format);
}
}
}
#[test]
fn test_ffmpeg_oracle_pjs_realtext_jacosub() {
let Some(ff) = craft_media_testkit::ffmpeg_or_skip("legacy text subtitles oracle") else { return };
let temp_dir = std::env::temp_dir().join("craft_captions_legacy_test");
let _ = std::fs::create_dir_all(&temp_dir);
// 1. PJS Oracle: write PJS -> decode with FFmpeg -> compare cues
let pjs_doc = Document {
cues: vec![
Cue { start: ms(1000), end: ms(4000), text: "PJS oracle cue 1".into(), ..Default::default() },
Cue { start: ms(5000), end: ms(9000), text: "PJS cue 2 line 1\nPJS cue 2 line 2".into(), ..Default::default() },
],
..Default::default()
};
let pjs_path = temp_dir.join("test.pjs");
std::fs::write(&pjs_path, write(&pjs_doc, Format::Pjs, WriteOptions::default())).expect("write pjs");
let ff_pjs_srt =
std::process::Command::new(&ff).args(["-y", "-i", pjs_path.to_str().expect("path"), "-f", "srt", "-"]).output().expect("ffmpeg pjs").stdout;
let ff_pjs_doc = parse(&ff_pjs_srt, Format::Srt).expect("parse ff pjs srt");
assert_eq!(ff_pjs_doc.cues.len(), pjs_doc.cues.len());
for i in 0..pjs_doc.cues.len() {
assert_eq!(ff_pjs_doc.cues[i].start, pjs_doc.cues[i].start);
assert_eq!(ff_pjs_doc.cues[i].end, pjs_doc.cues[i].end);
assert_eq!(ff_pjs_doc.cues[i].text.trim(), pjs_doc.cues[i].text.trim());
}
// 2. RealText Oracle: write RealText -> decode with FFmpeg -> compare cues
let rt_doc = Document {
cues: vec![
Cue { start: ms(1000), end: ms(3000), text: "RealText oracle cue 1".into(), ..Default::default() },
Cue { start: ms(4500), end: ms(8000), text: "RealText cue 2 line 1\nRealText cue 2 line 2".into(), ..Default::default() },
],
..Default::default()
};
let rt_path = temp_dir.join("test.rt");
std::fs::write(&rt_path, write(&rt_doc, Format::RealText, WriteOptions::default())).expect("write rt");
let ff_rt_srt =
std::process::Command::new(&ff).args(["-y", "-i", rt_path.to_str().expect("path"), "-f", "srt", "-"]).output().expect("ffmpeg rt").stdout;
let ff_rt_doc = parse(&ff_rt_srt, Format::Srt).expect("parse ff rt srt");
assert_eq!(ff_rt_doc.cues.len(), rt_doc.cues.len());
for i in 0..rt_doc.cues.len() {
assert_eq!(ff_rt_doc.cues[i].start, rt_doc.cues[i].start);
assert_eq!(ff_rt_doc.cues[i].end, rt_doc.cues[i].end);
assert_eq!(ff_rt_doc.cues[i].text.trim(), rt_doc.cues[i].text.trim());
}
// 3. JACOsub Oracle: write JACOsub -> decode with FFmpeg -> compare cues
let jss_doc = Document {
cues: vec![
Cue { start: ms(1000), end: ms(4000), text: "JACOsub oracle cue 1".into(), ..Default::default() },
Cue { start: ms(5000), end: ms(9000), text: "JACOsub cue 2 line 1\nJACOsub cue 2 line 2".into(), ..Default::default() },
],
..Default::default()
};
let jss_path = temp_dir.join("test.jss");
std::fs::write(&jss_path, write(&jss_doc, Format::JacoSub, WriteOptions::default())).expect("write jss");
let ff_jss_srt =
std::process::Command::new(&ff).args(["-y", "-i", jss_path.to_str().expect("path"), "-f", "srt", "-"]).output().expect("ffmpeg jss").stdout;
let ff_jss_doc = parse(&ff_jss_srt, Format::Srt).expect("parse ff jss srt");
assert_eq!(ff_jss_doc.cues.len(), jss_doc.cues.len());
for i in 0..jss_doc.cues.len() {
assert_eq!(ff_jss_doc.cues[i].start, jss_doc.cues[i].start);
assert_eq!(ff_jss_doc.cues[i].end, jss_doc.cues[i].end);
assert_eq!(ff_jss_doc.cues[i].text.trim(), jss_doc.cues[i].text.trim());
}
}