Skip to content

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

#333: Fade In and Fade Out presets add up on one clip

vangel766a995d7c0a07
diff --git a/crates/engine/src/presets.rs b/crates/engine/src/presets.rs
index 75f711447..bd1c14e83 100644
--- a/crates/engine/src/presets.rs
+++ b/crates/engine/src/presets.rs
@@ -365,12 +365,24 @@ pub fn instantiate(p: &EffectPreset, target: &TrackItem, size: (u32, u32)) -> Ve
out
}
-/// Put preset effects on a clip: intrinsic ones replace the clip's instance, others are inserted
-/// before the intrinsic effects.
+/// Keep `old`'s keyframes that lie outside the span `new` animates, so presets keyframing the same
+/// parameter at different times (Fade In, then Fade Out) add up instead of replacing each other.
+fn merge_keyframes(new: &mut EffectInstance, old: &EffectInstance) {
+ for (id, p) in new.params.iter_mut() {
+ let (Some(first), Some(last)) = (p.keyframes.first().map(|k| k.time), p.keyframes.last().map(|k| k.time)) else { continue };
+ let Some(o) = old.params.get(id) else { continue };
+ p.keyframes.extend(o.keyframes.iter().filter(|k| k.time < first || k.time > last).cloned());
+ p.keyframes.sort_by_key(|k| k.time);
+ }
+}
+
+/// Put preset effects on a clip: intrinsic ones replace the clip's instance (keeping its keyframes
+/// outside the span the preset animates), others are inserted before the intrinsic effects.
pub fn put_effects(it: &mut TrackItem, effects: Vec<EffectInstance>) {
- for e in effects {
+ for mut e in effects {
let intrinsic = e.def().is_some_and(|d| d.intrinsic);
if intrinsic && let Some(slot) = it.effects.iter_mut().find(|x| x.effect == e.effect) {
+ merge_keyframes(&mut e, slot);
*slot = e;
continue;
}
diff --git a/crates/engine/src/presets_tests.rs b/crates/engine/src/presets_tests.rs
index db8065b37..7a2ab0e11 100644
--- a/crates/engine/src/presets_tests.rs
+++ b/crates/engine/src/presets_tests.rs
@@ -194,6 +194,25 @@ fn builtins_apply_and_user_presets_persist() {
let _ = std::fs::remove_dir_all(&dir);
}
+/// #333: Fade In then Fade Out on one clip keeps both ramps (they keyframe the same Opacity).
+#[test]
+fn fade_in_and_fade_out_add_up_on_one_clip() {
+ let (mut s, clips) = demo();
+ let c = clips[0];
+ s.execute("presets.apply", json!({"preset": "Fade In", "clips": [c.0]})).unwrap();
+ s.execute("presets.apply", json!({"preset": "Fade Out", "clips": [c.0]})).unwrap();
+ let it = item(&s, c);
+ let op = it.effect("opacity").unwrap().param("opacity").unwrap();
+ let keys: Vec<(Tick, ParamValue)> = op.keyframes.iter().map(|k| (k.time, k.value.clone())).collect();
+ let (tin, tout) = (it.source_in, it.source_in + media_len(&it));
+ let f = ParamValue::Float;
+ assert_eq!(keys, vec![(tin, f(0.0)), (tin + secs(1.0), f(100.0)), (tout - secs(1.0), f(100.0)), (tout, f(0.0))], "both fades survive");
+ // re-applying Fade In replaces its own ramp, not the Fade Out one
+ s.execute("presets.apply", json!({"preset": "Fade In", "clips": [c.0]})).unwrap();
+ let it = item(&s, c);
+ assert_eq!(it.effect("opacity").unwrap().param("opacity").unwrap().keyframes.len(), 4);
+}
+
#[test]
fn mask_geometry_scales_to_the_target_frame_size() {
let (s, clips) = demo();