Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

fix(edit): a copy, paste or duplicate carries every clip setting, and a paste into another project plays the right file

Jareer1d04d1472a3f
diff --git a/src/crates/concat-project/src/commands/mod.rs b/src/crates/concat-project/src/commands/mod.rs
index 733d0f81..f66afd3c 100644
--- a/src/crates/concat-project/src/commands/mod.rs
+++ b/src/crates/concat-project/src/commands/mod.rs
@@ -430,6 +430,24 @@ pub enum Command {
#[serde(default)]
ripple: bool,
},
+ /// Gives a clip everything another has but its place: its look and
+ /// sound, keys, effects, crop, mask, cutout, stabilizing, background
+ /// and enhancement - the whole clip, so a field added to [`Clip`] is
+ /// carried without a list here to remember it in. The clip keeps its
+ /// own id, lane, start, length, media, kind, in-point and speed, which
+ /// say where it is and what it plays, and its own transition in and
+ /// link to the picture its sound came off, which belong to its
+ /// neighbours. What a copy, a paste and a duplicate end with.
+ ///
+ /// The window's alone: a whole clip from outside would be a way round
+ /// every check the other commands make, so it never crosses the API.
+ #[serde(skip)]
+ MatchClip {
+ /// The clip that takes everything on.
+ clip_id: String,
+ /// The clip it is made like.
+ like: Box<Clip>,
+ },
/// Puts a key on one property at one point of a clip, replacing
/// whichever key on that property was already within a hair of it.
///
@@ -1219,6 +1237,7 @@ pub fn apply(
| Command::ConsolidateClips { .. }
| Command::RemoveClips { .. }) => clips::apply(project, mint, command),
command @ (Command::UpdateClip { .. }
+ | Command::MatchClip { .. }
| Command::SetClipSpeed { .. }
| Command::SetClipCutout { .. }
| Command::AddCutoutStroke { .. }
diff --git a/src/crates/concat-project/src/commands/properties.rs b/src/crates/concat-project/src/commands/properties.rs
index 7af0d66f..65b66b29 100644
--- a/src/crates/concat-project/src/commands/properties.rs
+++ b/src/crates/concat-project/src/commands/properties.rs
@@ -327,6 +327,37 @@ pub(super) fn apply(
})
}
+ Command::MatchClip { clip_id, like } => {
+ let timeline = project.active_mut();
+ let Some(clip) = timeline.clip_mut(&clip_id) else {
+ return Ok(Outcome::default());
+ };
+ let mut matched = *like;
+ matched.id = clip.id.clone();
+ matched.track_id = clip.track_id.clone();
+ matched.media_id = clip.media_id.clone();
+ matched.kind = clip.kind;
+ matched.start = clip.start;
+ matched.duration = clip.duration;
+ matched.source_start = clip.source_start;
+ matched.speed = clip.speed;
+ matched.speed_curve = clip.speed_curve.clone();
+ matched.detached_from = clip.detached_from.clone();
+ matched.transition_in = clip.transition_in.clone();
+ // A title's words and a shape's figure are what it is, and only
+ // a clip of the same kind can wear them.
+ if matched.text.is_some() != clip.text.is_some() {
+ matched.text = clip.text.clone();
+ }
+ if matched.shape.is_some() != clip.shape.is_some() {
+ matched.shape = clip.shape.clone();
+ }
+ let applied = assign(clip, matched);
+ Ok(Outcome {
+ created_id: None,
+ applied,
+ })
+ }
Command::SetClipTransform {
clip_id,
scale,
diff --git a/src/crates/concat-project/src/lib.rs b/src/crates/concat-project/src/lib.rs
index d7f83584..356ab923 100644
--- a/src/crates/concat-project/src/lib.rs
+++ b/src/crates/concat-project/src/lib.rs
@@ -566,6 +566,58 @@ mod tests {
);
}
+ /// A clip made like another takes on every field but its place, and
+ /// the command cannot be sent from outside.
+ #[test]
+ fn a_matched_clip_takes_everything_but_its_place() {
+ let (mut editor, media_id, source_id) = fixture();
+ let track_id = editor.project().active().tracks[0].id.clone();
+ let mut like = editor
+ .project()
+ .active()
+ .clip(&source_id)
+ .expect("clip")
+ .clone();
+ like.mask = Some(crate::model::ClipMask::of(crate::model::MaskShape::Linear));
+ like.stabilize = Some(crate::model::Stabilize::default());
+ like.opacity = 0.4;
+ like.transition_in = Some(crate::model::Transition {
+ id: "concat.dissolve".to_owned(),
+ duration: 0.5,
+ params: Default::default(),
+ });
+ like.detached_from = Some("c99".to_owned());
+ like.start = 99.0;
+ let copy_id = editor
+ .apply(Command::AddClip {
+ media_id,
+ track_id,
+ start: 20.0,
+ ripple: false,
+ })
+ .expect("adds")
+ .created_id
+ .expect("id");
+ let applied = editor
+ .apply(Command::MatchClip {
+ clip_id: copy_id.clone(),
+ like: Box::new(like.clone()),
+ })
+ .expect("matches")
+ .applied;
+ assert!(applied);
+ let copy = editor.project().active().clip(&copy_id).expect("copy");
+ assert_eq!(copy.mask, like.mask);
+ assert_eq!(copy.stabilize, like.stabilize);
+ assert_eq!(copy.opacity, 0.4);
+ assert_eq!((copy.id.as_str(), copy.start), (copy_id.as_str(), 20.0));
+ assert_eq!(copy.transition_in, None, "its neighbours' business");
+ assert_eq!(copy.detached_from, None);
+
+ let wire = serde_json::json!({ "op": "matchClip", "clipId": copy_id, "like": {} });
+ assert!(serde_json::from_value::<Command>(wire).is_err());
+ }
+
/// Editor with one media item and one clip at [0, 10) on track one.
fn fixture() -> (Editor, String, String) {
let mut editor = Editor::new();
diff --git a/src/crates/concat/src/studio.rs b/src/crates/concat/src/studio.rs
index a0f38385..e93596d9 100644
--- a/src/crates/concat/src/studio.rs
+++ b/src/crates/concat/src/studio.rs
@@ -2123,19 +2123,24 @@ pub enum Confirm {
QuitWithoutSaving { error: String },
}
-/// One clip on the clipboard: the clip as it was when copied, and the row
-/// of the lane it sat on. A lane's id belongs to its timeline, so a paste
-/// into another timeline goes by the row instead and lands the clip on
-/// the same row there (#290).
+/// One clip on the clipboard: the clip as it was when copied, the row of
+/// the lane it sat on, and the file it plays. A lane's id belongs to its
+/// timeline, so a paste into another timeline goes by the row instead and
+/// lands the clip on the same row there (#290). A media id belongs to its
+/// project, so a paste into another project goes by the file: the same
+/// id there is another file or none, and the paste laid the wrong footage
+/// down or nothing at all.
#[derive(Clone, Debug)]
pub struct Held {
pub row: usize,
pub clip: Clip,
+ pub media: Option<model::MediaItem>,
}
/// The edits that make `placed` - the whole of `source`'s file, just laid
/// on a free stretch of the lane - into a copy of `source` at `start` on
-/// `track_id`: its frames, speed, placement, keys and mix.
+/// `track_id`: its frames, speed and placement, and then everything else
+/// it has (see [`match_command`]).
///
/// The footage is trimmed at normal speed to what the source covers, then
/// retimed - the speed command keeps the footage covered and recomputes
@@ -2185,46 +2190,26 @@ fn copy_commands(source: &Clip, placed: &Clip, track_id: &str, start: f64) -> Ve
track_id: track_id.to_owned(),
}],
});
- commands.push(Command::SetClipTransform {
- clip_id: created.clone(),
- scale: Some(source.scale),
- offset_x: Some(source.offset_x),
- offset_y: Some(source.offset_y),
- rotation: Some(source.rotation),
- stretch_x: Some(source.stretch_x),
- stretch_y: Some(source.stretch_y),
- });
- for key in &source.keys {
- commands.push(Command::SetClipKey {
- clip_id: created.clone(),
- property: key.property,
- at: key.at,
- value: key.value,
- ease: key.ease,
- });
- }
- commands.push(Command::UpdateClip {
- clip_id: created.clone(),
- patch: ClipPatch {
- name: Some(format!("{} copy", source.name)),
- volume: Some(source.volume),
- fade_in: Some(source.fade_in),
- fade_out: Some(source.fade_out),
- opacity: Some(source.opacity),
- preserve_pitch: Some(source.preserve_pitch),
- muted: source.muted,
- flip_h: Some(source.flip_h),
- flip_v: Some(source.flip_v),
- blend: Some(source.blend.clone()),
- crop: Some(source.crop),
- filters: Some(source.filters.clone()),
- video_effects: Some(source.video_effects.clone()),
- ..ClipPatch::default()
- },
- });
+ commands.push(match_command(source, created));
commands
}
+/// Makes `created` like `source` in everything but its place, named as a
+/// copy. The whole clip goes, not a list of its fields: the list this
+/// replaced was written before masks, cutouts, stabilizing, backgrounds
+/// and enhancing, and a copy quietly lost every one of them.
+fn match_command(source: &Clip, created: &str) -> Command {
+ let mut like = source.clone();
+ // A title's name follows its words; anything else says it is a copy.
+ if like.text.is_none() {
+ like.name = format!("{} copy", source.name);
+ }
+ Command::MatchClip {
+ clip_id: created.to_owned(),
+ like: Box::new(like),
+ }
+}
+
/// Where each held clip lands: `(row, start)` per clip in the order held,
/// or `None` for one with no lane to land on. The group keeps its shape,
/// every clip the same distance from the earliest as when copied, from
@@ -8101,7 +8086,13 @@ impl Studio {
.filter_map(|id| {
let clip = self.clip(id)?.clone();
let row = tracks.iter().position(|track| track.id == clip.track_id)?;
- Some(Held { row, clip })
+ let media = self
+ .project()
+ .media
+ .iter()
+ .find(|item| item.id == clip.media_id)
+ .cloned();
+ Some(Held { row, clip, media })
})
.collect();
if held.is_empty() {
@@ -8157,7 +8148,10 @@ impl Studio {
let Some((row, start)) = place else {
continue;
};
- if let Some(id) = self.place_copy(&held.clip, &tracks[row], start, Some("paste")) {
+ let Some(source) = self.held_here(held) else {
+ continue;
+ };
+ if let Some(id) = self.place_copy(&source, &tracks[row], start, Some("paste")) {
created.push(id);
}
}
@@ -8170,6 +8164,48 @@ impl Studio {
}
}
+ /// The held clip as this project knows it: playing this project's
+ /// media item for its file - the same one when it was copied here, the
+ /// one with the same path when it was copied elsewhere, or the file
+ /// added to the bin when this project has not got it.
+ fn held_here(&mut self, held: &Held) -> Option<Clip> {
+ let mut clip = held.clip.clone();
+ let Some(item) = &held.media else {
+ return Some(clip);
+ };
+ let here = self
+ .project()
+ .media
+ .iter()
+ .find(|mine| mine.path == item.path)
+ .map(|mine| mine.id.clone());
+ clip.media_id = match here {
+ Some(id) => id,
+ None => self.apply_in(
+ Some("paste"),
+ Command::AddMedia {
+ item: concat_project::commands::NewMedia {
+ path: item.path.clone(),
+ name: item.name.clone(),
+ duration: item.duration,
+ kind: item.kind,
+ width: item.width,
+ height: item.height,
+ frame_rate: item.frame_rate,
+ frame_rate_fraction: item.frame_rate_fraction.clone(),
+ video_codec: item.video_codec.clone(),
+ audio_codec: item.audio_codec.clone(),
+ has_audio: item.has_audio,
+ audio_tracks: item.audio_tracks.clone(),
+ origin: item.origin,
+ color_space: item.color_space,
+ },
+ },
+ )?,
+ };
+ Some(clip)
+ }
+
/// Lays a copy of `source` on `track_id` starting at `start` - its
/// trims, mix, placement, speed, keys and effects with it - and says
/// the new clip's id. `gesture` folds the commands into one undo step
@@ -8183,31 +8219,29 @@ impl Studio {
start: f64,
gesture: Option<&str>,
) -> Option<String> {
- if source.kind == model::ClipKind::Text {
- return self.apply_in(
- gesture,
- Command::AddTextClip {
- above: false,
- track_id: Some(track_id.to_owned()),
- start,
- style: source.text.clone(),
- duration: Some(source.duration),
- offset_y: Some(source.offset_y),
- },
- );
- }
- if source.kind == model::ClipKind::Shape {
- return self.apply_in(
- gesture,
- Command::AddShapeClip {
- above: false,
- track_id: Some(track_id.to_owned()),
- start,
- style: source.shape.clone(),
- duration: Some(source.duration),
- name: source.name.clone(),
- },
- );
+ let drawn = match source.kind {
+ model::ClipKind::Text => Some(Command::AddTextClip {
+ above: false,
+ track_id: Some(track_id.to_owned()),
+ start,
+ style: source.text.clone(),
+ duration: Some(source.duration),
+ offset_y: Some(source.offset_y),
+ }),
+ model::ClipKind::Shape => Some(Command::AddShapeClip {
+ above: false,
+ track_id: Some(track_id.to_owned()),
+ start,
+ style: source.shape.clone(),
+ duration: Some(source.duration),
+ name: source.name.clone(),
+ }),
+ _ => None,
+ };
+ if let Some(add) = drawn {
+ let created = self.apply_in(gesture, add)?;
+ self.apply_in(gesture, match_command(source, &created));
+ return Some(created);
}
let lane_end = self
.timeline()
@@ -12523,6 +12557,7 @@ mod tests {
let held = |row: usize, start: f64| super::Held {
row,
clip: Clip::blank("c", "t", ClipKind::Video, "c", start, 1.0),
+ media: None,
};
let group = [held(1, 4.0), held(2, 5.5), held(0, 9.0)];
let none = |_: usize| false;