Skip to content

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

Roll linked audio with the picture cut (#855).

Vikram Bhaskaran111991591b9f
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index 6609afddf..2e76582f9 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -411,6 +411,47 @@ pub(crate) fn slip_together(q: &mut filmcraft_project::Sequence, ctx: &mut edit:
Ok(())
}
+/// Adjacent roll pairs that follow `left`/`right` when linked selection is on: the given cut, plus
+/// through-edits between the same pieces' linked partners at that same time (Premiere linked
+/// selection). Split edits (audio cut at another time) and unlinked clips are not added.
+pub fn linked_roll_pairs(seq: &filmcraft_project::Sequence, left: ClipId, right: ClipId, follow_links: bool) -> Vec<(ClipId, ClipId)> {
+ let mut pairs = vec![(left, right)];
+ if !follow_links {
+ return pairs;
+ }
+ let Some((_, l)) = seq.find_item(left) else {
+ return pairs;
+ };
+ let Some((_, r)) = seq.find_item(right) else {
+ return pairs;
+ };
+ let cut = l.end();
+ let left_link = l.link;
+ let right_link = r.link;
+ if left_link.is_none() && right_link.is_none() {
+ return pairs;
+ }
+ for tr in seq.all_tracks() {
+ if tr.locked {
+ continue;
+ }
+ for w in tr.items.windows(2) {
+ if w[0].id == left && w[1].id == right {
+ continue;
+ }
+ if w[0].end() != w[1].start || w[0].end() != cut {
+ continue;
+ }
+ let left_ok = left_link.is_some_and(|g| w[0].link == Some(g));
+ let right_ok = right_link.is_some_and(|g| w[1].link == Some(g));
+ if left_ok && right_ok {
+ pairs.push((w[0].id, w[1].id));
+ }
+ }
+ }
+ pairs
+}
+
/// Expand a clip selection with linked partners (when linked selection is on).
pub fn with_links(s: &Session, clips: &[ClipId]) -> Vec<ClipId> {
let Some(seq) = s.active_sequence() else { return clips.to_vec() };
@@ -2412,8 +2453,26 @@ fn build() -> Vec<CommandSpec> {
.map(Tick)
.or_else(|| p.get("deltaFrames").and_then(Value::as_i64).map(|f| s.sequence_rate().tick_of(f)))
.unwrap_or_default();
- let x = s.edit_sequence("Rolling Edit", |q, ctx, _| Ok(edit::roll(q, l, r, d, ctx)?))?;
- Ok(json!({"delta": x.0}))
+ let follow = s.state.linked_selection;
+ let (x, n, linked) = s.edit_sequence("Rolling Edit", |q, ctx, _| {
+ let pairs = linked_roll_pairs(q, l, r, follow);
+ let linked = pairs.len() > 1;
+ let mut d = d;
+ for &(pl, pr) in &pairs {
+ let mut probe = q.clone();
+ let x = edit::roll(&mut probe, pl, pr, d, ctx)?;
+ if x.abs() < d.abs() {
+ d = x;
+ }
+ }
+ if d != Tick::ZERO {
+ for &(pl, pr) in &pairs {
+ edit::roll(q, pl, pr, d, ctx)?;
+ }
+ }
+ Ok((d, pairs.len(), linked))
+ })?;
+ Ok(json!({"delta": x.0, "pairs": n, "linked": linked}))
}),
cmd!("timeline.slip", "Slip", [], None, r#"{"clip":id,"delta":ticks|"deltaFrames":i64}"#, has_seq, |s, p| {
let c = clip_p(p, "clip").ok_or_else(|| bad("timeline.slip", "need `clip`"))?;
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index a2ed9e6a2..2d9a84261 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -1156,6 +1156,8 @@ mod remix_tests;
#[cfg(test)]
mod ripple_delete_tests;
#[cfg(test)]
+mod roll_linked_tests;
+#[cfg(test)]
mod scopes_tests;
#[cfg(test)]
mod sequence_inspect_tests;
diff --git a/crates/engine/src/roll_linked_tests.rs b/crates/engine/src/roll_linked_tests.rs
new file mode 100644
index 000000000..dd3f2e4b2
--- /dev/null
+++ b/crates/engine/src/roll_linked_tests.rs
@@ -0,0 +1,86 @@
+//! #855: a rolling edit on a linked A/V cut must move the sound cut with the picture.
+
+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()
+}
+
+fn source_in(s: &Session, c: ClipId) -> Tick {
+ s.active_sequence().unwrap().find_item(c).unwrap().1.source_in
+}
+
+/// Two consecutive 48-frame A/V clips (picture on V1, linked sound on A1).
+fn two_clips(s: &mut Session) -> [(ClipId, ClipId); 2] {
+ s.execute("file.openDemoProject", json!({})).unwrap();
+ s.execute("file.newSequence", json!({"name": "Roll", "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..2 {
+ 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].map(|n| (q.video_tracks[0].items[n].id, q.audio_tracks[0].items[n].id))
+}
+
+/// #855: rolling the V1 cut +12 frames used to leave A1, so export played the second clip's audio
+/// under the first clip's picture.
+#[test]
+fn roll_moves_linked_audio_cut() {
+ let mut s = Session::default();
+ let [(v0, a0), (v1, a1)] = two_clips(&mut s);
+ assert!(s.state.linked_selection);
+ let rate = s.sequence_rate();
+ let f = |n: i64| rate.tick_of(n);
+ let r = s.execute("timeline.roll", json!({"left": v0.0, "right": v1.0, "deltaFrames": 12})).unwrap();
+ assert_eq!(r["linked"], json!(true), "{r}");
+ assert_eq!(r["pairs"], json!(2), "{r}");
+ assert_eq!(range(&s, v0), (f(0), f(60)));
+ assert_eq!(range(&s, v1), (f(60), f(96)));
+ assert_eq!(range(&s, a0), (f(0), f(60)), "linked sound must roll with the picture");
+ assert_eq!(range(&s, a1), (f(60), f(96)));
+}
+
+/// Linked selection off (or unlinked clips) rolls only the given pair and says so.
+#[test]
+fn roll_without_linked_selection_leaves_audio() {
+ let mut s = Session::default();
+ let [(v0, a0), (v1, a1)] = two_clips(&mut s);
+ s.execute("sequence.linkedSelection", json!({"on": false})).unwrap();
+ let rate = s.sequence_rate();
+ let f = |n: i64| rate.tick_of(n);
+ let r = s.execute("timeline.roll", json!({"left": v0.0, "right": v1.0, "deltaFrames": 12})).unwrap();
+ assert_eq!(r["linked"], json!(false), "{r}: clips were not followed as a linked pair");
+ assert_eq!(range(&s, v0), (f(0), f(60)));
+ assert_eq!(range(&s, v1), (f(60), f(96)));
+ assert_eq!(range(&s, a0), (f(0), f(48)), "unlinked A1 cut stays");
+ assert_eq!(range(&s, a1), (f(48), f(96)));
+}
+
+/// #526/#558: reversed linked clips still keep the media on the unmoved timeline edges.
+#[test]
+fn roll_linked_reversed_clips_keeps_media_mapping() {
+ let mut s = Session::default();
+ let [(v0, a0), (v1, a1)] = two_clips(&mut s);
+ s.execute("timeline.select", json!({"clips": [v0.0, a0.0, v1.0, a1.0]})).unwrap();
+ s.execute("clip.speedDuration", json!({"speed": 100, "reverse": true})).unwrap();
+ let rate = s.sequence_rate();
+ let f = |n: i64| rate.tick_of(n);
+ let left_in = source_in(&s, v0);
+ let right_in = source_in(&s, v1);
+ s.execute("timeline.roll", json!({"left": v0.0, "right": v1.0, "deltaFrames": 12})).unwrap();
+ assert_eq!(range(&s, v0), (f(0), f(60)));
+ assert_eq!(range(&s, a0), (f(0), f(60)));
+ assert_eq!(range(&s, v1), (f(60), f(96)));
+ assert_eq!(range(&s, a1), (f(60), f(96)));
+ // reversed left: source_in moves so the clip's In still shows the same media
+ assert_eq!(source_in(&s, v0), left_in - f(12));
+ assert_eq!(source_in(&s, a0), left_in - f(12));
+ // reversed right: source_in stays so the clip's Out still shows the same media
+ assert_eq!(source_in(&s, v1), right_in);
+ assert_eq!(source_in(&s, a1), right_in);
+}
diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs
index 70cde2b69..a2e3dc874 100644
--- a/crates/ui-egui/src/panels/timeline.rs
+++ b/crates/ui-egui/src/panels/timeline.rs
@@ -1680,11 +1680,14 @@ fn preview_drag(app: &FilmcraftApp, seq: &Sequence, out: &mut HashMap<ClipId, (T
}
}
Drag::Roll { left, right, delta, .. } => {
- if let Some((_, l)) = seq.find_item(*left) {
- moved(l, filmcraft_edit::Edge::Out, *delta);
- }
- if let Some((_, r)) = seq.find_item(*right) {
- moved(r, filmcraft_edit::Edge::In, *delta);
+ let pairs = filmcraft_engine::commands::linked_roll_pairs(seq, *left, *right, app.session.state.linked_selection);
+ for (pl, pr) in pairs {
+ if let Some((_, l)) = seq.find_item(pl) {
+ moved(l, filmcraft_edit::Edge::Out, *delta);
+ }
+ if let Some((_, r)) = seq.find_item(pr) {
+ moved(r, filmcraft_edit::Edge::In, *delta);
+ }
}
}
Drag::Slide { clip, delta } => {