mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
fix(captions): preserve literal ASS escapes and normalize newlines
This commit is contained in:
1 parent
7b4d5f992c
commit
d3024d0cd5
2 files changed
+38
-4
No files matched your search
@@ -191,7 +191,10 @@ fn escape(text: &str) -> String {
|
||||
let mut out = String::with_capacity(text.len() + 8);
|
||||
let mut rest = text;
|
||||
while !rest.is_empty() {
|
||||
if let Some(r) = rest.strip_prefix("<i>") {
|
||||
if let Some(r) = rest.strip_prefix("\r\n") {
|
||||
out.push_str("\\N");
|
||||
rest = r;
|
||||
} else if let Some(r) = rest.strip_prefix("<i>") {
|
||||
out.push_str("{\\i1}");
|
||||
rest = r;
|
||||
} else if let Some(r) = rest.strip_prefix("</i>") {
|
||||
@@ -211,7 +214,9 @@ fn escape(text: &str) -> String {
|
||||
rest = r;
|
||||
} else if let Some(c) = rest.chars().next() {
|
||||
match c {
|
||||
'\n' => out.push_str("\\N"),
|
||||
'\n' | '\r' => out.push_str("\\N"),
|
||||
// Empty override block separates a literal slash from N/n/h.
|
||||
'\\' => out.push_str("\\{}"),
|
||||
'{' => out.push('\u{ff5b}'),
|
||||
'}' => out.push('\u{ff5d}'),
|
||||
'\u{a0}' => out.push_str("\\h"),
|
||||
@@ -232,7 +237,7 @@ pub fn write(doc: &Document) -> String {
|
||||
);
|
||||
for c in &doc.cues {
|
||||
// A comma in the Name field would shift the columns; the other fields are fixed text.
|
||||
let name = c.speaker.as_deref().unwrap_or("").replace([',', '\n'], " ");
|
||||
let name = c.speaker.as_deref().unwrap_or("").replace([',', '\n', '\r'], " ");
|
||||
out.push_str(&format!("Dialogue: 0,{},{},Default,{},0,0,0,,{}\n", format_time(c.start), format_time(c.end), name, escape(&c.text)));
|
||||
}
|
||||
out
|
||||
|
||||
@@ -99,7 +99,8 @@ fn we_read_what_ffmpeg_writes() {
|
||||
fn ffmpeg_reads_what_we_write() {
|
||||
let Some(ff) = craft_media_testkit::ffmpeg() else { return };
|
||||
let dir = craft_media_testkit::fixture_subdir("captions_ass");
|
||||
let doc = parse(SRT.as_bytes(), Format::Srt).expect("parse srt");
|
||||
let mut doc = parse(SRT.as_bytes(), Format::Srt).expect("parse srt");
|
||||
doc.cues.push(Cue { start: cs(400_000), end: cs(400_100), text: r"C:\New\notes\hello and \N \n \h".into(), ..Default::default() });
|
||||
let (ass, srt) = (dir.join("ours.ass"), dir.join("back.srt"));
|
||||
std::fs::write(&ass, write(&doc, Format::Ass, WriteOptions::default())).expect("write");
|
||||
ffmpeg_run(&ff, &["-i", ass.to_str().expect("path"), srt.to_str().expect("path")]);
|
||||
@@ -111,3 +112,31 @@ fn ffmpeg_reads_what_we_write() {
|
||||
assert_eq!(t.text, d.text, "text of the cue at {:?}", d.start);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn literal_backslashes_survive_ass_round_trip() {
|
||||
let text = r"C:\New\notes\hello and \N \n \h";
|
||||
let doc = Document { cues: vec![Cue { start: cs(100), end: cs(200), text: text.into(), ..Default::default() }], ..Default::default() };
|
||||
let bytes = write(&doc, Format::Ass, WriteOptions::default());
|
||||
assert_eq!(parse(&bytes, Format::Ass).unwrap().cues[0].text, text);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn carriage_returns_cannot_create_dialogue_records() {
|
||||
let doc = Document {
|
||||
cues: vec![Cue {
|
||||
start: cs(100),
|
||||
end: cs(200),
|
||||
text: "first\rsecond\r\nthird".into(),
|
||||
speaker: Some("Ann\rOther".into()),
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
};
|
||||
let bytes = write(&doc, Format::Ass, WriteOptions::default());
|
||||
assert!(!bytes.contains(&b'\r'));
|
||||
let back = parse(&bytes, Format::Ass).unwrap();
|
||||
assert_eq!(back.cues.len(), 1);
|
||||
assert_eq!(back.cues[0].text, "first\nsecond\nthird");
|
||||
assert_eq!(back.cues[0].speaker.as_deref(), Some("Ann Other"));
|
||||
}
|
||||
Reference in new issue
Block a user