Skip to content

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

#915: Match Frame replacement maps source frames on reversed clips

Vikram Bhaskaran82c318ed5a45
diff --git a/crates/engine/src/clip_ops.rs b/crates/engine/src/clip_ops.rs
index ec4804a3d..8a85b9eee 100644
--- a/crates/engine/src/clip_ops.rs
+++ b/crates/engine/src/clip_ops.rs
@@ -1715,8 +1715,8 @@ fn replace_with(s: &mut Session, p: &Value, how: Replace) -> Result<Value> {
}
let Some((_, it)) = q.find_item_mut(*c) else { continue };
let src_in = match how {
- // the Source monitor frame lines up with the sequence playhead
- Replace::MatchFrame => src_ph - Tick(((ph - it.start).0 as f64 * it.speed.abs()).round() as i64),
+ // the Source monitor frame lines up with the sequence playhead (reverse-aware)
+ Replace::MatchFrame => src_ph - (it.moving_source_time_at(ph) - it.source_in),
_ if keep_in => {
let kept = keep_within.map(|(first, last)| it.source_in.clamp(first, last)).unwrap_or(it.source_in);
if kept != it.source_in {
diff --git a/crates/engine/src/clip_ops_tests.rs b/crates/engine/src/clip_ops_tests.rs
index 68eb470f1..57cb8bdcb 100644
--- a/crates/engine/src/clip_ops_tests.rs
+++ b/crates/engine/src/clip_ops_tests.rs
@@ -606,6 +606,32 @@ fn replace_with_clip_from_source_match_frame_and_bin() {
assert!(s.execute("clip.replaceFromBin", json!({"item": music.0})).is_err());
}
+/// Match Frame on a reversed clip maps the Source monitor frame through reverse playback, not as
+/// if the clip still ran forward (source 50 at sequence 20 used to become source 69).
+#[test]
+fn replace_from_source_match_frame_on_a_reversed_clip() {
+ let mut s = demo();
+ let rate = s.sequence_rate();
+ s.execute("sequence.linkedSelection", json!({"on": false})).unwrap();
+ let forest = item_named(&s, "Misty_Forest.mp4");
+ s.execute("file.newSequence", json!({"name": "MatchFrameReverse"})).unwrap();
+ s.execute("timeline.place", json!({"item": forest.0, "time": 0})).unwrap();
+ let c = s.active_sequence().unwrap().video_tracks[0].items[0].clone();
+ s.execute("timeline.trim", json!({"clip": c.id.0, "edge": "out", "mode": "regular", "delta": (rate.tick_of(60) - c.duration).0})).unwrap();
+ select(&mut s, &[c.id.0]);
+ s.execute("clip.speedDuration", json!({"speed": 100, "reverse": true, "ripple": false})).unwrap();
+ let got = clip(&s, c.id.0);
+ assert_eq!(rate.frame_at(got.duration), 60);
+ assert!(got.reverse);
+ s.execute("source.open", json!({"item": forest.0})).unwrap();
+ s.execute("playhead.set", json!({"time": rate.tick_of(20).0})).unwrap();
+ s.execute("source.setPlayhead", json!({"frame": 50})).unwrap();
+ s.execute("clip.replaceFromSourceMatchFrame", json!({})).unwrap();
+ let got = clip(&s, c.id.0);
+ assert_eq!(got.source_time_at(rate.tick_of(20)), rate.tick_of(50));
+ assert_ne!(got.source_time_at(rate.tick_of(20)), rate.tick_of(69));
+}
+
/// Replace With Clip ▸ From Bin restarts the clip at the new item's In point (as Premiere does);
/// `keepSourceIn` keeps the clip's place in the media instead.
#[test]