The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.
Rust ffmpegfree-video-editormodel-context-protocolnon-linear-editoroffline-firstopen-source-video-editorself-hostedvideo-editingvideo-editing-automationvideo-editing-softwarevideo-editing-toolsvideo-editorvoice-cloningyoutube-automationyoutube-shorts
fix(media): a variable-rate average too heavy to keep exactly is rounded, not dropped for the timestamp grid
0xvoidworm4f69340b8286
diff --git a/src/crates/concat-core/src/time.rs b/src/crates/concat-core/src/time.rsindex 1eef8fe0..62c838e0 100644--- a/src/crates/concat-core/src/time.rs+++ b/src/crates/concat-core/src/time.rs@@ -358,6 +358,34 @@ impl FrameRate {.then_some(Self(rate))}+ /// The closest rate to `fps` a video can have, at the finest thousandth+ /// of a frame per second that fits [`FrameRate::MAX_TERM`] - coarser+ /// only where the numerator of a finer one would not.+ ///+ /// A container's `avg_frame_rate` is a statistic over the whole file: a+ /// 138-second variable-rate recording comes out at+ /// `764460000/12487247`, whose terms are far past what the frame+ /// arithmetic counts in, while the rate the container holds beside it is+ /// its timestamp grid, `90000/1`, which is not a rate at all. Rounded,+ /// the average still says the pace the file was recorded at. `None` for+ /// one no video has: zero, negative, not finite, or a rate whose+ /// thousandths, hundredths, tenths and whole frames are all past+ /// [`FrameRate::MAX_TERM`].+ pub fn approximated(fps: f64) -> Option<Self> {+ if !fps.is_finite() || fps <= 0.0 {+ return None;+ }+ for factor in [1_000_i64, 100, 10, 1] {+ // A rate past an i64 saturates rather than wraps, and a+ // saturated one is past MAX_TERM, where `checked` refuses it.+ let scaled = (fps * factor as f64).round() as i64;+ if let Some(rate) = Self::checked(scaled, factor) {+ return Some(rate);+ }+ }+ None+ }+/// Builds a whole-number frame rate.pub fn from_int(fps: u32) -> Self {Self::new(Rational::from_int(i64::from(fps)))@@ -456,6 +484,28 @@ mod tests {);}+ /// A rate written as a float - a container's `avg_frame_rate` for a+ /// variable-rate file - comes back as the closest rate the frame+ /// arithmetic takes, and nothing at all for one no video has.+ #[test]+ fn an_approximated_rate_is_the_closest_one_that_fits() {+ assert_eq!(+ FrameRate::approximated(61.219_258),+ Some(FrameRate::new(Rational::new(61_219, 1_000))),+ "a statistic keeps its thousandths"+ );+ assert_eq!(FrameRate::approximated(25.0), Some(FrameRate::PAL));+ assert_eq!(FrameRate::approximated(0.0), None);+ assert_eq!(FrameRate::approximated(-25.0), None);+ assert_eq!(FrameRate::approximated(f64::NAN), None);+ assert_eq!(FrameRate::approximated(f64::INFINITY), None);+ assert_eq!(+ FrameRate::approximated(1_500_000.0),+ None,+ "past whole frames there is no rate left to take"+ );+ }+#[test]fn reduces_to_lowest_terms() {let r = Rational::new(6, 8);diff --git a/src/crates/concat-media/src/probe.rs b/src/crates/concat-media/src/probe.rsindex ba22e256..471ea918 100644--- a/src/crates/concat-media/src/probe.rs+++ b/src/crates/concat-media/src/probe.rs@@ -231,7 +231,19 @@ fn pick_rate(average: Option<Rational>, guess: Option<Rational>) -> Option<Ratiolet usable = |rate: &Option<Rational>| {rate.filter(|rate| FrameRate::checked(rate.numerator(), rate.denominator()).is_some())};- match (usable(&average), usable(&guess)) {+ // An average the frame arithmetic cannot count in is still the only+ // statement a variable stream makes about its pace. Rounded to the+ // closest rate it can count in, it is kept: one screen recording's+ // average of 764460000/12487247 over 138 seconds came out as+ // 61219/1000, where taking the base rate instead - its timestamp grid,+ // 90000/1 - wrote all 8494 frames into a proxy 0.094 seconds long, and+ // Half and Quarter previews held one frame (#394).+ let average = usable(&average).or_else(|| {+ average+ .and_then(|rate| FrameRate::approximated(rate.as_f64()))+ .map(|rate| rate.fps())+ });+ match (average, usable(&guess)) {(Some(average), Some(base)) => {let (a, b) = (average.as_f64(), base.as_f64());let close = ((a - b) / b).abs() <= 0.02;@@ -297,6 +309,24 @@ mod tests {assert_eq!(pick_rate(negative, thirty), thirty);}+ /// A variable-rate recording's average is a fraction whose terms are+ /// past what a document's rates carry, and the rate beside it in the+ /// container is the timestamp grid. Keeping the grid gave a 138-second+ /// screen recording a 90000-fps proxy - 8494 frames in 0.094 seconds -+ /// and a Half or Quarter preview that held a single frame (#394). The+ /// average is kept, rounded to the closest rate that fits.+ #[test]+ fn a_heavy_average_keeps_its_pace_rather_than_the_timestamp_grid() {+ let average = Some(Rational::new(764_460_000, 12_487_247));+ let grid = Some(Rational::from_int(90_000));+ let picked = pick_rate(average, grid).expect("a rate");+ assert!(+ (picked.as_f64() - 61.219_258).abs() < 0.001,+ "{picked:?} is not the pace the recording was made at"+ );+ assert_ne!(picked, Rational::from_int(90_000));+ }+#[test]fn a_missing_file_is_an_error_not_a_panic() {assert!(probe("does-not-exist.mp4").is_err());