Skip to content

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

Keep linked rate-stretch partners at one speed.

Vikram Bhaskaran53b99ae33517
diff --git a/crates/edit/src/lib.rs b/crates/edit/src/lib.rs
index bb4e6415b..39242e26d 100644
--- a/crates/edit/src/lib.rs
+++ b/crates/edit/src/lib.rs
@@ -999,27 +999,52 @@ pub fn slide_items(seq: &mut Sequence, clips: &[ClipId], delta: Tick, ctx: &mut
Ok(d)
}
-/// Rate stretch: change an item's duration by dragging an edge, adjusting speed to keep the same media.
-pub fn rate_stretch(seq: &mut Sequence, clip: ClipId, edge: Edge, delta: Tick, ctx: &mut EditCtx) -> Result<f64> {
+/// How far `clip` can rate-stretch on `edge` without overlapping a neighbour or going shorter than
+/// one frame. Linked partners use this so they all take the tightest room.
+pub fn clamp_rate_stretch(seq: &Sequence, clip: ClipId, edge: Edge, delta: Tick, ctx: &EditCtx) -> Result<Tick> {
let (tid, it) = seq.find_item(clip).ok_or(EditError::NoItem(clip))?;
let tr = seq.track(tid).ok_or(EditError::NoTrack(tid))?;
let (prev_end, next_start) = neighbours(tr, clip);
- let src_len = it.source_out() - it.source_in;
- let d = match edge {
+ Ok(match edge {
Edge::Out => delta.clamp(-(it.duration - ctx.min_duration), next_start - it.end()),
Edge::In => delta.clamp(-(it.start - prev_end), it.duration - ctx.min_duration),
- };
- let new_dur = match edge {
- Edge::Out => it.duration + d,
- Edge::In => it.duration - d,
- };
- let speed = src_len.0 as f64 / new_dur.0 as f64;
- let (_, it) = seq.find_item_mut(clip).ok_or(EditError::NoItem(clip))?;
- if edge == Edge::In {
- it.start += d;
+ })
+}
+
+/// Rate stretch: change an item's duration by dragging an edge, adjusting speed to keep the same media.
+pub fn rate_stretch(seq: &mut Sequence, clip: ClipId, edge: Edge, delta: Tick, ctx: &mut EditCtx) -> Result<f64> {
+ rate_stretch_items(seq, &[clip], edge, delta, ctx)
+}
+
+/// Rate stretch a clip and its linked partners as one edit: the tightest neighbour on any member
+/// clamps them all, so they keep one speed. A neighbour on the sound track cannot leave the picture
+/// stretching further (and the pair still linked at different rates).
+pub fn rate_stretch_items(seq: &mut Sequence, clips: &[ClipId], edge: Edge, delta: Tick, ctx: &mut EditCtx) -> Result<f64> {
+ let mut d = delta;
+ for c in clips {
+ let x = clamp_rate_stretch(seq, *c, edge, d, ctx)?;
+ if x.0.signum() != d.0.signum() {
+ d = Tick::ZERO;
+ } else if x.abs() < d.abs() {
+ d = x;
+ }
+ }
+ let mut speed = 1.0;
+ for c in clips {
+ let (_, it) = seq.find_item(*c).ok_or(EditError::NoItem(*c))?;
+ let src_len = it.source_out() - it.source_in;
+ let new_dur = match edge {
+ Edge::Out => it.duration + d,
+ Edge::In => it.duration - d,
+ };
+ speed = src_len.0 as f64 / new_dur.0 as f64;
+ let (_, it) = seq.find_item_mut(*c).ok_or(EditError::NoItem(*c))?;
+ if edge == Edge::In {
+ it.start += d;
+ }
+ it.duration = new_dur;
+ it.speed = speed;
}
- it.duration = new_dur;
- it.speed = speed;
Ok(speed)
}
diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rs
index 9272a94da..a6ae3b4cf 100644
--- a/crates/edit/src/tests.rs
+++ b/crates/edit/src/tests.rs
@@ -537,6 +537,29 @@ fn rate_stretch_and_speed() {
assert_eq!(fx.spans(v1), vec![(0, 5)]);
}
+#[test]
+fn linked_rate_stretch_uses_the_tighter_neighbour() {
+ // picture and sound are 60 frames; a later clip on A1 starts at 70, so the sound has only 10
+ // frames of room. Stretching the pair +20 must not leave the picture at 80f / 0.75x and the
+ // sound at 70f / 6/7x while still linked.
+ let mut fx = Fx::new();
+ let (v1, a1) = (fx.v(0), fx.a(0));
+ let pic = fx.put(v1, 0, 60, 0);
+ let snd = fx.put(a1, 0, 60, 0);
+ fx.put(a1, 70, 10, 0);
+ for c in [pic, snd] {
+ fx.seq.find_item_mut(c).unwrap().1.link = Some(1);
+ }
+ let mut n = fx.next;
+ let speed = rate_stretch_items(&mut fx.seq, &[pic, snd], Edge::Out, f(20), &mut Fx::ctx(&mut n)).unwrap();
+ assert!((speed - 60.0 / 70.0).abs() < 1e-9);
+ let (p, s) = (fx.seq.find_item(pic).unwrap().1, fx.seq.find_item(snd).unwrap().1);
+ assert_eq!((R.frame_at(p.duration), R.frame_at(s.duration)), (70, 70));
+ assert!((p.speed - s.speed).abs() < 1e-12);
+ assert_eq!(fx.spans(a1), vec![(0, 70), (70, 10)]);
+ fx.seq.check().unwrap();
+}
+
#[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).
diff --git a/crates/engine/src/clip_ops_tests.rs b/crates/engine/src/clip_ops_tests.rs
index 68eb470f1..ea2d64656 100644
--- a/crates/engine/src/clip_ops_tests.rs
+++ b/crates/engine/src/clip_ops_tests.rs
@@ -988,3 +988,25 @@ fn keyboard_slip_keeps_linked_clips_in_sync_past_the_shorter_handle() {
s.execute("timeline.slipLeft5", json!({})).unwrap();
assert_eq!((clip(&s, v).source_in, clip(&s, a).source_in), (Tick(fr.0 * 30), Tick(fr.0 * 35)));
}
+
+#[test]
+fn linked_rate_stretch_keeps_one_speed_when_a_neighbour_blocks_the_sound() {
+ let mut s = Session::default();
+ s.execute("file.newProject", json!({"name": "Stretch"})).unwrap();
+ s.execute("file.newSequence", json!({"name": "Sequence", "fps": 25, "video": 1, "audio": 1, "width": 16, "height": 16})).unwrap();
+ let r = s.execute("file.newOfflineFile", json!({"name": "A", "seconds": 8, "fps": 25, "video": true, "audio": true})).unwrap();
+ let a = r["item"].as_u64().unwrap();
+ let r = s.execute("file.newOfflineFile", json!({"name": "B", "seconds": 2, "fps": 25, "video": true, "audio": true})).unwrap();
+ let b = r["item"].as_u64().unwrap();
+ let fr = s.sequence_rate().tick_of(1);
+ let r = s.execute("timeline.place", json!({"item": a, "track": "V1", "audioTrack": "A1", "time": 0, "duration": fr.0 * 60})).unwrap();
+ let (v, snd) = (r["clips"][0].as_u64().unwrap(), r["clips"][1].as_u64().unwrap());
+ s.execute("sequence.linkedSelection", json!({"on": false})).unwrap();
+ s.execute("timeline.place", json!({"item": b, "track": "A1", "time": fr.0 * 70, "duration": fr.0 * 10})).unwrap();
+ s.execute("sequence.linkedSelection", json!({"on": true})).unwrap();
+ let r = s.execute("timeline.rateStretch", json!({"clip": v, "edge": "out", "deltaFrames": 20})).unwrap();
+ let (pic, sound) = (clip(&s, v), clip(&s, snd));
+ assert_eq!((pic.duration, sound.duration), (Tick(fr.0 * 70), Tick(fr.0 * 70)));
+ assert!((pic.speed - sound.speed).abs() < 1e-12);
+ assert!((r["speed"].as_f64().unwrap() - 60.0 / 70.0).abs() < 1e-9);
+}
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index 6609afddf..6cd201471 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -2454,13 +2454,7 @@ fn build() -> Vec<CommandSpec> {
.or_else(|| p.get("deltaFrames").and_then(Value::as_i64).map(|f| s.sequence_rate().tick_of(f)))
.unwrap_or_default();
let clips = with_links(s, &[c]);
- let sp = s.edit_sequence("Rate Stretch", |q, ctx, _| {
- let mut sp = 1.0;
- for c in &clips {
- sp = edit::rate_stretch(q, *c, edge, d, ctx)?;
- }
- Ok(sp)
- })?;
+ let sp = s.edit_sequence("Rate Stretch", |q, ctx, _| Ok(edit::rate_stretch_items(q, &clips, edge, d, ctx)?))?;
Ok(json!({"speed": sp}))
}),
cmd!("timeline.razor", "Razor", [], None, r#"{"time":ticks,"track":"V1"|id?,"clip":id?}"#, has_seq, |s, p| {