Skip to content

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

edit: a speed change with ripple moves later clips once

punkhopf06bfc4648fb
diff --git a/crates/edit/src/lib.rs b/crates/edit/src/lib.rs
index 3a84f6d33..2598a0fa0 100644
--- a/crates/edit/src/lib.rs
+++ b/crates/edit/src/lib.rs
@@ -751,41 +751,66 @@ pub fn rate_stretch(seq: &mut Sequence, clip: ClipId, edge: Edge, delta: Tick, c
/// Set speed/duration (Clip ▸ Speed/Duration…). `ripple` shifts following material.
pub fn set_speed(seq: &mut Sequence, clip: ClipId, speed: f64, reverse: bool, ripple: bool, ctx: &mut EditCtx) -> Result<()> {
+ set_speed_group(seq, &[clip], speed, reverse, ripple, ctx)
+}
+
+/// Speed/Duration on a clip and its linked partners on other tracks (its sound) as a single edit.
+/// `clips` holds at most one clip per track. Every member takes the speed, and with `ripple` the
+/// later material moves once, on the members' tracks and on sync-locked tracks. Rippling each
+/// member on its own would move the later clips once per member. When the members' lengths change
+/// by different amounts (a split edit), the later material moves by the largest change, so no
+/// member is overlapped and the tracks stay in sync. Nothing changes when the edit fails.
+pub fn set_speed_group(seq: &mut Sequence, clips: &[ClipId], speed: f64, reverse: bool, ripple: bool, ctx: &mut EditCtx) -> Result<()> {
if speed <= 0.0 {
return Err(EditError::Other("speed must be positive".into()));
}
- let (tid, it) = seq.find_item(clip).ok_or(EditError::NoItem(clip))?;
- let src_len = it.source_out() - it.source_in;
- let mut new_dur = Tick((src_len.0 as f64 / speed).round() as i64).max(ctx.min_duration);
- let old_end = it.end();
- let tr = seq.track(tid).ok_or(EditError::NoTrack(tid))?;
- let (_, next_start) = neighbours(tr, clip);
- if !ripple {
- new_dur = new_dur.min(next_start - it.start);
- }
- let delta = new_dur - it.duration;
- let before = ripple.then(|| seq.clone());
- {
- let (_, it) = seq.find_item_mut(clip).ok_or(EditError::NoItem(clip))?;
+ let mut work = seq.clone();
+ // per member track: where its later material starts, and by how much the member's length changed
+ let mut origins: Vec<(TrackId, Tick, Tick)> = Vec::new();
+ for clip in clips {
+ let (tid, it) = work.find_item(*clip).ok_or(EditError::NoItem(*clip))?;
+ if origins.iter().any(|o| o.0 == tid) {
+ return Err(EditError::Other("clips on one track change speed one at a time".into()));
+ }
+ let src_len = it.source_out() - it.source_in;
+ let mut new_dur = Tick((src_len.0 as f64 / speed).round() as i64).max(ctx.min_duration);
+ let old_end = it.end();
+ let tr = work.track(tid).ok_or(EditError::NoTrack(tid))?;
+ let (_, next_start) = neighbours(tr, *clip);
+ if !ripple {
+ new_dur = new_dur.min(next_start - it.start);
+ }
+ let delta = new_dur - it.duration;
+ let (_, it) = work.find_item_mut(*clip).ok_or(EditError::NoItem(*clip))?;
it.speed = speed;
it.reverse = reverse;
it.duration = new_dur;
+ origins.push((tid, old_end, delta));
}
- if ripple && delta != Tick::ZERO {
- for tr in seq.all_tracks_mut() {
- if !tr.locked && (tr.id == tid || tr.sync_lock) {
- for i in &mut tr.items {
- if i.id != clip && i.start >= old_end {
- i.start += delta;
- }
+ let main_from = origins.first().map(|o| o.1).unwrap_or_default();
+ let shift = origins.iter().map(|o| o.2).max().unwrap_or_default();
+ if ripple && shift != Tick::ZERO {
+ for tr in work.all_tracks_mut() {
+ if tr.locked {
+ continue;
+ }
+ let member = origins.iter().find(|o| o.0 == tr.id).map(|o| o.1);
+ if member.is_none() && !tr.sync_lock {
+ continue;
+ }
+ let from = member.unwrap_or(main_from);
+ for i in &mut tr.items {
+ if !clips.contains(&i.id) && i.start >= from {
+ i.start += shift;
}
- tr.sort();
}
+ tr.sort();
}
}
- if let Some(before) = before {
- transitions_follow_cuts(&before, seq);
+ if ripple {
+ transitions_follow_cuts(seq, &mut work);
}
+ *seq = work;
Ok(())
}
diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rs
index fac06e430..aea3ceead 100644
--- a/crates/edit/src/tests.rs
+++ b/crates/edit/src/tests.rs
@@ -269,6 +269,44 @@ fn rate_stretch_and_speed() {
assert_eq!(fx.spans(v1), vec![(0, 5)]);
}
+#[test]
+fn speed_on_a_linked_group_ripples_once() {
+ // V1: a [0,10), b [10,20). A1: a's sound [0,10), b's sound [10,20).
+ let build = |sound_len: i64| {
+ let mut fx = Fx::new();
+ let (v1, a1) = (fx.v(0), fx.a(0));
+ let a = fx.put(v1, 0, 10, 0);
+ fx.put(v1, 10, 10, 0);
+ let sound = fx.put(a1, 0, sound_len, 0);
+ fx.put(a1, sound_len, 10, 0);
+ (fx, a, sound)
+ };
+ for (speed, picture, later) in [(0.5, (0, 20), (20, 10)), (2.0, (0, 5), (5, 10))] {
+ let (mut fx, a, sound) = build(10);
+ let mut n = fx.next;
+ set_speed_group(&mut fx.seq, &[a, sound], speed, false, true, &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!((fx.spans(fx.v(0)), fx.spans(fx.a(0))), (vec![picture, later], vec![picture, later]), "{speed}");
+ }
+ // a split edit: the sound is 4 longer. Later clips move by the larger change, on both tracks.
+ let (mut fx, a, sound) = build(14);
+ let mut n = fx.next;
+ set_speed_group(&mut fx.seq, &[a, sound], 0.5, false, true, &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!((fx.spans(fx.v(0)), fx.spans(fx.a(0))), (vec![(0, 20), (24, 10)], vec![(0, 28), (28, 10)]));
+ let (mut fx, a, sound) = build(14);
+ let mut n = fx.next;
+ set_speed_group(&mut fx.seq, &[a, sound], 2.0, false, true, &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!((fx.spans(fx.v(0)), fx.spans(fx.a(0))), (vec![(0, 5), (5, 10)], vec![(0, 7), (9, 10)]));
+ fx.seq.check().unwrap();
+ // two clips of one track are not one group; a missing clip changes nothing
+ let (mut fx, a, _) = build(10);
+ let b = fx.seq.video_tracks[0].items[1].id;
+ let before = fx.seq.clone();
+ let mut n = fx.next;
+ assert!(set_speed_group(&mut fx.seq, &[a, b], 2.0, false, true, &mut Fx::ctx(&mut n)).is_err());
+ assert!(set_speed_group(&mut fx.seq, &[a, ClipId(1)], 2.0, false, true, &mut Fx::ctx(&mut n)).is_err());
+ assert_eq!(fx.seq, before);
+}
+
#[test]
fn move_with_insert_and_overwrite() {
let mut fx = Fx::new();
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index e5cde54e5..cc895dc03 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -1034,8 +1034,26 @@ fn build() -> Vec<CommandSpec> {
};
let sel = with_links(s, &clips_p(s, p));
s.edit_sequence("Speed/Duration", |q, ctx, _| {
+ // a clip and its linked partners on other tracks change as one edit, so a ripple
+ // moves the later clips once
+ let place = |q: &filmcraft_project::Sequence, c: ClipId| q.find_item(c).map(|(t, i)| (t, i.link));
+ let mut groups: Vec<Vec<ClipId>> = Vec::new();
+ for c in &sel {
+ // a clip that is not there goes on alone, so the edit fails with "no such item"
+ let Some((track, link)) = place(q, *c) else {
+ groups.push(vec![*c]);
+ continue;
+ };
+ let joins = |g: &Vec<ClipId>| link.is_some() && g.iter().all(|o| place(q, *o).is_some_and(|(t, l)| l == link && t != track));
+ match groups.iter_mut().find(|g| joins(g)) {
+ Some(g) => g.push(*c),
+ None => groups.push(vec![*c]),
+ }
+ }
+ for group in &groups {
+ edit::set_speed_group(q, group, speed, reverse, ripple, ctx)?;
+ }
for c in &sel {
- edit::set_speed(q, *c, speed, reverse, ripple, ctx)?;
if let (Some(m), Some((_, it))) = (interp, q.find_item_mut(*c)) {
it.time_interpolation = m;
}
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index a5f34e61d..2e078e5b6 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -888,6 +888,8 @@ mod image_sequence_tests;
#[cfg(test)]
mod keyboard_tests;
#[cfg(test)]
+mod linked_speed_tests;
+#[cfg(test)]
mod masks_tests;
#[cfg(test)]
mod media_browser_tests;
diff --git a/crates/engine/src/linked_speed_tests.rs b/crates/engine/src/linked_speed_tests.rs
new file mode 100644
index 000000000..edf3c0216
--- /dev/null
+++ b/crates/engine/src/linked_speed_tests.rs
@@ -0,0 +1,152 @@
+//! `clip.speedDuration` with `ripple` on a clip that has linked sound: the clip and its sound
+//! change as one edit, and the later clips move once.
+
+use filmcraft_project::ClipId;
+use filmcraft_time::Tick;
+use serde_json::json;
+
+use crate::Session;
+
+fn range(s: &Session, c: ClipId) -> (Tick, Tick) {
+ s.active_sequence().unwrap().find_item(c).map(|(_, i)| (i.start, i.end())).unwrap()
+}
+
+/// Three 48-frame clips in a row, pictures on V1 and their linked sounds on A1: [(picture, sound); 3].
+fn three_clips(s: &mut Session) -> [(ClipId, ClipId); 3] {
+ s.execute("file.openDemoProject", json!({})).unwrap();
+ s.execute("file.newSequence", json!({"name": "Speed", "video": 1, "audio": 1})).unwrap();
+ let rate = s.sequence_rate();
+ let item = s.project.items.values().find(|i| i.has_video() && i.has_audio() && i.as_media().is_some()).map(|i| i.id).unwrap();
+ for n in 0..3 {
+ s.execute("timeline.place", json!({"item": item.0, "frame": 48 * n, "sourceIn": rate.tick_of(48).0, "duration": rate.tick_of(48).0})).unwrap();
+ }
+ let q = s.active_sequence().unwrap();
+ [0, 1, 2].map(|n| (q.video_tracks[0].items[n].id, q.audio_tracks[0].items[n].id))
+}
+
+/// Slower with ripple moved the later clips twice (once for the picture, once for its sound) and
+/// left a double gap.
+#[test]
+fn slower_with_ripple_moves_later_clips_once() {
+ let mut s = Session::default();
+ let [_, (picture2, sound2), (picture3, sound3)] = three_clips(&mut s);
+ let f = |n: i64| s.sequence_rate().tick_of(n);
+ let (f48, f144, f192) = (f(48), f(144), f(192));
+ let before = s.project.clone();
+ s.execute("clip.speedDuration", json!({"clips": [picture2.0], "speed": 50, "ripple": true})).unwrap();
+ let q = s.active_sequence().unwrap();
+ q.check().unwrap();
+ assert_eq!((range(&s, picture2), range(&s, sound2)), ((f48, f144), (f48, f144)), "the clip and its sound are twice as long");
+ assert_eq!((range(&s, picture3), range(&s, sound3)), ((f144, f192), (f144, f192)), "the next clip follows, once");
+ assert_eq!(q.duration(), f192);
+ let after = s.project.clone();
+ s.execute("edit.undo", json!({})).unwrap();
+ assert_eq!(*s.project, *before, "one undo step restores it");
+ s.execute("edit.redo", json!({})).unwrap();
+ assert_eq!(*s.project, *after);
+}
+
+/// Faster with ripple was refused when more than one clip followed: the second ripple pulled the
+/// farther clips onto the nearer ones.
+#[test]
+fn faster_with_ripple_moves_later_clips_once() {
+ let mut s = Session::default();
+ let [(picture1, sound1), (picture2, sound2), (picture3, sound3)] = three_clips(&mut s);
+ let f = |n: i64| s.sequence_rate().tick_of(n);
+ let (f0, f24, f72, f120) = (f(0), f(24), f(72), f(120));
+ s.execute("clip.speedDuration", json!({"clips": [picture1.0], "speed": 200, "ripple": true})).unwrap();
+ s.active_sequence().unwrap().check().unwrap();
+ for (picture, sound, at) in [(picture1, sound1, (f0, f24)), (picture2, sound2, (f24, f72)), (picture3, sound3, (f72, f120))] {
+ assert_eq!((range(&s, picture), range(&s, sound)), (at, at));
+ }
+}
+
+/// A clip that is not in the sequence is an error and nothing changes, also next to one that is.
+#[test]
+fn speed_on_a_missing_clip_is_an_error() {
+ let mut s = Session::default();
+ let [_, (picture2, _), _] = three_clips(&mut s);
+ let before = s.project.clone();
+ let undo = s.history.undo.len();
+ assert!(s.execute("clip.speedDuration", json!({"clips": [999_999], "speed": 50, "ripple": true})).is_err());
+ assert!(s.execute("clip.speedDuration", json!({"clips": [picture2.0, 999_999], "speed": 50, "ripple": true})).is_err());
+ assert_eq!(*s.project, *before);
+ assert_eq!(s.history.undo.len(), undo);
+}
+
+/// Several clips in one call each ripple in turn, as before.
+#[test]
+fn speed_with_ripple_on_two_clips_ripples_each() {
+ let mut s = Session::default();
+ let [(picture1, sound1), (picture2, sound2), (picture3, sound3)] = three_clips(&mut s);
+ let f = |n: i64| s.sequence_rate().tick_of(n);
+ let (f0, f24, f48, f96) = (f(0), f(24), f(48), f(96));
+ s.execute("clip.speedDuration", json!({"clips": [picture1.0, picture2.0], "speed": 200, "ripple": true})).unwrap();
+ s.active_sequence().unwrap().check().unwrap();
+ for (picture, sound, at) in [(picture1, sound1, (f0, f24)), (picture2, sound2, (f24, f48)), (picture3, sound3, (f48, f96))] {
+ assert_eq!((range(&s, picture), range(&s, sound)), (at, at));
+ }
+}
+
+/// Without `ripple` nothing after the clip moves.
+#[test]
+fn speed_without_ripple_leaves_later_clips() {
+ let mut s = Session::default();
+ let [_, (picture2, sound2), (picture3, sound3)] = three_clips(&mut s);
+ let f = |n: i64| s.sequence_rate().tick_of(n);
+ let (f48, f72, f96, f144) = (f(48), f(72), f(96), f(144));
+ s.execute("clip.speedDuration", json!({"clips": [picture2.0], "speed": 200})).unwrap();
+ assert_eq!((range(&s, picture2), range(&s, sound2)), ((f48, f72), (f48, f72)));
+ assert_eq!((range(&s, picture3), range(&s, sound3)), ((f96, f144), (f96, f144)));
+}
+
+/// Linked Selection off and both the picture and its sound named: still one edit, one ripple.
+#[test]
+fn speed_with_ripple_on_a_named_picture_and_sound() {
+ let mut s = Session::default();
+ let [_, (picture2, sound2), (picture3, sound3)] = three_clips(&mut s);
+ let f = |n: i64| s.sequence_rate().tick_of(n);
+ let (f48, f144, f192) = (f(48), f(144), f(192));
+ s.execute("sequence.linkedSelection", json!({"on": false})).unwrap();
+ s.execute("clip.speedDuration", json!({"clips": [picture2.0, sound2.0], "speed": 50, "ripple": true})).unwrap();
+ assert_eq!((range(&s, picture2), range(&s, sound2)), ((f48, f144), (f48, f144)));
+ assert_eq!((range(&s, picture3), range(&s, sound3)), ((f144, f192), (f144, f192)));
+}
+
+/// Two clips of one track that were linked to each other (Clip ▸ Link) ripple one after the
+/// other, as unlinked clips do.
+#[test]
+fn speed_with_ripple_on_linked_clips_of_one_track() {
+ let mut s = Session::default();
+ let [(picture1, _), (picture2, _), (picture3, _)] = three_clips(&mut s);
+ let f = |n: i64| s.sequence_rate().tick_of(n);
+ let (f0, f96, f192, f240) = (f(0), f(96), f(192), f(240));
+ s.execute("timeline.setTrack", json!({"track": "A1", "syncLock": false})).unwrap();
+ s.execute("sequence.linkedSelection", json!({"on": false})).unwrap();
+ s.execute("timeline.select", json!({"clips": [picture1.0, picture2.0]})).unwrap();
+ // both are linked to their sounds, so the first call unlinks them and the second links the two
+ s.execute("clip.link", json!({})).unwrap();
+ assert_eq!(s.execute("clip.link", json!({})).unwrap()["linked"], true);
+ s.execute("sequence.linkedSelection", json!({"on": true})).unwrap();
+ s.execute("clip.speedDuration", json!({"clips": [picture1.0], "speed": 50, "ripple": true})).unwrap();
+ s.active_sequence().unwrap().check().unwrap();
+ assert_eq!((range(&s, picture1), range(&s, picture2), range(&s, picture3)), ((f0, f96), (f96, f192), (f192, f240)));
+}
+
+/// A split edit: the sound runs 12 frames longer than its picture. Slower, the later clips move
+/// by the sound's change, so nothing lands on the sound and the next clip stays in sync.
+#[test]
+fn speed_with_ripple_on_a_split_edit() {
+ let mut s = Session::default();
+ let [_, (picture2, sound2), (picture3, sound3)] = three_clips(&mut s);
+ let f = |n: i64| s.sequence_rate().tick_of(n);
+ let (f48, f144, f156, f168) = (f(48), f(144), f(156), f(168));
+ s.execute("sequence.linkedSelection", json!({"on": false})).unwrap();
+ s.execute("timeline.trim", json!({"clip": sound3.0, "edge": "in", "mode": "regular", "deltaFrames": 12})).unwrap();
+ s.execute("timeline.trim", json!({"clip": sound2.0, "edge": "out", "mode": "regular", "deltaFrames": 12})).unwrap();
+ s.execute("sequence.linkedSelection", json!({"on": true})).unwrap();
+ s.execute("clip.speedDuration", json!({"clips": [picture2.0], "speed": 50, "ripple": true})).unwrap();
+ s.active_sequence().unwrap().check().unwrap();
+ assert_eq!((range(&s, picture2), range(&s, sound2)), ((f48, f144), (f48, f168)), "picture 48 to 96 frames, sound 60 to 120");
+ assert_eq!((range(&s, picture3).0, range(&s, sound3).0), (f156, f168), "the next sound still starts 12 after its picture");
+}