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(project): a key stays on its instant of the picture through split, trim, freeze and merge
Jareercbd961e988e5
diff --git a/src/crates/concat-project/src/commands/clips.rs b/src/crates/concat-project/src/commands/clips.rsindex 9d15599c..4a062546 100644--- a/src/crates/concat-project/src/commands/clips.rs+++ b/src/crates/concat-project/src/commands/clips.rs@@ -165,7 +165,12 @@ pub(super) fn apply(let applied = match edge {TrimEdge::End => {let duration = (clip.duration + delta).max(MIN_CLIP_DURATION);- assign(&mut clip.duration, duration)+ let old = clip.duration;+ let applied = assign(&mut clip.duration, duration);+ if applied {+ clip.rewindow_keys(old, 0.0, duration);+ }+ applied}TrimEdge::Start => {// Dragging the head moves the in-point too, so the pixels@@ -186,10 +191,15 @@ pub(super) fn apply(} else {(clip.source_start + moved * clip.speed).max(0.0)};+ let old = clip.duration;// Bitwise so no assignment is short-circuited away.- assign(&mut clip.start, start)+ let applied = assign(&mut clip.start, start)| assign(&mut clip.duration, duration)- | assign(&mut clip.source_start, source_start)+ | assign(&mut clip.source_start, source_start);+ if applied {+ clip.rewindow_keys(old, moved, old);+ }+ applied}};Ok(Outcome {@@ -231,17 +241,27 @@ pub(super) fn apply(offset,clip.reverse,);+ let whole = clip.duration;let mut tail = clip.clone();tail.id = mint.next("c");tail.start = clip.start + offset;tail.duration = clip.duration - offset;tail.source_start = tail_source;// The transition belongs to the cut at the original clip's- // start, which the head keeps.+ // start, which the head keeps; the way in belongs to the+ // head and the way out to the tail, so neither piece plays+ // an entrance or an exit the whole did not have at the cut.tail.transition_in = None;+ tail.fade_in = 0.0;+ tail.animation_in = None;+ tail.rewindow_keys(whole, offset, whole);created = Some(tail.id.clone());- timeline.clip_at_mut(index).duration = offset;- timeline.clip_at_mut(index).source_start = head_source;+ let head = timeline.clip_at_mut(index);+ head.duration = offset;+ head.source_start = head_source;+ head.fade_out = 0.0;+ head.animation_out = None;+ head.rewindow_keys(whole, 0.0, offset);timeline.clips.insert(index + 1, Arc::new(tail));}// A split always mints the tail, so "minted anything" and@@ -338,8 +358,15 @@ pub(super) fn apply(tail.duration = clip_duration - offset;tail.source_start = tail_source;tail.transition_in = None;- timeline.clip_at_mut(index).duration = offset;- timeline.clip_at_mut(index).source_start = head_source;+ tail.fade_in = 0.0;+ tail.animation_in = None;+ tail.rewindow_keys(clip_duration, offset, clip_duration);+ let head = timeline.clip_at_mut(index);+ head.duration = offset;+ head.source_start = head_source;+ head.fade_out = 0.0;+ head.animation_out = None;+ head.rewindow_keys(clip_duration, 0.0, offset);timeline.clips.insert(index + 1, Arc::new(tail));// Ripple every later placement on this track (including the new@@ -410,6 +437,14 @@ pub(super) fn apply(// clip's in-point is that.survivor.source_start = last.source_start;}+ // Every piece's keys land where they were on the picture; the+ // way out is the last piece's, as the way in is the first's.+ survivor.rewindow_keys(first.duration, 0.0, merged_duration);+ for piece in ordered.iter().skip(1) {+ survivor.absorb_keys(piece, piece.start - first.start);+ }+ survivor.fade_out = last.fade_out;+ survivor.animation_out = last.animation_out.clone();// A validated merge always absorbs at least one piece.Ok(Outcome {created_id: Some(first.id),diff --git a/src/crates/concat-project/src/lib.rs b/src/crates/concat-project/src/lib.rsindex 11f8d991..5dea7afd 100644--- a/src/crates/concat-project/src/lib.rs+++ b/src/crates/concat-project/src/lib.rs@@ -792,6 +792,239 @@ mod tests {);}+ /// A key marks an instant of the picture. Cutting the clip around it,+ /// trimming the clip under it and joining the pieces back must leave it+ /// on that instant, and a ride cut in two must play, over each piece,+ /// exactly what the whole played there.+ #[test]+ fn keys_stay_on_their_instant_through_split_trim_and_merge() {+ use crate::model::{KeyEase, KeyProperty};+ let (mut editor, _, clip_id) = fixture();+ // Opacity 0 at the head, 1 at the middle (5 s of a 10 s clip), 0.5 at the end.+ for (at, value) in [(0.0, 0.0), (0.5, 1.0), (1.0, 0.5)] {+ editor+ .apply(Command::SetClipKey {+ clip_id: clip_id.clone(),+ property: KeyProperty::Opacity,+ at,+ value,+ ease: KeyEase::LINEAR,+ })+ .expect("keys");+ }+ let whole = editor+ .project()+ .active()+ .clip(&clip_id)+ .expect("clip")+ .clone();+ let ride_at = |seconds: f64| whole.value_at(KeyProperty::Opacity, seconds / whole.duration);++ editor+ .apply(Command::SplitClips {+ clip_ids: vec![clip_id.clone()],+ time: 4.0,+ })+ .expect("splits");+ let timeline = editor.project().active();+ let head = timeline.clips[0].clone();+ let tail = timeline.clips[1].clone();+ // The head's ride over its four seconds is the whole's over 0-4 s;+ // the tail's over its six is the whole's over 4-10 s.+ for seconds in [0.0, 1.0, 2.5, 4.0] {+ let got = head.value_at(KeyProperty::Opacity, seconds / head.duration);+ assert!(+ (got - ride_at(seconds)).abs() < 1e-9,+ "head at {seconds}: {got}"+ );+ }+ for seconds in [4.0, 5.0, 7.0, 10.0] {+ let got = tail.value_at(KeyProperty::Opacity, (seconds - 4.0) / tail.duration);+ assert!(+ (got - ride_at(seconds)).abs() < 1e-9,+ "tail at {seconds}: {got}"+ );+ }+ assert_eq!(+ head.keys.len(),+ 2,+ "the head: its own key and one on the cut"+ );+ assert_eq!(+ tail.keys.len(),+ 3,+ "the tail: one on the cut and its own two"+ );++ // Trimming a second off the tail's head keeps the middle key on+ // the picture's 5 s: now one second into the tail.+ editor+ .apply(Command::TrimClip {+ clip_id: tail.id.clone(),+ edge: TrimEdge::Start,+ delta: 1.0,+ })+ .expect("trims");+ let trimmed = editor+ .project()+ .active()+ .clip(&tail.id)+ .expect("tail")+ .clone();+ let middle = trimmed+ .keys_on(KeyProperty::Opacity)+ .find(|key| (key.value - 1.0).abs() < 1e-9)+ .expect("the middle key");+ assert!(+ (middle.at * trimmed.duration - 0.0).abs() < 1e-9,+ "at {}",+ middle.at+ );+ // And back, so the pieces join again.+ editor+ .apply(Command::TrimClip {+ clip_id: tail.id.clone(),+ edge: TrimEdge::Start,+ delta: -1.0,+ })+ .expect("trims back");++ editor+ .apply(Command::MergeClips {+ clip_ids: vec![clip_id.clone(), tail.id.clone()],+ })+ .expect("merges");+ let merged = editor+ .project()+ .active()+ .clip(&clip_id)+ .expect("merged")+ .clone();+ assert_eq!(merged.duration, 10.0);+ for seconds in [0.0, 2.0, 4.0, 5.0, 8.0, 10.0] {+ let got = merged.value_at(KeyProperty::Opacity, seconds / merged.duration);+ assert!(+ (got - ride_at(seconds)).abs() < 1e-9,+ "merged at {seconds}: {got}"+ );+ }++ // Lengthening the end spreads nothing: the keys keep their seconds.+ editor+ .apply(Command::TrimClip {+ clip_id: clip_id.clone(),+ edge: TrimEdge::End,+ delta: 10.0,+ })+ .expect("extends");+ let longer = editor+ .project()+ .active()+ .clip(&clip_id)+ .expect("clip")+ .clone();+ let middle = longer+ .keys_on(KeyProperty::Opacity)+ .find(|key| (key.value - 1.0).abs() < 1e-9)+ .expect("the middle key");+ assert!((middle.at * longer.duration - 5.0).abs() < 1e-9);+ }++ #[test]+ fn a_split_leaves_the_entrance_with_the_head_and_the_exit_with_the_tail() {+ use crate::model::{AnimationSlot, ClipAnimation};+ let (mut editor, _, clip_id) = fixture();+ for (slot, preset) in [(AnimationSlot::In, "Fade"), (AnimationSlot::Out, "Fade")] {+ editor+ .apply(Command::SetClipAnimation {+ clip_id: clip_id.clone(),+ slot,+ animation: Some(ClipAnimation {+ preset: preset.to_owned(),+ duration: 0.5,+ }),+ })+ .expect("animates");+ }+ editor+ .apply(Command::UpdateClip {+ clip_id: clip_id.clone(),+ patch: ClipPatch {+ fade_in: Some(0.5),+ fade_out: Some(0.5),+ ..Default::default()+ },+ })+ .expect("fades");+ editor+ .apply(Command::SplitClips {+ clip_ids: vec![clip_id.clone()],+ time: 4.0,+ })+ .expect("splits");+ let timeline = editor.project().active();+ let (head, tail) = (&timeline.clips[0], &timeline.clips[1]);+ assert!(head.animation_in.is_some() && head.animation_out.is_none());+ assert!(tail.animation_in.is_none() && tail.animation_out.is_some());+ assert_eq!((head.fade_in, head.fade_out), (0.5, 0.0));+ assert_eq!((tail.fade_in, tail.fade_out), (0.0, 0.5));++ let ids: Vec<String> = timeline.clips.iter().map(|clip| clip.id.clone()).collect();+ editor+ .apply(Command::MergeClips { clip_ids: ids })+ .expect("merges");+ let clip = &editor.project().active().clips[0];+ assert!(clip.animation_in.is_some() && clip.animation_out.is_some());+ assert_eq!((clip.fade_in, clip.fade_out), (0.5, 0.5));+ }++ #[test]+ fn effect_keys_stay_on_their_instant_through_a_split() {+ use crate::model::{AppliedFilter, KeyEase};+ let (mut editor, _, clip_id) = fixture();+ editor+ .apply(Command::UpdateClip {+ clip_id: clip_id.clone(),+ patch: ClipPatch {+ video_effects: Some(vec![AppliedFilter::new("concat.vignette")]),+ ..Default::default()+ },+ })+ .expect("applies");+ for (at, value) in [(0.0, 10.0), (0.8, 90.0)] {+ editor+ .apply(Command::SetEffectKey {+ clip_id: clip_id.clone(),+ entry: 0,+ key: "strength".to_owned(),+ at,+ value,+ ease: KeyEase::LINEAR,+ })+ .expect("keys");+ }+ editor+ .apply(Command::SplitClips {+ clip_ids: vec![clip_id.clone()],+ time: 4.0,+ })+ .expect("splits");+ let timeline = editor.project().active();+ let (head, tail) = (&timeline.clips[0], &timeline.clips[1]);+ // The ride is 10 → 90 over 0-8 s: 50 at the cut.+ let at_cut_head = head.video_effects[0].value_at("strength", 1.0, 0.0);+ let at_cut_tail = tail.video_effects[0].value_at("strength", 0.0, 0.0);+ assert!((at_cut_head - 50.0).abs() < 1e-9, "{at_cut_head}");+ assert!((at_cut_tail - 50.0).abs() < 1e-9, "{at_cut_tail}");+ // The tail's second key is still on the picture's 8 s: four seconds in.+ let ninety = tail.video_effects[0]+ .keys_on("strength")+ .iter()+ .find(|key| (key.value - 90.0).abs() < 1e-9)+ .expect("the 90 key");+ assert!((ninety.at * tail.duration - 4.0).abs() < 1e-9);+ }+#[test]fn rearranged_pieces_refuse_to_merge() {let (mut editor, _, clip_id) = fixture();diff --git a/src/crates/concat-project/src/model.rs b/src/crates/concat-project/src/model.rsindex 417011c4..dc9c24a0 100644--- a/src/crates/concat-project/src/model.rs+++ b/src/crates/concat-project/src/model.rs@@ -1198,6 +1198,38 @@ pub struct Clip {pub extra: Map<String, Value>,}+/// One property's keys, as `(at, value, ease)` over `0..=1` of an old+/// length, re-anchored to the window `(a, b)` of that length; see+/// [`Clip::rewindow_keys`]. `value_at` is the ride over the old length.+fn rewindow(+ keys: impl Iterator<Item = (f64, f64, KeyEase)>,+ (a, b): (f64, f64),+ value_at: impl Fn(f64) -> f64,+) -> Vec<(f64, f64, KeyEase)> {+ let keys: Vec<(f64, f64, KeyEase)> = keys.collect();+ let span = b - a;+ let mut out = Vec::with_capacity(keys.len() + 2);+ let before = keys.iter().any(|(at, ..)| *at < a - KEY_EPSILON);+ if before {+ out.push((0.0, value_at(a), KeyEase::LINEAR));+ }+ let mut first_after: Option<KeyEase> = None;+ for (at, value, ease) in &keys {+ if *at < a - KEY_EPSILON {+ continue;+ }+ if *at > b + KEY_EPSILON {+ first_after.get_or_insert(*ease);+ continue;+ }+ out.push((((at - a) / span).clamp(0.0, 1.0), *value, *ease));+ }+ if let Some(ease) = first_after {+ out.push((1.0, value_at(b), ease));+ }+ out+}+impl Default for Clip {/// What a document entry starts from before its own fields land on/// it: [`Clip::blank`] with no id, on no track, one second of video.@@ -1459,6 +1491,106 @@ impl Clip {self.keys.len() != before}+ /// Re-anchors every key - the clip's own and its effects' - after the+ /// clip's span changed, so each key stays at the instant of the picture+ /// it was set on.+ ///+ /// Keys are stored as fractions of the clip's length, which is the+ /// right unit for a preset and the wrong one for an edit: a split, a+ /// trim or a merge changes the length under them, and without this a+ /// fade over the first half of a clip becomes a fade over the first+ /// half of each piece. `old` is the length the keys were set against;+ /// `[from, to]` is the window of that length the clip now covers, in+ /// seconds from its old start. A window past either end of the old+ /// clip (a trim that extends it, a merge) simply spreads the keys over+ /// the longer span, where the ride holds its end values anyway.+ ///+ /// A key outside the window is replaced by one on the window's edge+ /// carrying the ride's value there, so the piece plays exactly what+ /// the whole played over that stretch, and a merge of the pieces gets+ /// the ride back.+ pub fn rewindow_keys(&mut self, old: f64, from: f64, to: f64) {+ let sane = old.is_finite() && old > 0.0 && to.is_finite() && to > from;+ if !sane {+ return;+ }+ let (a, b) = (from / old, to / old);+ for property in KeyProperty::ALL {+ if !self.is_keyed(property) {+ continue;+ }+ let ride = self.track_on(property);+ let constant = self.constant(property);+ let keys: Vec<ClipKey> = self.keys_on(property).copied().collect();+ let windowed = rewindow(+ keys.iter().map(|key| (key.at, key.value, key.ease)),+ (a, b),+ |x| ride.value_at(x, constant),+ );+ self.keys.retain(|key| key.property != property);+ self.keys+ .extend(windowed.into_iter().map(|(at, value, ease)| ClipKey {+ property,+ at,+ value,+ ease,+ }));+ }+ for link in self.filters.iter_mut().chain(self.video_effects.iter_mut()) {+ let names: Vec<String> = link.keys.keys().cloned().collect();+ for name in names {+ let ride = link.track_on(&name);+ let constant = link.params.get(&name).copied().unwrap_or(0.0);+ let keys: Vec<ParamKey> = link.keys_on(&name).to_vec();+ let windowed = rewindow(+ keys.iter().map(|key| (key.at, key.value, key.ease)),+ (a, b),+ |x| ride.value_at(x, constant),+ );+ link.keys.insert(+ name,+ windowed+ .into_iter()+ .map(|(at, value, ease)| ParamKey { at, value, ease })+ .collect(),+ );+ }+ link.sort_keys();+ }+ self.sort_keys();+ }++ /// Takes on the keys of `piece`, a clip that sat `offset` seconds+ /// after this one's start and has been merged into it: each of its+ /// keys lands at the same instant of the picture it marked, now+ /// measured over this clip's `duration`. Effect keys come across where+ /// the effect at the same position of the chain is the same effect.+ pub fn absorb_keys(&mut self, piece: &Clip, offset: f64) {+ let sane = self.duration.is_finite() && self.duration > 0.0;+ if !sane {+ return;+ }+ for key in &piece.keys {+ let at = (offset + key.at * piece.duration) / self.duration;+ self.set_key(key.property, at, key.value, key.ease);+ }+ for (mine, theirs) in self.filters.iter_mut().zip(piece.filters.iter()).chain(+ self.video_effects+ .iter_mut()+ .zip(piece.video_effects.iter()),+ ) {+ if mine.id != theirs.id {+ continue;+ }+ for (name, run) in &theirs.keys {+ for key in run {+ let at = (offset + key.at * piece.duration) / self.duration;+ mine.set_key(name, at, key.value, key.ease);+ }+ }+ }+ }+/// Drops keys that are not finite or not in `0..=1`, then orders them./// Called by everything that can put a key in, including the document/// reader, so a hand-edited file cannot produce an unsorted track.