FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
Merge main into #1139: frame-grid snapping moves into place_one (#746); saturating end
Brandon Thomasbbacbf275062
diff --cc crates/engine/src/commands.rsindex 26577fa10,5f46ecc6b..94a685c30--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@@ -664,6 -791,119 +791,126 @@@ pub(crate) fn ensure_audio_tracks(p: &mOk(p.sequence(seq_id).map(|q| q.audio_tracks.iter().map(|t| t.id).collect()).unwrap_or_default())}+ /// `timeline.place` for one item (`item`).+ fn place_one(s: &mut Session, p: &Value) -> Result<Value> {+ if crate::sequence_tools::wants_new_track(p) {+ return crate::sequence_tools::place_on_new_tracks(s, p);+ }+ let stream_flag = |key: &str| match p.get(key) {+ None => Ok(true),+ Some(Value::Bool(v)) => Ok(*v),+ _ => Err(bad("timeline.place", format!("`{key}` must be a boolean"))),+ };+ let (video, audio) = (stream_flag("video")?, stream_flag("audio")?);+ if !video && !audio {+ return Err(bad("timeline.place", "enable video, audio, or both"));+ }+ let item = item_p(p, "item").ok_or_else(|| bad("timeline.place", "need `item`"))?;+ let at = sequence_time_p(s, p, "", "timeline.place")?.unwrap_or(s.playhead());+ let tg = s.targeting();+ let pi = s.project.item(item).ok_or_else(|| bad("timeline.place", "no such item"))?;+ let (has_video, has_audio) = s.project.source_streams(item);+ let is_audio = |t: TrackId| s.active_sequence().is_some_and(|q| q.audio_tracks.iter().any(|x| x.id == t));+ // `track` is where the picture goes and `audioTrack` where the sound goes. A caller that+ // names an audio track as `track` asks for the sound alone, on that track.+ let (v, a) = match (track_p(s, p, "track", "timeline.place")?, track_p(s, p, "audioTrack", "timeline.place")?) {+ (Some(t), Some(_)) if is_audio(t) => {+ return Err(bad("timeline.place", "`track` names an audio track and `audioTrack` is given as well: name the sound's track once"));+ }+ (_, Some(t)) if !is_audio(t) => return Err(bad("timeline.place", "`audioTrack` names a video track")),+ (Some(t), None) if is_audio(t) && !has_audio => {+ return Err(bad("timeline.place", "`track` names an audio track and this item has no sound"));+ }+ (Some(t), None) if is_audio(t) => (None, Some(t)),+ (Some(_), None) if !has_video => {+ return Err(bad("timeline.place", "`track` names a video track and this item has no picture: name an audio track"));+ }+ (v, a) => (v.or(tg.video_dest), a.or(tg.audio_dest)),+ };+ let v = v.filter(|_| video);+ let a = a.filter(|_| audio);+ if p.get("video").is_some() && video && has_video && v.is_none() {+ return Err(bad("timeline.place", "no video destination: add or enable a video track"));+ }+ if p.get("audio").is_some() && audio && has_audio && a.is_none() {+ return Err(bad("timeline.place", "no audio destination: add or enable an audio track"));+ }+ let full = full_duration(s, pi);+ let (mi, mo) = match &pi.kind {+ ItemKind::Media(m) => (m.mark_in, m.mark_out.map(|o| o + pi.frame_rate().frame_duration())),+ ItemKind::Subclip { range, .. } => (Some(range.start), Some(range.end())),+ _ => (None, None),+ };+ let sin = p.get("sourceIn").and_then(Value::as_i64).map(Tick).or(mi).unwrap_or_default();+ let dur = p.get("duration").and_then(Value::as_i64).map(Tick).unwrap_or_else(|| mo.unwrap_or(full) - sin);++ // The clip goes on the frame grid: its start and its end (`time + duration`) each snap to the++ // nearest frame, so a sub-frame `time` never pushes the end past the one asked for and the++ // sequence never ends between frames (#1101).++ let rate = s.sequence_rate();++ let end = rate.snap_nearest(Tick(at.0.saturating_add(dur.0)));++ let at = rate.snap_nearest(at);++ let dur = end - at;+ let ids = place_item(s, item, TimeRange::new(sin, dur), at, v, a, bool_p(p, "insert").unwrap_or(false), "Place Clip", None)?;+ Ok(json!({"clips": ids.iter().map(|c| c.0).collect::<Vec<_>>()}))+ }++ /// Most items one `timeline.place` with `items` takes: a larger list is refused, not cut.+ const MAX_PLACE_ITEMS: usize = 10_000;++ /// `timeline.place` for `items`: the first at the given time, each next one where the previous one+ /// ends, all as one undo step. `track`, `audioTrack`, `insert`, `video` and `audio` apply to every+ /// item; `sourceIn` and `duration` name one item's range, so they need `item`.+ fn place_items(s: &mut Session, p: &Value, items: &Value) -> Result<Value> {+ let ids: Vec<ItemId> = items+ .as_array()+ .ok_or_else(|| bad("timeline.place", "`items` must be a list of item ids"))?+ .iter()+ .map(|v| v.as_u64().map(ItemId).ok_or_else(|| bad("timeline.place", "`items` must be a list of item ids")))+ .collect::<Result<_>>()?;+ if p.get("item").is_some() || p.get("sourceIn").is_some() || p.get("duration").is_some() {+ return Err(bad("timeline.place", "give `item` or `items`, and no `sourceIn` or `duration` with `items`"));+ }+ if ids.is_empty() || ids.len() > MAX_PLACE_ITEMS {+ return Err(bad("timeline.place", format!("`items` needs 1 to {MAX_PLACE_ITEMS} item ids")));+ }+ let mut at = sequence_time_p(s, p, "", "timeline.place")?.unwrap_or(s.playhead());+ s.edit_group("Place Clips", |s| {+ let mut placed = Vec::new();+ // "new" adds a track for the first item only; the rest follow it on that track+ let mut base = p.clone();+ for item in ids {+ let mut one = base.clone();+ if let Some(o) = one.as_object_mut() {+ o.remove("items");+ for k in ["time", "frame", "seconds"] {+ o.remove(k);+ }+ o.insert("item".into(), json!(item.0));+ o.insert("time".into(), json!(at.0));+ }+ let r = place_one(s, &one)?;+ let clips: Vec<ClipId> = r["clips"].as_array().map(|a| a.iter().filter_map(|c| c.as_u64().map(ClipId)).collect()).unwrap_or_default();+ if crate::sequence_tools::wants_new_track(&base)+ && let Some(q) = s.active_sequence()+ {+ for (key, audio) in [("track", false), ("audioTrack", true)] {+ if base.get(key).and_then(Value::as_str).is_some_and(|v| v.eq_ignore_ascii_case("new"))+ && let Some(t) =+ clips.iter().find_map(|c| q.find_item(*c).map(|(t, _)| t).filter(|t| q.audio_tracks.iter().any(|x| x.id == *t) == audio))+ {+ base[key] = json!(t.0);+ }+ }+ }+ if let Some(end) = s.active_sequence().and_then(|q| clips.iter().filter_map(|c| q.find_item(*c).map(|(_, i)| i.end())).max()) {+ at = end;+ }+ placed.push(json!(clips.iter().map(|c| c.0).collect::<Vec<_>>()));+ }+ Ok(json!({"clips": placed.iter().flat_map(|c| c.as_array().cloned().unwrap_or_default()).collect::<Vec<_>>(), "placed": placed}))+ })+ }+/// Place a project item on the timeline (drag from Project, or Insert/Overwrite from source).pub(crate) fn place_item(s: &mut Session,