Skip to content

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

#374: a clip's fades follow its edges on a regular trim

vangel762cab09e69b3f
diff --git a/crates/edit/src/lib.rs b/crates/edit/src/lib.rs
index 2f5a8382b..1b978cf42 100644
--- a/crates/edit/src/lib.rs
+++ b/crates/edit/src/lib.rs
@@ -214,6 +214,26 @@ fn transitions_follow_cuts(before: &Sequence, after: &mut Sequence) {
}
}
+/// Keep a clip's one-sided transitions (a fade in from nothing, a fade out to nothing) on its edges
+/// after a regular trim moved them, no longer than the clip (#374). Transitions between two clips
+/// are left alone: a regular trim cannot move their cut.
+fn fades_follow_edges(track: &mut Track, clip: ClipId) {
+ let Some((start, end, len)) = track.item(clip).map(|i| (i.start, i.end(), i.duration)) else { return };
+ for tr in &mut track.transitions {
+ match (tr.from, tr.to) {
+ (None, Some(to)) if to == clip => {
+ tr.duration = tr.duration.min(len);
+ tr.start = start;
+ }
+ (Some(from), None) if from == clip => {
+ tr.duration = tr.duration.min(len);
+ tr.start = end - tr.duration;
+ }
+ _ => {}
+ }
+ }
+}
+
fn place(track: &mut Track, item: TrackItem) {
let idx = track.items.partition_point(|i| i.start <= item.start);
track.items.insert(idx, item);
@@ -630,6 +650,8 @@ pub fn trim(seq: &mut Sequence, clip: ClipId, edge: Edge, mode: TrimMode, delta:
}
}
transitions_follow_cuts(seq, &mut work);
+ } else if let Ok(track) = track_mut(&mut work, tid) {
+ fades_follow_edges(track, clip);
}
work.check().map_err(EditError::Other)?;
*seq = work;
diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rs
index ae258cb0f..f8a5cb8d3 100644
--- a/crates/edit/src/tests.rs
+++ b/crates/edit/src/tests.rs
@@ -464,6 +464,34 @@ fn transitions_stay_on_their_cuts() {
fx.seq.check().unwrap();
}
+#[test]
+fn fades_follow_a_regular_trim() {
+ let mut fx = Fx::new();
+ let v1 = fx.v(0);
+ let a = fx.put(v1, 10, 20, 10);
+ let fade = |id, start, from, to| Transition {
+ id: TransitionId(id),
+ effect: filmcraft_project::find_effect("cross_dissolve").unwrap().instance(),
+ start: f(start),
+ duration: f(6),
+ from,
+ to,
+ align: Default::default(),
+ reverse: false,
+ };
+ fx.seq.track_mut(v1).unwrap().transitions = vec![fade(1, 10, None, Some(a)), fade(2, 24, Some(a), None)];
+ let spans = |fx: &Fx| fx.seq.track(v1).unwrap().transitions.iter().map(|t| (t.start, t.duration)).collect::<Vec<_>>();
+ let mut n = fx.next;
+ trim(&mut fx.seq, a, Edge::In, TrimMode::Regular, f(2), &mut Fx::ctx(&mut n)).unwrap();
+ trim(&mut fx.seq, a, Edge::Out, TrimMode::Regular, f(4), &mut Fx::ctx(&mut n)).unwrap();
+ // the fade in starts with the clip (12), the fade out ends with it (34)
+ assert_eq!(spans(&fx), vec![(f(12), f(6)), (f(28), f(6))]);
+ // a clip trimmed shorter than its fades keeps them inside it
+ trim(&mut fx.seq, a, Edge::Out, TrimMode::Regular, -f(18), &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!(spans(&fx), vec![(f(12), f(4)), (f(12), f(4))]);
+ fx.seq.check().unwrap();
+}
+
#[test]
fn rate_stretch_and_speed() {
let mut fx = Fx::new();