Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

fix(export): a frame rate no video has is an error, not a panic

marcinx98xe9f09dffd4ad
diff --git a/src/crates/concat-core/src/time.rs b/src/crates/concat-core/src/time.rs
index 068aab2a..df3d9639 100644
--- a/src/crates/concat-core/src/time.rs
+++ b/src/crates/concat-core/src/time.rs
@@ -337,6 +337,21 @@ impl FrameRate {
Self(fps)
}
+ /// The largest term [`FrameRate::checked`] takes. Every rate a camera or
+ /// a standard uses is a few thousand over a thousand and one at most;
+ /// terms past this only come from a hand-edited document, and frame
+ /// arithmetic on them overflows.
+ pub const MAX_TERM: i64 = 1_000_000;
+
+ /// The rate `num / den`, or `None` for one no video has: a zero or
+ /// negative term, or one past [`FrameRate::MAX_TERM`]. The
+ /// non-panicking path for a rate that comes from a document or a
+ /// request rather than from a constant.
+ pub fn checked(num: i64, den: i64) -> Option<Self> {
+ let term = 1..=Self::MAX_TERM;
+ (term.contains(&num) && term.contains(&den)).then(|| Self(Rational::new(num, den)))
+ }
+
/// Builds a whole-number frame rate.
pub fn from_int(fps: u32) -> Self {
Self::new(Rational::from_int(i64::from(fps)))
@@ -419,6 +434,17 @@ impl TimeRange {
mod tests {
use super::*;
+ #[test]
+ fn a_checked_rate_refuses_what_no_video_has() {
+ assert_eq!(FrameRate::checked(30000, 1001), Some(FrameRate::NTSC_30));
+ assert_eq!(FrameRate::checked(25, 1), Some(FrameRate::PAL));
+ assert_eq!(FrameRate::checked(0, 1), None);
+ assert_eq!(FrameRate::checked(30, 0), None);
+ assert_eq!(FrameRate::checked(-30, 1), None);
+ assert_eq!(FrameRate::checked(i64::MAX, 1), None);
+ assert_eq!(FrameRate::checked(30, FrameRate::MAX_TERM + 1), None);
+ }
+
#[test]
fn reduces_to_lowest_terms() {
let r = Rational::new(6, 8);
diff --git a/src/crates/concat-export/src/lib.rs b/src/crates/concat-export/src/lib.rs
index b5bdd8bf..03ecf01e 100644
--- a/src/crates/concat-export/src/lib.rs
+++ b/src/crates/concat-export/src/lib.rs
@@ -887,7 +887,12 @@ pub fn render_on(
return Err("there is nothing on the timeline to export".to_owned());
}
- let rate = FrameRate::new(Rational::new(request.rate_num, request.rate_den));
+ let rate = FrameRate::checked(request.rate_num, request.rate_den).ok_or_else(|| {
+ format!(
+ "{}/{} is not a frame rate a video can have",
+ request.rate_num, request.rate_den
+ )
+ })?;
let output = PathBuf::from(&request.output);
// Transitions become overlaps, ramps and fade filters before anything
@@ -1902,7 +1907,9 @@ pub fn preview_plan(
rate_den: i64,
color_space: ColorSpace,
) -> PreviewPlan {
- let rate = FrameRate::new(Rational::new(rate_num, rate_den));
+ // A preview has no error to give, and a picture at the wrong pace beats
+ // none; the document opens with a sane rate (VideoSettings::or) anyway.
+ let rate = FrameRate::checked(rate_num, rate_den).unwrap_or(FrameRate::THIRTY);
let mut resolved = clips.to_vec();
let transitions = resolve_transitions(&mut resolved, rate);
let visible: Vec<&ExportClip> = resolved
diff --git a/src/crates/concat-project/src/lib.rs b/src/crates/concat-project/src/lib.rs
index 4295268b..f0b230d7 100644
--- a/src/crates/concat-project/src/lib.rs
+++ b/src/crates/concat-project/src/lib.rs
@@ -187,6 +187,28 @@ mod tests {
);
}
+ /// A hand-edited rate whose terms would overflow the frame arithmetic
+ /// opens at the fallback rather than panicking an export later.
+ #[test]
+ fn a_rate_no_video_has_opens_at_the_fallback_and_is_refused() {
+ use crate::model::VideoSettings;
+ let edited = VideoSettings {
+ rate_num: i64::MAX,
+ rate_den: 7,
+ ..VideoSettings::default()
+ };
+ assert!(!edited.is_sane());
+ let opened = edited.or(VideoSettings::default());
+ assert_eq!((opened.rate_num, opened.rate_den), (30, 1));
+ assert!(opened.is_sane());
+ let ntsc = VideoSettings {
+ rate_num: 30000,
+ rate_den: 1001,
+ ..VideoSettings::default()
+ };
+ assert_eq!(ntsc.or(VideoSettings::default()), ntsc);
+ }
+
/// A timeline's colour is left out of the document for SDR, kept
/// through a save and an open for HDR, and read as SDR from a document
/// naming one this build does not know; a file's likewise.
diff --git a/src/crates/concat-project/src/model.rs b/src/crates/concat-project/src/model.rs
index 8351ba92..884b649d 100644
--- a/src/crates/concat-project/src/model.rs
+++ b/src/crates/concat-project/src/model.rs
@@ -1903,9 +1903,24 @@ impl VideoSettings {
}
/// This frame with every zero or negative field taken from `fallback`
- /// instead: a document hand-edited into nonsense still opens at a size
- /// that is a size.
+ /// instead, and the rate as a pair when it is not one a video has (see
+ /// [`FrameRate::checked`](concat_core::time::FrameRate::checked)): a
+ /// document hand-edited into nonsense still opens at a size that is a
+ /// size, and at a rate the frame arithmetic can count in.
pub fn or(self, fallback: VideoSettings) -> VideoSettings {
+ let settings = self.or_terms(fallback);
+ if concat_core::time::FrameRate::checked(settings.rate_num, settings.rate_den).is_some() {
+ settings
+ } else {
+ VideoSettings {
+ rate_num: fallback.rate_num,
+ rate_den: fallback.rate_den,
+ ..settings
+ }
+ }
+ }
+
+ fn or_terms(self, fallback: VideoSettings) -> VideoSettings {
VideoSettings {
width: if self.width > 0 {
self.width
@@ -1933,9 +1948,12 @@ impl VideoSettings {
/// Whether every term is one a frame could actually have. A zero
/// dimension or rate is never a real setting, only a caller bug, and
- /// writing one would poison the document until the next open.
+ /// writing one would poison the document until the next open. Neither
+ /// is a rate whose terms overflow the frame arithmetic.
pub fn is_sane(self) -> bool {
- self.width > 0 && self.height > 0 && self.rate_num > 0 && self.rate_den > 0
+ self.width > 0
+ && self.height > 0
+ && concat_core::time::FrameRate::checked(self.rate_num, self.rate_den).is_some()
}
}