Skip to content

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

Fix Automate to Sequence dissolve duration when overlap is clamped.

Vikram Bhaskaran82609afec97f
diff --git a/crates/engine/src/project_tools.rs b/crates/engine/src/project_tools.rs
index a0e2d181a..459dd411c 100644
--- a/crates/engine/src/project_tools.rs
+++ b/crates/engine/src/project_tools.rs
@@ -1171,7 +1171,8 @@ fn automate_to_sequence(s: &mut Session, p: &Value) -> Result<Value> {
let d = rate.tick_of(rate.frame_at(r.duration + Tick(fd.0 / 2)).max(1));
plan.push((*id, TimeRange::new(r.start, d)));
}
- // destinations
+ // destinations. `cut_overlap[k]` is the overlap between clip k and k+1 (placement-clamped).
+ let mut cut_overlap: Vec<Tick> = Vec::new();
let starts: Vec<Tick> = if placement == "unnumberedMarkers" {
let mut m: Vec<Tick> = q.markers.iter().filter(|m| m.name.is_empty() && m.kind == MarkerKind::Comment).map(|m| rate.snap(m.start)).collect();
m.sort();
@@ -1189,6 +1190,7 @@ fn automate_to_sequence(s: &mut Session, p: &Value) -> Result<Value> {
if k > 0 {
let prev = plan[k - 1].1.duration;
let ov = overlap.min(Tick(prev.0 / 2)).min(Tick(r.duration.0 / 2));
+ cut_overlap.push(ov);
at -= rate.snap(ov);
}
v.push(at);
@@ -1239,20 +1241,23 @@ fn automate_to_sequence(s: &mut Session, p: &Value) -> Result<Value> {
let mut transitions = 0;
if placement == "sequentially" && (vt || at) {
let q = s.active_sequence().ok_or(EngineError::NoSequence)?;
+ let default_frames = s.project.settings.default_transition_duration_frames;
let mut edges = Vec::new();
- for (_, ids) in placed.iter().skip(1) {
+ for ((_, ids), ov) in placed.iter().skip(1).zip(cut_overlap.iter().chain(std::iter::repeat(&overlap))) {
+ let frames = if *ov > Tick::ZERO { rate.frame_at(*ov) } else { default_frames };
for c in ids {
let Some((tid, _)) = q.find_item(*c) else { continue };
let kind = q.track(tid).map(|t| t.kind);
- if (kind == Some(TrackKind::Video) && vt) || (kind == Some(TrackKind::Audio) && at) {
- edges.push((*c, kind == Some(TrackKind::Video)));
- }
+ let cmd = match kind {
+ Some(TrackKind::Video) if vt => "sequence.applyVideoTransition",
+ Some(TrackKind::Audio) if at => "sequence.applyAudioTransition",
+ _ => continue,
+ };
+ edges.push((cmd, *c, frames));
}
}
- let frames = if overlap > Tick::ZERO { rate.frame_at(overlap) } else { s.project.settings.default_transition_duration_frames };
- for (c, video) in edges {
- let id = if video { "sequence.applyVideoTransition" } else { "sequence.applyAudioTransition" };
- if s.execute(id, json!({"clip": c.0, "edge": "in", "frames": frames})).is_ok() {
+ for (cmd, c, frames) in edges {
+ if s.execute(cmd, json!({"clip": c.0, "edge": "in", "frames": frames})).is_ok() {
transitions += 1;
}
}
diff --git a/crates/engine/src/project_tools_tests.rs b/crates/engine/src/project_tools_tests.rs
index e00af7326..82291264f 100644
--- a/crates/engine/src/project_tools_tests.rs
+++ b/crates/engine/src/project_tools_tests.rs
@@ -445,6 +445,32 @@ fn automate_to_sequence_places_clips_with_overlap_and_transitions() {
assert!(!s.is_enabled("clip.automateToSequence"));
}
+/// Issue #916: overlap is clamped to half of each clip for placement, but the Cross Dissolve
+/// used the unclamped `overlapFrames` and started before the first clip.
+#[test]
+fn automate_to_sequence_clamps_transition_to_overlap() {
+ let mut s = demo();
+ s.execute("file.newSequence", json!({"name": "Auto", "fps": 23.976})).unwrap();
+ let rate = s.sequence_rate();
+ let names = ["Misty_Forest.mp4", "Ocean_Sunset.mp4"];
+ let ids: Vec<ItemId> = names.iter().map(|n| item_named(&s, n)).collect();
+ for id in &ids {
+ s.execute("project.setMarks", json!({"item": id.0, "in": 0, "out": rate.tick_of(19).0})).unwrap();
+ }
+ s.execute("project.select", json!({"items": ids.iter().map(|i| i.0).collect::<Vec<_>>()})).unwrap();
+ s.execute("clip.automateToSequence", json!({"ordering": "selection", "method": "overwrite", "overlapFrames": 30})).unwrap();
+ let q = s.active_sequence().unwrap();
+ let v = &q.video_tracks[0];
+ assert_eq!(v.items.len(), 2);
+ assert_eq!(v.items[0].start, Tick::ZERO);
+ assert_eq!(v.items[1].start, rate.tick_of(10), "overlap clamped to half of a 20-frame clip");
+ assert_eq!(v.transitions.len(), 1);
+ let t = &v.transitions[0];
+ assert_eq!(t.duration, rate.tick_of(10), "dissolve matches clamped overlap, not 30");
+ assert!(t.start >= v.items[0].start, "dissolve must not start before the first clip: {t:?}");
+ assert!(t.end() <= v.items[1].end(), "dissolve must not end after the second clip: {t:?}");
+}
+
#[test]
fn system_report_lists_the_build() {
let s = &mut Session::default();