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 constant-rate stream is indexed at its base rate, and the end is remembered
Jareer381c47767ad8
diff --git a/src/crates/concat-media/src/pool.rs b/src/crates/concat-media/src/pool.rsindex 9109a96b..3f0e9dc7 100644--- a/src/crates/concat-media/src/pool.rs+++ b/src/crates/concat-media/src/pool.rs@@ -849,15 +849,26 @@ impl ReaderPool {// Stops at end of stream too: a clip trimmed past its media's end,// where the last real frame is the honest answer.let mut latest: Option<Arc<Frame>> = None;+ let mut reached = i64::MIN;while let Some((index, frame)) = reader.next_frame(rate)? {Self::count(&self.counters.decoded);let frame = Arc::new(frame);remember(index, Arc::clone(&frame));latest = Some(frame);+ reached = index;if index >= target {break;}}+ // The stream ended short of the target: the last frame is the+ // answer, and it is remembered under the index asked for, so a+ // container that claims a frame more than it holds does not cost a+ // seek and a walk every time the end is asked for.+ if reached < target+ && let Some(frame) = &latest+ {+ remember(target, Arc::clone(frame));+ }// Nothing at all means the seek itself landed past the end of the// picture stream - a container whose video ends before its audio, or@@ -1321,6 +1332,24 @@ pub(crate) mod tests {let _ = std::fs::remove_file(&path);}+ /// A container that claims one frame more than it holds: the request+ /// for that frame gets the last real one, and asking again costs a+ /// lookup, not another seek and walk from the keyframe.+ #[test]+ fn a_request_past_the_last_frame_is_remembered_under_its_index() {+ let path = counting_video("past-end", 32, 30);+ let pool = ReaderPool::new(64 * 1024 * 1024, 1);+ let rate = FrameRate::THIRTY;+ // Straight to the end and past it, twice over.+ let past = FrameRequest::new(&path, rate.time_of_frame(200), 32, 32);+ let first = pool.frame(&past).expect("the last frame stands in");+ let before = pool.stats();+ let again = pool.frame(&past).expect("still");+ assert!(Arc::ptr_eq(&first, &again), "the same remembered frame");+ assert_eq!(pool.stats().since(before).decoded, 0, "no second walk");+ let _ = std::fs::remove_file(&path);+ }+/// A proxy that has gone from disk is forgotten on the first request/// that asks for it, and the original serves the frame.#[test]diff --git a/src/crates/concat-media/src/probe.rs b/src/crates/concat-media/src/probe.rsindex 6c44423c..ba4744db 100644--- a/src/crates/concat-media/src/probe.rs+++ b/src/crates/concat-media/src/probe.rs@@ -182,13 +182,30 @@ fn rational(value: ffmpeg::Rational) -> Option<Rational> {.then(|| Rational::new(i64::from(value.numerator()), i64::from(value.denominator())))}-/// `avg_frame_rate` is 0/0 for streams with no constant rate, in which case-/// `r_frame_rate` carries the best guess FFmpeg has.+/// The rate a stream's frames are indexed by.+///+/// `avg_frame_rate` is the frame count over the duration, and on a short+/// or oddly-closed file it is off by a fraction - ninety frames over+/// eighty-nine thirtieths is 30.34 - which puts every frame on a boundary+/// a third of a frame early and leaves holes where an index is skipped.+/// `r_frame_rate` is the rate the timestamps are on. So a constant-rate+/// stream, where the two agree within two percent, takes the base rate+/// exactly; a stream with no constant rate takes the average, since its+/// base rate is whatever the timestamps' common denominator happens to+/// be and can be absurd. `avg_frame_rate` is 0/0 for such a stream with+/// no average either, in which case the base rate is the best guess left.fn pick_rate(average: Option<Rational>, guess: Option<Rational>) -> Option<Rational> {- [average, guess]- .into_iter()- .flatten()- .find(|rate| !rate.is_zero() && !rate.is_negative())+ let usable =+ |rate: &Option<Rational>| rate.filter(|rate| !rate.is_zero() && !rate.is_negative());+ match (usable(&average), usable(&guess)) {+ (Some(average), Some(base)) => {+ let (a, b) = (average.as_f64(), base.as_f64());+ let close = ((a - b) / b).abs() <= 0.02;+ Some(if close { base } else { average })+ }+ (Some(average), None) => Some(average),+ (None, base) => base,+ }}#[cfg(test)]@@ -207,6 +224,24 @@ mod tests {);}+ /// A short constant-rate file whose average is skewed by its length+ /// takes the base rate exactly; a stream whose average is nothing+ /// like its base rate is variable, and keeps the average.+ #[test]+ fn a_constant_rate_stream_takes_its_base_rate_exactly() {+ let skewed = Some(Rational::new(2700, 89));+ let thirty = Some(Rational::from_int(30));+ assert_eq!(pick_rate(skewed, thirty), thirty);+ let ntsc = Some(Rational::new(30000, 1001));+ assert_eq!(pick_rate(Some(Rational::new(29970, 1000)), ntsc), ntsc);+ // Variable: the base rate is the timestamps' denominator, not a rate.+ let variable = Some(Rational::new(24, 1));+ assert_eq!(+ pick_rate(variable, Some(Rational::from_int(1000))),+ variable+ );+ }+#[test]fn no_usable_rate_is_none() {assert_eq!(pick_rate(Some(Rational::ZERO), Some(Rational::ZERO)), None);