Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

Fix TTML/DFXP export overflow for cue times near i64::MAX

NetterSilvera073371bc67f
diff --git a/crates/captions/src/ttml.rs b/crates/captions/src/ttml.rs
index bf6b2f65b..8636c4340 100644
--- a/crates/captions/src/ttml.rs
+++ b/crates/captions/src/ttml.rs
@@ -18,7 +18,7 @@ use std::collections::{HashMap, HashSet};
use filmcraft_time::{FrameRate, TICKS_PER_SECOND, Tick};
-use crate::{Cue, Document, Error, Result, TICKS_PER_MS};
+use crate::{Cue, Document, Error, Result};
/// Which TTML flavour to write.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -55,8 +55,7 @@ fn esc(s: &str) -> String {
}
fn clock(t: Tick) -> String {
- let ms = (t.0.max(0) + TICKS_PER_MS / 2) / TICKS_PER_MS;
- format!("{:02}:{:02}:{:02}.{:03}", ms / 3_600_000, (ms / 60_000) % 60, (ms / 1000) % 60, ms % 1000)
+ crate::format_clock(t, '.')
}
/// Time expression for `t`: frames at `rate` when it is on a frame boundary, else clock time.
diff --git a/crates/captions/tests/ttml_clock_overflow.rs b/crates/captions/tests/ttml_clock_overflow.rs
new file mode 100644
index 000000000..23a3870ad
--- /dev/null
+++ b/crates/captions/tests/ttml_clock_overflow.rs
@@ -0,0 +1,18 @@
+//! TTML / DFXP export must not overflow for cue times near `i64::MAX` (e.g. from an MCC with a
+//! huge timecode).
+
+use filmcraft_captions::{Cue, Document, Format, WriteOptions, write};
+use filmcraft_time::Tick;
+
+fn doc(end: Tick) -> Document {
+ Document { cues: vec![Cue { start: Tick(0), end, text: "x".into(), ..Cue::default() }], ..Document::default() }
+}
+
+#[test]
+fn ttml_and_dfxp_write_survive_tick_max() {
+ for format in [Format::Ttml, Format::Dfxp] {
+ let out = write(&doc(Tick(i64::MAX)), format, WriteOptions::default());
+ let s = String::from_utf8(out).unwrap();
+ assert!(s.contains("end=\""), "{format:?}: {s}");
+ }
+}