The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.
Rust ffmpegfree-video-editormodel-context-protocolnon-linear-editoroffline-firstopen-source-video-editorself-hostedvideo-editingvideo-editing-automationvideo-editing-softwarevideo-editing-toolsvideo-editorvoice-cloningyoutube-automationyoutube-shorts
Keys that survive a field, a bin that imports quietly, titles that grow from their edge, audio tracks apart, four motion transitions
Jareereee30e00a461
diff --git a/README.md b/README.mdindex 9f9ed49a..72961704 100644--- a/README.md+++ b/README.md@@ -42,6 +42,8 @@ start cutting, no account, no extra downloads, no setup.Concat is currently in **Beta version (pre-release)**. **Download** the latest build from [Releases](https://github.com/jub0t/Concat/releases).+**Portable:** the Windows and Linux builds are plain archives. To keep everything on the stick or in the folder you unpacked into, make a folder named `portable` beside the `concat` executable: settings, recents and downloaded models then live there and nothing is written to the user profile.+**Platform support:**- ✅ **Windows** — testeddiff --git a/engine/Cargo.lock b/engine/Cargo.lockindex 95af1722..1ca78778 100644--- a/engine/Cargo.lock+++ b/engine/Cargo.lock@@ -1197,7 +1197,9 @@ dependencies = ["clap","concat-api","concat-core",+ "concat-effects","concat-media",+ "concat-project","concat-render","serde_json",]diff --git a/engine/crates/concat-api/src/lib.rs b/engine/crates/concat-api/src/lib.rsindex 9f447205..2f92cb4d 100644--- a/engine/crates/concat-api/src/lib.rs+++ b/engine/crates/concat-api/src/lib.rs@@ -566,6 +566,7 @@ mod tests {video_codec: None,audio_codec: None,has_audio: false,+ audio_tracks: Vec::new(),}}diff --git a/engine/crates/concat-cli/Cargo.toml b/engine/crates/concat-cli/Cargo.tomlindex 20adf6ac..02d614bf 100644--- a/engine/crates/concat-cli/Cargo.toml+++ b/engine/crates/concat-cli/Cargo.toml@@ -18,6 +18,10 @@ concat-render.workspace = true# The `api` command: the Concat API over stdin and stdout.concat-api.workspace = trueserde_json.workspace = true+# `preview` renders one effect at its defaults: the catalogue for the+# chain, the document model for the applied entry it takes.+concat-effects.workspace = true+concat-project.workspace = trueclap.workspace = true[lints]diff --git a/engine/crates/concat-cli/src/main.rs b/engine/crates/concat-cli/src/main.rsindex 7f773676..84a8d9a6 100644--- a/engine/crates/concat-cli/src/main.rs+++ b/engine/crates/concat-cli/src/main.rs@@ -7,7 +7,9 @@//! `render` command is the vertical slice: probe, build a timeline, plan every//! frame, decode, composite, encode. The `api` command is the Concat API's//! first transport: JSON requests in, JSON responses and events out, one per-//! line, so a script in any language edits and exports a project.+//! line, so a script in any language edits and exports a project. `preview`+//! is how the window's effect cards get their pictures: one still through one+//! package at its defaults.use std::error::Error;use std::io::{BufRead, Write};@@ -16,7 +18,9 @@ use std::path::PathBuf;use clap::{Parser, Subcommand};use concat_core::time::{FrameRate, Rational};use concat_core::timeline::{Clip, MediaRef, Timeline, Track, TrackKind};-use concat_media::{EncodeOptions, Encoder, FrameSink, ReaderPool};+use concat_media::{+ DecodeOptions, Decoder, EncodeOptions, Encoder, FrameSink, FrameSource, ReaderPool,+};use concat_render::{Compositor, CpuCompositor, Layer, plan_frame};#[derive(Parser)]@@ -54,6 +58,26 @@ enum Command {/// One request to run instead of reading stdin.request: Option<String>,},++ /// Run one picture through an effect at its defaults and write the+ /// result as a JPEG - how the effect cards' previews are made:+ /// `concat-cli preview assets/effect-preview-source.jpg out.jpg --effect concat.emboss`.+ Preview {+ /// A still, or the first frame of a video.+ input: PathBuf,+ /// Where to write the JPEG.+ output: PathBuf,+ /// The package's catalogue id or alias, e.g. `concat.emboss`.+ #[arg(long)]+ effect: String,+ /// Output width in pixels; the picture is scaled before the effect+ /// runs, so pixel-sized effects look as they will on the card.+ #[arg(long, default_value_t = 320)]+ width: u32,+ /// Output height in pixels.+ #[arg(long, default_value_t = 180)]+ height: u32,+ },}fn main() -> Result<(), Box<dyn Error>> {@@ -66,6 +90,13 @@ fn main() -> Result<(), Box<dyn Error>> {fade,} => render(&input, &output, frames, fade),Command::Api { request } => api(request),+ Command::Preview {+ input,+ output,+ effect,+ width,+ height,+ } => preview(&input, &output, &effect, width, height),}}@@ -110,6 +141,36 @@ fn api(single: Option<String>) -> Result<(), Box<dyn Error>> {}}+/// One frame of `input`, scaled to the card's size, through `effect` at its+/// defaults, as a JPEG at `output`.+fn preview(+ input: &PathBuf,+ output: &PathBuf,+ effect: &str,+ width: u32,+ height: u32,+) -> Result<(), Box<dyn Error>> {+ let catalogue = concat_effects::Catalogue::builtin();+ let package = catalogue+ .get(effect)+ .ok_or_else(|| format!("no package answers to {effect}"))?;+ let applied = concat_project::model::AppliedFilter::new(package.manifest.effect.id.clone());+ // The CPU chain: what a machine without a GPU renders, and what every+ // package has whether or not it also carries a shader.+ let chain = catalogue.video_chain(&[applied]);+ if chain.is_empty() {+ return Err(format!("{effect} has no FFmpeg chain to preview").into());+ }+ let mut decoder = Decoder::open(input, &DecodeOptions::default().scaled_to(width, height))?;+ let frame = decoder+ .next_frame()?+ .ok_or_else(|| format!("{} holds no picture", input.display()))?;+ let treated = concat_media::treat(&frame, &chain)?;+ std::fs::write(output, concat_media::jpeg(&treated, 3)?)?;+ println!("{effect}: {chain}\n -> {}", output.display());+ Ok(())+}+fn probe(path: &PathBuf) -> Result<(), Box<dyn Error>> {let info = concat_media::probe(path)?;@@ -132,12 +193,22 @@ fn probe(path: &PathBuf) -> Result<(), Box<dyn Error>> {None => println!(" video none"),}- match &info.audio {- Some(audio) => println!(- " audio #{} {} {} Hz, {} channels",+ // Every audio stream, the default first: a recording with its tracks+ // apart lists them all, and a clip may play any one.+ if info.audio_streams.is_empty() {+ println!(" audio none");+ }+ for audio in &info.audio_streams {+ let name = match (audio.title.is_empty(), audio.language.is_empty()) {+ (true, true) => String::new(),+ (false, true) => format!(" \"{}\"", audio.title),+ (true, false) => format!(" [{}]", audio.language),+ (false, false) => format!(" \"{}\" [{}]", audio.title, audio.language),+ };+ println!(+ " audio #{} {} {} Hz, {} channels{name}",audio.index, audio.codec, audio.sample_rate, audio.channels- ),- None => println!(" audio none"),+ );}Ok(())diff --git a/engine/crates/concat-effects/packages/concat.chromatic-aberration/effect.toml b/engine/crates/concat-effects/packages/concat.chromatic-aberration/effect.tomlnew file mode 100644index 00000000..85c1897e--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.chromatic-aberration/effect.toml@@ -0,0 +1,25 @@+[effect]+id = "concat.chromatic-aberration"+name = "Chromatic Aberration"+kind = "effect"+category = "Distort"+order = 54+intensity = "shift"+aliases = ["chromatic-aberration"]+description = "Red and blue pulled apart, the colour fringing of a cheap lens."++[[param]]+key = "shift"+label = "Shift"+type = "int"+min = 1+max = 24+default = 4+step = 1+unit = "px"++[ffmpeg]+chain = "rgbashift=rh=-{round(shift)}:bh={round(shift)}"++[wgsl]+entry = "effect.wgsl"diff --git a/engine/crates/concat-effects/packages/concat.chromatic-aberration/effect.wgsl b/engine/crates/concat-effects/packages/concat.chromatic-aberration/effect.wgslnew file mode 100644index 00000000..a4cc476b--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.chromatic-aberration/effect.wgsl@@ -0,0 +1,12 @@+struct Params { shift: f32 }++// Red taken from a little to one side and blue from the other: the two ends+// of the spectrum land apart, as a lens that cannot bring them together+// leaves them.+fn effect(uv: vec2<f32>) -> vec4<f32> {+ let d = vec2<f32>(params.shift * texel().x, 0.0);+ let c = sample(uv);+ let r = sample(uv + d).r;+ let b = sample(uv - d).b;+ return vec4<f32>(r, c.g, b, c.a);+}diff --git a/engine/crates/concat-effects/packages/concat.chromatic-aberration/fixtures.toml b/engine/crates/concat-effects/packages/concat.chromatic-aberration/fixtures.tomlnew file mode 100644index 00000000..84d08705--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.chromatic-aberration/fixtures.toml@@ -0,0 +1,13 @@+[[case]]+name = "default"+chain = "rgbashift=rh=-4:bh=4"++[[case]]+name = "minimum"+at = "min"+chain = "rgbashift=rh=-1:bh=1"++[[case]]+name = "maximum"+at = "max"+chain = "rgbashift=rh=-24:bh=24"diff --git a/engine/crates/concat-effects/packages/concat.emboss/effect.toml b/engine/crates/concat-effects/packages/concat.emboss/effect.tomlnew file mode 100644index 00000000..2f9efd4e--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.emboss/effect.toml@@ -0,0 +1,23 @@+[effect]+id = "concat.emboss"+name = "Emboss"+kind = "effect"+category = "Stylize"+order = 44+intensity = "depth"+aliases = ["emboss"]+description = "The picture pressed into relief, lit from the top left."++[[param]]+key = "depth"+label = "Depth"+min = 0.5+max = 3+default = 1+step = 0.1++[ffmpeg]+chain = "convolution=0m='-{fixed(2 * depth, 2)} -{fixed(depth, 2)} 0 -{fixed(depth, 2)} 1 {fixed(depth, 2)} 0 {fixed(depth, 2)} {fixed(2 * depth, 2)}':1m='-{fixed(2 * depth, 2)} -{fixed(depth, 2)} 0 -{fixed(depth, 2)} 1 {fixed(depth, 2)} 0 {fixed(depth, 2)} {fixed(2 * depth, 2)}':2m='-{fixed(2 * depth, 2)} -{fixed(depth, 2)} 0 -{fixed(depth, 2)} 1 {fixed(depth, 2)} 0 {fixed(depth, 2)} {fixed(2 * depth, 2)}'"++[wgsl]+entry = "effect.wgsl"diff --git a/engine/crates/concat-effects/packages/concat.emboss/effect.wgsl b/engine/crates/concat-effects/packages/concat.emboss/effect.wgslnew file mode 100644index 00000000..29b74752--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.emboss/effect.wgsl@@ -0,0 +1,18 @@+struct Params { depth: f32 }++// A relief kernel over the picture's own colour: the pixel above and to the+// left is taken away, the one below and to the right added, weighted by the+// depth, and the kernel sums to one so the picture stays under the relief.+fn effect(uv: vec2<f32>) -> vec4<f32> {+ let t = texel();+ let d = params.depth;+ let c = sample(uv);+ var rgb = c.rgb;+ rgb = rgb - 2.0 * d * sample(uv + vec2<f32>(-t.x, -t.y)).rgb;+ rgb = rgb - d * sample(uv + vec2<f32>(0.0, -t.y)).rgb;+ rgb = rgb - d * sample(uv + vec2<f32>(-t.x, 0.0)).rgb;+ rgb = rgb + d * sample(uv + vec2<f32>(t.x, 0.0)).rgb;+ rgb = rgb + d * sample(uv + vec2<f32>(0.0, t.y)).rgb;+ rgb = rgb + 2.0 * d * sample(uv + vec2<f32>(t.x, t.y)).rgb;+ return vec4<f32>(clamp(rgb, vec3<f32>(0.0), vec3<f32>(1.0)), c.a);+}diff --git a/engine/crates/concat-effects/packages/concat.emboss/fixtures.toml b/engine/crates/concat-effects/packages/concat.emboss/fixtures.tomlnew file mode 100644index 00000000..ecd0c4bd--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.emboss/fixtures.toml@@ -0,0 +1,13 @@+[[case]]+name = "default"+chain = "convolution=0m='-2.00 -1.00 0 -1.00 1 1.00 0 1.00 2.00':1m='-2.00 -1.00 0 -1.00 1 1.00 0 1.00 2.00':2m='-2.00 -1.00 0 -1.00 1 1.00 0 1.00 2.00'"++[[case]]+name = "minimum"+at = "min"+chain = "convolution=0m='-1.00 -0.50 0 -0.50 1 0.50 0 0.50 1.00':1m='-1.00 -0.50 0 -0.50 1 0.50 0 0.50 1.00':2m='-1.00 -0.50 0 -0.50 1 0.50 0 0.50 1.00'"++[[case]]+name = "maximum"+at = "max"+chain = "convolution=0m='-6.00 -3.00 0 -3.00 1 3.00 0 3.00 6.00':1m='-6.00 -3.00 0 -3.00 1 3.00 0 3.00 6.00':2m='-6.00 -3.00 0 -3.00 1 3.00 0 3.00 6.00'"diff --git a/engine/crates/concat-effects/packages/concat.hue-shift/effect.toml b/engine/crates/concat-effects/packages/concat.hue-shift/effect.tomlnew file mode 100644index 00000000..3c5c2d9c--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.hue-shift/effect.toml@@ -0,0 +1,24 @@+[effect]+id = "concat.hue-shift"+name = "Hue Shift"+kind = "effect"+category = "Basic"+order = 14+intensity = "angle"+aliases = ["hue-shift"]+description = "Every colour turned around the wheel by the same angle."++[[param]]+key = "angle"+label = "Angle"+min = -180+max = 180+default = 60+step = 1+unit = "°"++[ffmpeg]+chain = "hue=h={round(angle)}"++[wgsl]+entry = "effect.wgsl"diff --git a/engine/crates/concat-effects/packages/concat.hue-shift/effect.wgsl b/engine/crates/concat-effects/packages/concat.hue-shift/effect.wgslnew file mode 100644index 00000000..022e3688--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.hue-shift/effect.wgsl@@ -0,0 +1,23 @@+struct Params { angle: f32 }++// A turn of the hue is a plain rotation in YIQ, where the two chroma axes+// are at right angles: convert, turn I and Q by the angle, convert back.+fn effect(uv: vec2<f32>) -> vec4<f32> {+ let c = sample(uv);+ let a = radians(params.angle);+ let cs = cos(a);+ let sn = sin(a);+ let to_yiq = mat3x3<f32>(+ vec3<f32>(0.299, 0.596, 0.211),+ vec3<f32>(0.587, -0.274, -0.523),+ vec3<f32>(0.114, -0.322, 0.312),+ );+ let from_yiq = mat3x3<f32>(+ vec3<f32>(1.0, 1.0, 1.0),+ vec3<f32>(0.956, -0.272, -1.106),+ vec3<f32>(0.621, -0.647, 1.703),+ );+ let yiq = to_yiq * c.rgb;+ let turned = vec3<f32>(yiq.x, yiq.y * cs - yiq.z * sn, yiq.y * sn + yiq.z * cs);+ return vec4<f32>(clamp(from_yiq * turned, vec3<f32>(0.0), vec3<f32>(1.0)), c.a);+}diff --git a/engine/crates/concat-effects/packages/concat.hue-shift/fixtures.toml b/engine/crates/concat-effects/packages/concat.hue-shift/fixtures.tomlnew file mode 100644index 00000000..9ed7915c--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.hue-shift/fixtures.toml@@ -0,0 +1,13 @@+[[case]]+name = "default"+chain = "hue=h=60"++[[case]]+name = "minimum"+at = "min"+chain = "hue=h=-180"++[[case]]+name = "maximum"+at = "max"+chain = "hue=h=180"diff --git a/engine/crates/concat-effects/packages/concat.scanlines/effect.toml b/engine/crates/concat-effects/packages/concat.scanlines/effect.tomlnew file mode 100644index 00000000..a50e0f87--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.scanlines/effect.toml@@ -0,0 +1,34 @@+[effect]+id = "concat.scanlines"+name = "Scanlines"+kind = "effect"+category = "Stylize"+order = 47+intensity = "strength"+aliases = ["scanlines"]+description = "Dark lines across the picture every few pixels, the way a tube television drew it."++[[param]]+key = "strength"+label = "Strength"+min = 10+max = 100+default = 40+step = 1+unit = "%"++[[param]]+key = "pitch"+label = "Pitch"+type = "int"+min = 2+max = 8+default = 3+step = 1+unit = "px"++[ffmpeg]+chain = "drawgrid=x=-1:w=iw+2:h={round(pitch)}:t=1:c=black@{fixed(strength / 100, 2)}"++[wgsl]+entry = "effect.wgsl"diff --git a/engine/crates/concat-effects/packages/concat.scanlines/effect.wgsl b/engine/crates/concat-effects/packages/concat.scanlines/effect.wgslnew file mode 100644index 00000000..adbf8d58--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.scanlines/effect.wgsl@@ -0,0 +1,10 @@+struct Params { strength: f32, pitch: f32 }++// One row in every `pitch` is darkened by the strength.+fn effect(uv: vec2<f32>) -> vec4<f32> {+ let c = sample(uv);+ let pitch = max(round(params.pitch), 2.0);+ let row = floor(uv.y * frame.size.y);+ let dark = select(0.0, params.strength / 100.0, (row % pitch) < 1.0);+ return vec4<f32>(c.rgb * (1.0 - dark), c.a);+}diff --git a/engine/crates/concat-effects/packages/concat.scanlines/fixtures.toml b/engine/crates/concat-effects/packages/concat.scanlines/fixtures.tomlnew file mode 100644index 00000000..f6c3bca6--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.scanlines/fixtures.toml@@ -0,0 +1,13 @@+[[case]]+name = "default"+chain = "drawgrid=x=-1:w=iw+2:h=3:t=1:[email protected]"++[[case]]+name = "minimum"+at = "min"+chain = "drawgrid=x=-1:w=iw+2:h=2:t=1:[email protected]"++[[case]]+name = "maximum"+at = "max"+chain = "drawgrid=x=-1:w=iw+2:h=8:t=1:[email protected]"diff --git a/engine/crates/concat-effects/packages/concat.sketch/effect.toml b/engine/crates/concat-effects/packages/concat.sketch/effect.tomlnew file mode 100644index 00000000..5f5be5b6--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.sketch/effect.toml@@ -0,0 +1,25 @@+[effect]+id = "concat.sketch"+name = "Sketch"+kind = "effect"+category = "Stylize"+order = 45+intensity = "detail"+aliases = ["sketch"]+description = "The picture's edges drawn over it in its own colours."++[[param]]+key = "detail"+label = "Detail"+min = 0+max = 100+default = 50+step = 1+unit = "%"++[ffmpeg]+let = [+ "high = 0.5 - detail / 100 * 0.4",+ "low = high * 0.4",+]+chain = "edgedetect=mode=colormix:low={fixed(low, 3)}:high={fixed(high, 3)}"diff --git a/engine/crates/concat-effects/packages/concat.sketch/fixtures.toml b/engine/crates/concat-effects/packages/concat.sketch/fixtures.tomlnew file mode 100644index 00000000..b5769f29--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.sketch/fixtures.toml@@ -0,0 +1,13 @@+[[case]]+name = "default"+chain = "edgedetect=mode=colormix:low=0.120:high=0.300"++[[case]]+name = "minimum"+at = "min"+chain = "edgedetect=mode=colormix:low=0.200:high=0.500"++[[case]]+name = "maximum"+at = "max"+chain = "edgedetect=mode=colormix:low=0.040:high=0.100"diff --git a/engine/crates/concat-effects/packages/concat.solarize/effect.toml b/engine/crates/concat-effects/packages/concat.solarize/effect.tomlnew file mode 100644index 00000000..87c3ba68--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.solarize/effect.toml@@ -0,0 +1,24 @@+[effect]+id = "concat.solarize"+name = "Solarize"+kind = "effect"+category = "Stylize"+order = 46+intensity = "threshold"+aliases = ["solarize"]+description = "Everything brighter than the threshold turns negative, as an over-exposed print does."++[[param]]+key = "threshold"+label = "Threshold"+min = 10+max = 90+default = 50+step = 1+unit = "%"++[ffmpeg]+chain = "lutrgb=r='if(gt(val,{round(threshold * 255 / 100)}),255-val,val)':g='if(gt(val,{round(threshold * 255 / 100)}),255-val,val)':b='if(gt(val,{round(threshold * 255 / 100)}),255-val,val)'"++[wgsl]+entry = "effect.wgsl"diff --git a/engine/crates/concat-effects/packages/concat.solarize/effect.wgsl b/engine/crates/concat-effects/packages/concat.solarize/effect.wgslnew file mode 100644index 00000000..4b2e59a9--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.solarize/effect.wgsl@@ -0,0 +1,9 @@+struct Params { threshold: f32 }++// Each channel above the threshold is flipped; below it is left alone.+fn effect(uv: vec2<f32>) -> vec4<f32> {+ let c = sample(uv);+ let t = vec3<f32>(params.threshold / 100.0);+ let flipped = select(c.rgb, vec3<f32>(1.0) - c.rgb, c.rgb > t);+ return vec4<f32>(flipped, c.a);+}diff --git a/engine/crates/concat-effects/packages/concat.solarize/fixtures.toml b/engine/crates/concat-effects/packages/concat.solarize/fixtures.tomlnew file mode 100644index 00000000..fcd3e82b--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.solarize/fixtures.toml@@ -0,0 +1,13 @@+[[case]]+name = "default"+chain = "lutrgb=r='if(gt(val,128),255-val,val)':g='if(gt(val,128),255-val,val)':b='if(gt(val,128),255-val,val)'"++[[case]]+name = "minimum"+at = "min"+chain = "lutrgb=r='if(gt(val,26),255-val,val)':g='if(gt(val,26),255-val,val)':b='if(gt(val,26),255-val,val)'"++[[case]]+name = "maximum"+at = "max"+chain = "lutrgb=r='if(gt(val,230),255-val,val)':g='if(gt(val,230),255-val,val)':b='if(gt(val,230),255-val,val)'"diff --git a/engine/crates/concat-effects/packages/concat.threshold/effect.toml b/engine/crates/concat-effects/packages/concat.threshold/effect.tomlnew file mode 100644index 00000000..3fc608f6--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.threshold/effect.toml@@ -0,0 +1,24 @@+[effect]+id = "concat.threshold"+name = "Threshold"+kind = "effect"+category = "Basic"+order = 15+intensity = "level"+aliases = ["threshold"]+description = "Pure black and pure white, cut at one level of brightness."++[[param]]+key = "level"+label = "Level"+min = 5+max = 95+default = 50+step = 1+unit = "%"++[ffmpeg]+chain = "hue=s=0,lutrgb=r='if(gt(val,{round(level * 255 / 100)}),255,0)':g='if(gt(val,{round(level * 255 / 100)}),255,0)':b='if(gt(val,{round(level * 255 / 100)}),255,0)'"++[wgsl]+entry = "effect.wgsl"diff --git a/engine/crates/concat-effects/packages/concat.threshold/effect.wgsl b/engine/crates/concat-effects/packages/concat.threshold/effect.wgslnew file mode 100644index 00000000..d5fa1c9a--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.threshold/effect.wgsl@@ -0,0 +1,8 @@+struct Params { level: f32 }++// Brighter than the level is white, the rest black.+fn effect(uv: vec2<f32>) -> vec4<f32> {+ let c = sample(uv);+ let on = luma(c.rgb) > params.level / 100.0;+ return vec4<f32>(vec3<f32>(select(0.0, 1.0, on)), c.a);+}diff --git a/engine/crates/concat-effects/packages/concat.threshold/fixtures.toml b/engine/crates/concat-effects/packages/concat.threshold/fixtures.tomlnew file mode 100644index 00000000..79331a09--- /dev/null+++ b/engine/crates/concat-effects/packages/concat.threshold/fixtures.toml@@ -0,0 +1,13 @@+[[case]]+name = "default"+chain = "hue=s=0,lutrgb=r='if(gt(val,128),255,0)':g='if(gt(val,128),255,0)':b='if(gt(val,128),255,0)'"++[[case]]+name = "minimum"+at = "min"+chain = "hue=s=0,lutrgb=r='if(gt(val,13),255,0)':g='if(gt(val,13),255,0)':b='if(gt(val,13),255,0)'"++[[case]]+name = "maximum"+at = "max"+chain = "hue=s=0,lutrgb=r='if(gt(val,242),255,0)':g='if(gt(val,242),255,0)':b='if(gt(val,242),255,0)'"diff --git a/engine/crates/concat-export/src/flatten.rs b/engine/crates/concat-export/src/flatten.rsindex 14b7ec21..b10b27ad 100644--- a/engine/crates/concat-export/src/flatten.rs+++ b/engine/crates/concat-export/src/flatten.rs@@ -58,6 +58,7 @@ pub fn flatten_timeline_in(if clip.kind == ModelClipKind::Layer {return Some(ExportClip {path: String::new(),+ audio_stream: None,kind: ClipKind::Layer,start: clip.start,duration: clip.duration,@@ -103,6 +104,7 @@ pub fn flatten_timeline_in(Some(ExportClip {path: media.path.clone(),+ audio_stream: clip.audio_stream,kind: match clip.kind {ModelClipKind::Video => ClipKind::Video,ModelClipKind::Audio => ClipKind::Audio,@@ -291,6 +293,7 @@ mod tests {video_codec: None,audio_codec: None,has_audio: true,+ audio_tracks: Vec::new(),},}).expect("adds media")diff --git a/engine/crates/concat-export/src/lib.rs b/engine/crates/concat-export/src/lib.rsindex 84314b7b..eaa91974 100644--- a/engine/crates/concat-export/src/lib.rs+++ b/engine/crates/concat-export/src/lib.rs@@ -37,7 +37,9 @@ use concat_effects::Catalogue;use concat_media::audio::{self, AudioClip};use concat_media::{DecodeOptions, Decoder, EncodeOptions, Encoder, FrameSink, FrameSource};use concat_project::model::{AppliedFilter, Cutout};-use concat_render::{Compositor, CpuCompositor, Layer, Placement, Treatment as GpuTreatment, plan_frame};+use concat_render::{+ Compositor, CpuCompositor, Layer, Placement, Treatment as GpuTreatment, plan_frame,+};use concat_vision::{Mapping, MaskStore};use serde::Deserialize;@@ -71,6 +73,11 @@ impl ClipKind {pub struct ExportClip {/// The media file this clip shows or plays.pub path: String,+ /// Which of the file's audio streams the clip plays, by stream index;+ /// absent is the first in file order. See the document's+ /// `Clip::audio_stream`.+ #[serde(default)]+ pub audio_stream: Option<u32>,/// Whether the clip is footage, sound or a still.pub kind: ClipKind,/// Seconds into the timeline where the clip begins.@@ -229,7 +236,8 @@ fn linear_ease() -> [f64; 4] {#[derive(Deserialize, Clone, PartialEq)]#[serde(rename_all = "camelCase")]pub struct TransitionSpec {- /// "cross-fade", "fade-black" or "fade-white". Anything else is ignored.+ /// "cross-fade", "fade-black", "fade-white", "push", "zoom", "wipe-left"+ /// or "wipe-right". Anything else renders as a cut.pub kind: String,/// Seconds the transition covers.pub duration: f64,@@ -290,13 +298,27 @@ impl Reporter<'_> {}/// Turns per-cut transition requests into things the renderer already knows-/// how to draw: overlapping clips, opacity ramps, and fade filters.+/// how to draw: overlapping clips, opacity ramps, placement keys, and fade+/// and mask filters.+///+/// Track indices are doubled first, so an incoming clip gets an odd lane of+/// its own directly above the pair it crosses over - stacking against every+/// other track is preserved, and nothing else occupies odd lanes. Cuts are+/// collected before anything mutates, so resolving one transition cannot+/// unhook the adjacency test of the next.///-/// Track indices are doubled first, so an incoming cross-fade clip gets an-/// odd lane of its own directly above the pair it dissolves over - stacking-/// against every other track is preserved, and nothing else occupies odd-/// lanes. Cuts are collected before anything mutates, so resolving one-/// transition cannot unhook the adjacency test of the next.+/// Every kind but the fades to a colour is built on one overlap: the+/// incoming clip extends backwards over the outgoing one by the transition's+/// length, on the lane above, showing the handle before its in-point. What+/// differs is how the two are blended across that overlap - a dissolve+/// ramps the incoming clip's opacity, a push slides both, a zoom scales+/// both under a dissolve, a wipe uncovers the incoming one behind a moving+/// edge. The slides and scales are keys on the clips' animations, which+/// the plan already plays for the monitor and the export alike; the wipe is+/// a mask filter, which only the export bakes (`bake_fades`), and the+/// monitor shows a dissolve in its place - the same split the fades to a+/// colour make, and for the same reason: the filter counts frames from the+/// clip's start, which the monitor's pooled seeks do not.fn resolve_transitions(clips: &mut [ExportClip], rate: FrameRate, bake_fades: bool) {for clip in clips.iter_mut() {clip.track *= 2;@@ -341,7 +363,7 @@ fn resolve_transitions(clips: &mut [ExportClip], rate: FrameRate, bake_fades: bofor cut in cuts {match cut.kind.as_str() {- "cross-fade" => {+ "cross-fade" | "push" | "zoom" | "wipe-left" | "wipe-right" => {let (a_track, a_duration) = {let a = &clips[cut.outgoing];(a.track, a.duration)@@ -365,11 +387,96 @@ fn resolve_transitions(clips: &mut [ExportClip], rate: FrameRate, bake_fades: boif b.kind != ClipKind::Image {b.source_start -= d * b.speed;}- b.video_fade_in = d;// Sound rides the picture: the pre-roll fades in rather than// arriving at full level a dissolve early.b.fade_in = b.fade_in.max(d);b.track = a_track + 1;++ // How the two blend across the overlap. A shape that cannot+ // be applied - a clip the user has already keyed on the+ // property the shape would ride - falls back to the dissolve+ // rather than half-applying, so the cut still transitions.+ let shaped = match cut.kind.as_str() {+ // The new picture slides in from the right and shoves the+ // old one out to the left, edge to edge: both ride the+ // same ease over the same seconds, which is what keeps+ // them glued.+ "push" => {+ rides(clips, cut.outgoing, cut.incoming, "offsetX")+ && ride(+ &mut clips[cut.incoming],+ "offsetX",+ 1.0,+ 0.0,+ d,+ true,+ EASE_IN_OUT,+ )+ && ride(+ &mut clips[cut.outgoing],+ "offsetX",+ 0.0,+ -1.0,+ d,+ false,+ EASE_IN_OUT,+ )+ }+ // The old picture grows as it dissolves into the new one,+ // which settles from a little large to its own size.+ "zoom" => {+ rides(clips, cut.outgoing, cut.incoming, "scale")+ && ride(+ &mut clips[cut.incoming],+ "scale",+ 1.25,+ 1.0,+ d,+ true,+ EASE_OUT,+ )+ && ride(+ &mut clips[cut.outgoing],+ "scale",+ 1.0,+ 1.4,+ d,+ false,+ EASE_IN,+ )+ && {+ clips[cut.incoming].video_fade_in = d;+ true+ }+ }+ // A straight edge sweeps across and uncovers the new+ // picture behind it. Baked only where the frame count+ // means something; see the function's docs.+ "wipe-left" | "wipe-right" if bake_fades => {+ let frames = ((d * fps).round() as i64).max(1);+ // `N` counts the decoded frames from the clip's new,+ // earlier start, so the edge is at the left at 0 and+ // off the far side by `frames`; after that the filter+ // is switched off and the picture is whole.+ let uncovered = if cut.kind == "wipe-right" {+ format!("lt(X,W*(N+1)/{frames})")+ } else {+ format!("gte(X,W*(1-(N+1)/{frames}))")+ };+ append_filter(+ &mut clips[cut.incoming].transition_chain,+ &format!(+ "format=rgba,geq=r='r(X,Y)':g='g(X,Y)':b='b(X,Y)':\+ a='alpha(X,Y)*{uncovered}':enable='lt(n,{frames})'"+ ),+ );+ true+ }+ _ => false,+ };+ if !shaped {+ clips[cut.incoming].video_fade_in = d;+ }}"fade-black" | "fade-white" if bake_fades => {// Half the duration on each side of the cut, as fade filters@@ -411,6 +518,59 @@ fn resolve_transitions(clips: &mut [ExportClip], rate: FrameRate, bake_fades: bo}}+/// The timing functions the transition shapes ride on, as `ExportKey` holds+/// them: CSS `ease-in-out`, `ease-out` and `ease-in`.+const EASE_IN_OUT: [f64; 4] = [0.42, 0.0, 0.58, 1.0];+const EASE_OUT: [f64; 4] = [0.0, 0.0, 0.58, 1.0];+const EASE_IN: [f64; 4] = [0.42, 0.0, 1.0, 1.0];++/// Whether a transition may key `property` on both sides of a cut: neither+/// clip carries keys on it already. Two rides on one property is a question+/// with no good answer, and the ones the user set are the ones they will be+/// looking at.+fn rides(clips: &[ExportClip], outgoing: usize, incoming: usize, property: &str) -> bool {+ let keyed = |clip: &ExportClip| clip.animation.iter().any(|key| key.property == property);+ !keyed(&clips[outgoing]) && !keyed(&clips[incoming])+}++/// Keys `property` on `clip` from `from` to `to` over `seconds` at its head+/// (`at_head`) or its tail, easing into the second key. Values are relative+/// to the clip's own, as animation keys are: an offset adds, a scale+/// multiplies. The first key holds before it and the second after, so the+/// clip rests at `from` until the ride and at `to` past it.+fn ride(+ clip: &mut ExportClip,+ property: &str,+ from: f64,+ to: f64,+ seconds: f64,+ at_head: bool,+ ease: [f64; 4],+) -> bool {+ if clip.duration <= 0.0 {+ return false;+ }+ let fraction = (seconds / clip.duration).clamp(0.0, 1.0);+ let (start, end) = if at_head {+ (0.0, fraction)+ } else {+ (1.0 - fraction, 1.0)+ };+ clip.animation.push(ExportKey {+ property: property.to_owned(),+ at: start,+ value: from,+ ease: linear_ease(),+ });+ clip.animation.push(ExportKey {+ property: property.to_owned(),+ at: end,+ value: to,+ ease,+ });+ true+}+/// Appends one filter to a chain, comma-separated. Effects the user stacked/// come first; a transition fades the styled picture, not the raw one.fn append_filter(chain: &mut String, filter: &str) {@@ -597,6 +757,7 @@ pub fn audio_pieces(clip: &ExportClip) -> Vec<AudioClip> {let Some(curve) = SpeedCurve::new(&clip.speed_curve) else {return vec![AudioClip {path: PathBuf::from(&clip.path),+ stream: clip.audio_stream.map(|index| index as usize),start: clip.start,duration: clip.duration,source_start: clip.source_start,@@ -634,6 +795,7 @@ pub fn audio_pieces(clip: &ExportClip) -> Vec<AudioClip> {let fade_out = (piece_end - fade_out_from.max(piece_start)).clamp(0.0, piece_duration);AudioClip {path: PathBuf::from(&clip.path),+ stream: clip.audio_stream.map(|index| index as usize),start: piece_start,duration: piece_duration,source_start,@@ -1266,7 +1428,11 @@ fn build_timeline(if !clip_passes.is_empty() {passes.insert(id, clip_passes);}- if clip.effects.iter().any(|link| link.enabled && !link.keys.is_empty()) {+ if clip+ .effects+ .iter()+ .any(|link| link.enabled && !link.keys.is_empty())+ {riding.insert(id,RidingChain {@@ -1613,8 +1779,7 @@ pub fn preview_sources_of(let frame_seconds = time.as_f64();let plan_at = plan_frame(timeline, time);- let mut sources: Vec<Source<std::sync::Arc<Frame>>> =- Vec::with_capacity(plan_at.layers.len());+ let mut sources: Vec<Source<std::sync::Arc<Frame>>> = Vec::with_capacity(plan_at.layers.len());let mut failures: Vec<String> = Vec::new();for layer in &plan_at.layers {let (decode_width, decode_height) = decode_sizes@@ -1884,6 +2049,7 @@ mod tests {fn clip(kind: &str, track: usize, start: f64, duration: f64, source_start: f64) -> ExportClip {ExportClip {path: format!("{kind}.mp4"),+ audio_stream: None,kind: match kind {"audio" => ClipKind::Audio,"image" => ClipKind::Image,@@ -2078,6 +2244,126 @@ mod tests {);}+ /// The keys a resolved transition put on a clip's property, as+ /// `(at, value)` pairs.+ fn keys_on(clip: &ExportClip, property: &str) -> Vec<(f64, f64)> {+ clip.animation+ .iter()+ .filter(|key| key.property == property)+ .map(|key| (key.at, key.value))+ .collect()+ }++ #[test]+ fn a_push_slides_both_pictures_across_the_overlap() {+ let mut clips = vec![+ clip("video", 0, 0.0, 4.0, 0.0),+ clip("video", 0, 4.0, 4.0, 2.0),+ ];+ clips[1].transition = spec("push", 1.0);+ resolve_transitions(&mut clips, FrameRate::THIRTY, true);++ // The overlap is the dissolve's: a second of pre-roll on the lane+ // above, sound fading in with it - but no picture fade.+ let b = &clips[1];+ assert_eq!((b.start, b.duration, b.source_start), (3.0, 5.0, 1.0));+ assert_eq!(b.track, 1);+ assert_eq!(b.fade_in, 1.0);+ assert_eq!(b.video_fade_in, 0.0, "a push does not dissolve");+ // The incoming picture comes in from a frame's width to the right+ // over its first second (a fifth of its new length).+ assert_eq!(keys_on(b, "offsetX"), vec![(0.0, 1.0), (0.2, 0.0)]);+ // The outgoing one leaves to the left over its last second.+ assert_eq!(+ keys_on(&clips[0], "offsetX"),+ vec![(0.75, 0.0), (1.0, -1.0)]+ );+ // Both ride the same ease, which is what keeps them edge to edge.+ assert_eq!(clips[0].animation[1].ease, clips[1].animation[1].ease);+ assert_eq!(clips[1].animation[1].ease, EASE_IN_OUT);+ }++ #[test]+ fn a_push_over_a_clip_the_user_keyed_becomes_a_dissolve() {+ let mut clips = vec![+ clip("video", 0, 0.0, 4.0, 0.0),+ clip("video", 0, 4.0, 4.0, 2.0),+ ];+ clips[0].animation.push(ExportKey {+ property: "offsetX".to_owned(),+ at: 0.5,+ value: 0.1,+ ease: linear_ease(),+ });+ clips[1].transition = spec("push", 1.0);+ resolve_transitions(&mut clips, FrameRate::THIRTY, true);+ assert_eq!(clips[1].video_fade_in, 1.0, "falls back to the dissolve");+ assert!(+ keys_on(&clips[1], "offsetX").is_empty(),+ "nothing half-applied"+ );+ assert_eq!(+ keys_on(&clips[0], "offsetX").len(),+ 1,+ "the user's key is untouched"+ );+ }++ #[test]+ fn a_zoom_scales_both_under_a_dissolve() {+ let mut clips = vec![+ clip("video", 0, 0.0, 4.0, 0.0),+ clip("video", 0, 4.0, 4.0, 2.0),+ ];+ clips[1].transition = spec("zoom", 1.0);+ resolve_transitions(&mut clips, FrameRate::THIRTY, true);+ assert_eq!(clips[1].video_fade_in, 1.0);+ assert_eq!(keys_on(&clips[1], "scale"), vec![(0.0, 1.25), (0.2, 1.0)]);+ assert_eq!(keys_on(&clips[0], "scale"), vec![(0.75, 1.0), (1.0, 1.4)]);+ }++ #[test]+ fn a_wipe_is_a_mask_that_switches_off_after_the_overlap() {+ let mut clips = vec![+ clip("video", 0, 0.0, 4.0, 0.0),+ clip("video", 0, 4.0, 4.0, 2.0),+ ];+ clips[1].transition = spec("wipe-right", 0.5);+ resolve_transitions(&mut clips, FrameRate::THIRTY, true);+ let b = &clips[1];+ assert_eq!((b.start, b.duration), (3.5, 4.5));+ assert_eq!(b.video_fade_in, 0.0, "the edge does the revealing");+ assert_eq!(+ b.transition_chain,+ "format=rgba,geq=r='r(X,Y)':g='g(X,Y)':b='b(X,Y)':\+ a='alpha(X,Y)*lt(X,W*(N+1)/15)':enable='lt(n,15)'"+ );+ assert_eq!(+ clips[0].transition_chain, "",+ "the outgoing picture is untouched"+ );++ // The other direction sweeps from the right edge.+ let mut clips = vec![+ clip("video", 0, 0.0, 4.0, 0.0),+ clip("video", 0, 4.0, 4.0, 2.0),+ ];+ clips[1].transition = spec("wipe-left", 0.5);+ resolve_transitions(&mut clips, FrameRate::THIRTY, true);+ assert!(clips[1].transition_chain.contains("gte(X,W*(1-(N+1)/15))"));++ // Unbaked - the monitor - a wipe shows as a dissolve, like a fade+ // to a colour shows as the UI's veil.+ let mut clips = vec![+ clip("video", 0, 0.0, 4.0, 0.0),+ clip("video", 0, 4.0, 4.0, 2.0),+ ];+ clips[1].transition = spec("wipe-right", 0.5);+ resolve_transitions(&mut clips, FrameRate::THIRTY, false);+ assert_eq!(clips[1].transition_chain, "");+ assert_eq!(clips[1].video_fade_in, 0.5);+ }+#[test]fn a_fade_to_black_splits_across_the_cut_as_fade_filters() {let mut clips = vec![@@ -2155,7 +2441,8 @@ mod tests {clip("video", 0, 0.0, 2.0, 0.0),clip("video", 0, 2.0, 2.0, 0.0),];- clips[1].transition = spec("wipe-left", 1.0);+ // A kind no build knows - the wipes are known now.+ clips[1].transition = spec("spiral", 1.0);resolve_transitions(&mut clips, FrameRate::THIRTY, true);assert_eq!(clips[1].start, 2.0);assert!(clips[1].video_filter_chain.is_empty());diff --git a/engine/crates/concat-host/src/dirs.rs b/engine/crates/concat-host/src/dirs.rsindex b5ff6228..3d7290a9 100644--- a/engine/crates/concat-host/src/dirs.rs+++ b/engine/crates/concat-host/src/dirs.rs@@ -7,6 +7,12 @@//! Two directories, named by the app identifier and following each//! platform's convention for configuration and data. On macOS and Windows//! they are the same folder; on Linux they follow the XDG split.+//!+//! Or one directory, chosen by the person running the app: a folder named+//! `portable` beside the executable holds both, and nothing is written+//! anywhere else on the machine. That is what makes the zip a portable+//! build - unpack it on a stick, make the folder, and the settings, recents+//! and models travel with it and leave with it.use std::path::{Path, PathBuf};@@ -24,8 +30,12 @@ pub struct AppDirs {impl AppDirs {/// The platform's directories for this app, created lazily by whoever- /// writes into them.+ /// writes into them - or the `portable` folder beside the executable,+ /// when there is one.pub fn locate() -> Result<AppDirs, String> {+ if let Some(portable) = portable_root() {+ return Ok(AppDirs::under(&portable));+ }if cfg!(target_os = "macos") {let dir = home()?.join("Library")@@ -78,6 +88,17 @@ impl AppDirs {}}+/// The `portable` folder beside the executable, when someone has made one.+/// A folder rather than a marker file, because it *is* where everything+/// then goes, and its presence is the whole of the switch: no flag, no+/// setting, nothing to remember. A phone's executable has no such+/// neighbour, and the desktop's never has one unless a person put it there.+fn portable_root() -> Option<PathBuf> {+ let exe = std::env::current_exe().ok()?;+ let folder = exe.parent()?.join("portable");+ folder.is_dir().then_some(folder)+}+fn home() -> Result<PathBuf, String> {std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE"))diff --git a/engine/crates/concat-host/src/media.rs b/engine/crates/concat-host/src/media.rsindex ce992f45..cbdfdcd5 100644--- a/engine/crates/concat-host/src/media.rs+++ b/engine/crates/concat-host/src/media.rs@@ -44,6 +44,34 @@ pub struct AudioStreamInfo {pub sample_rate: u32,/// Channel count.pub channels: u32,+ /// The name the file gives the stream, or empty.+ pub title: String,+ /// The stream's language tag, or empty.+ pub language: String,+}++impl AudioStreamInfo {+ fn from_stream(audio: concat_media::AudioStream) -> Self {+ Self {+ index: audio.index,+ codec: audio.codec,+ sample_rate: audio.sample_rate,+ channels: audio.channels,+ title: audio.title,+ language: audio.language,+ }+ }++ fn to_track(&self) -> concat_project::model::AudioTrack {+ concat_project::model::AudioTrack {+ index: self.index,+ codec: self.codec.clone(),+ channels: self.channels,+ sample_rate: self.sample_rate,+ title: self.title.clone(),+ language: self.language.clone(),+ }+ }}/// What [`probe`] hands back.@@ -58,8 +86,11 @@ pub struct MediaSummary {pub kind: concat_project::model::MediaKind,/// First video stream, if any.pub video: Option<VideoStreamInfo>,- /// First audio stream, if any.+ /// The first audio stream in file order, if any: what a clip plays+ /// unless it names another.pub audio: Option<AudioStreamInfo>,+ /// Every audio stream, in file order.+ pub audio_tracks: Vec<AudioStreamInfo>,}impl MediaSummary {@@ -83,6 +114,11 @@ impl MediaSummary {video_codec: self.video.as_ref().map(|video| video.codec.clone()),audio_codec: self.audio.as_ref().map(|audio| audio.codec.clone()),has_audio: self.audio.is_some(),+ audio_tracks: self+ .audio_tracks+ .iter()+ .map(AudioStreamInfo::to_track)+ .collect(),}}}@@ -142,12 +178,12 @@ impl From<concat_media::MediaInfo> for MediaSummary {video.frame_rate.fps().denominator()),}),- audio: info.audio.map(|audio| AudioStreamInfo {- index: audio.index,- codec: audio.codec,- sample_rate: audio.sample_rate,- channels: audio.channels,- }),+ audio: info.audio.map(AudioStreamInfo::from_stream),+ audio_tracks: info+ .audio_streams+ .into_iter()+ .map(AudioStreamInfo::from_stream)+ .collect(),}}}@@ -190,15 +226,20 @@ pub const PEAKS_BUCKETS_PER_SECOND: u32 = 200;/// Waveform peaks for one media file: engine-decoded, project-cached.///-/// The engine streams the decode into min/max buckets, so neither the file-/// nor its samples are ever resident. The result is cached in the project's-/// `cache/` folder under a key derived from the path, and served from there-/// on every later call; `project: None` (an unsaved session) just skips the-/// cache.-pub fn peaks(path: &str, project: Option<&str>) -> Result<concat_media::peaks::Peaks, String> {+/// `stream` is which of the file's audio streams, by index, or `None` for+/// the first - the same choice a clip makes. The engine streams the decode+/// into min/max buckets, so neither the file nor its samples are ever+/// resident. The result is cached in the project's `cache/` folder under a+/// key derived from the path and the stream, and served from there on every+/// later call; `project: None` (an unsaved session) just skips the cache.+pub fn peaks(+ path: &str,+ stream: Option<u32>,+ project: Option<&str>,+) -> Result<concat_media::peaks::Peaks, String> {use concat_media::peaks::Peaks;- let cached = project.and_then(|project| artwork_file(project, &peaks_key(path)).ok());+ let cached = project.and_then(|project| artwork_file(project, &peaks_key(path, stream)).ok());if let Some(file) = &cached&& let Ok(bytes) = std::fs::read(file)&& let Some(peaks) = Peaks::decode(&bytes)@@ -208,8 +249,12 @@ pub fn peaks(path: &str, project: Option<&str>) -> Result<concat_media::peaks::Preturn Ok(peaks);}- let peaks = concat_media::peaks::extract(Path::new(path), PEAKS_BUCKETS_PER_SECOND)- .map_err(describe)?;+ let peaks = concat_media::peaks::extract(+ Path::new(path),+ PEAKS_BUCKETS_PER_SECOND,+ stream.map(|index| index as usize),+ )+ .map_err(describe)?;// Best-effort, like every artwork write: a failed cache write only// means decoding again next launch.@@ -228,10 +273,13 @@ pub fn peaks(path: &str, project: Option<&str>) -> Result<concat_media::peaks::P/// same reason: these keys name files that outlive the process, and/// `DefaultHasher` is free to change between Rust releases. The bucket rate/// rides in the name so a resolution change regenerates instead of serving-/// yesterday's shape.-fn peaks_key(path: &str) -> String {+/// yesterday's shape. A named stream rides in it too; the default stream's+/// name is unchanged from before streams were named, so every cache written+/// until then is still served.+fn peaks_key(path: &str, stream: Option<u32>) -> String {+ let stream = stream.map(|index| format!("-s{index}")).unwrap_or_default();format!(- "{:016x}-b{PEAKS_BUCKETS_PER_SECOND}.peaks",+ "{:016x}{stream}-b{PEAKS_BUCKETS_PER_SECOND}.peaks",fnv1a(path.as_bytes()))}@@ -287,13 +335,29 @@ pub fn write_artwork(project: &str, key: &str, bytes: &[u8]) -> Result<(), Strinstd::fs::write(&file, bytes).map_err(|error| format!("could not write {key}: {error}"))}+/// A file with this many frames or fewer gets its filmstrip from one pass+/// through it rather than from seeks.+///+/// Seeking lands on a keyframe and, for the exact frame, walks from there;+/// on long-GOP footage that walk is hundreds of full-size decodes per tile,+/// and twenty-four tiles of it per file is what pinned every core when a+/// folder of clips came in at once (#52). Under this many frames a straight+/// read is cheaper than the seeks would be, and exact. Over it the tiles are+/// too far apart for the walk to be worth it, and each is the keyframe at+/// or before its instant - one decode per tile, near enough for a+/// thumbnail. Three thousand is a hundred seconds at thirty a second: a+/// few seconds of one core for 1080p, and the last length at which a+/// single keyframe could plausibly cover several tiles.+const STRIP_SEQUENTIAL_FRAMES: f64 = 3000.0;+/// Renders a strip of evenly spaced frames from a video as one picture.////// One image rather than N, because the timeline draws the frames as slices/// of a single texture - that is one texture upload instead of twenty-four.-/// Each frame is sought directly, so the cost is `count` seeks, not a decode-/// of the whole file. If the container reports no duration there is nothing-/// to space frames across, so this refuses rather than guessing.+/// A short file is read once through; a long one is sought tile by tile,+/// to its keyframes - see [`STRIP_SEQUENTIAL_FRAMES`]. If the container+/// reports no duration there is nothing to space frames across, so this+/// refuses rather than guessing.pub fn filmstrip(path: &str, count: u32, height: u32) -> Result<Frame, String> {let count = count.clamp(1, 60);let height = height.clamp(16, 240);@@ -305,21 +369,63 @@ pub fn filmstrip(path: &str, count: u32, height: u32) -> Result<Frame, String> {.map(|duration| duration.as_f64()).filter(|seconds| *seconds > 0.0).ok_or_else(|| format!("{path} reports no duration"))?;+ let fps = video.frame_rate.fps().as_f64();// Aspect-correct and even, which is what the scaler is happiest with.let width = ((f64::from(height) * f64::from(video.width) / f64::from(video.height)).round()as u32).max(2)& !1;- let mut decoder = Decoder::open(path, &DecodeOptions::default().scaled_to(width, height))- .map_err(describe)?;+ // Sample the middle of each slice, so the first frame is not always the+ // file's own first (often black) frame.+ let instant = |index: u32| duration * (f64::from(index) + 0.5) / f64::from(count);let mut strip = Frame::black(width * count, height);let mut last: Option<Frame> = None;++ if duration * fps <= STRIP_SEQUENTIAL_FRAMES {+ let mut decoder = Decoder::open(path, &DecodeOptions::default().scaled_to(width, height))+ .map_err(describe)?;+ let mut index = 0;+ let mut produced = 0u64;+ while index < count {+ let Ok(Some(frame)) = decoder.next_frame() else {+ break;+ };+ // Where this frame is: its own stamp, or its count when the+ // container stamps nothing.+ let at = decoder+ .position()+ .map(|position| position.as_f64())+ .unwrap_or(produced as f64 / fps.max(1.0));+ produced += 1;+ // Every tile whose instant this frame has reached takes it; a+ // file with fewer frames than tiles hands one frame to several.+ while index < count && at >= instant(index) {+ strip.blit(&frame, index * width, 0);+ index += 1;+ }+ last = Some(frame);+ }+ // Past the last frame - a duration the container overstated - the+ // last picture fills what is left rather than leaving holes.+ if let Some(frame) = &last {+ while index < count {+ strip.blit(frame, index * width, 0);+ index += 1;+ }+ }+ return Ok(strip);+ }++ let mut decoder = Decoder::open(+ path,+ &DecodeOptions::default()+ .scaled_to(width, height)+ .nearest_keyframes(),+ )+ .map_err(describe)?;for index in 0..count {- // Sample the middle of each slice, so the first frame is not always- // the file's own first (often black) frame.- let at = duration * (f64::from(index) + 0.5) / f64::from(count);- let time = Rational::approximate(at).unwrap_or(Rational::ZERO);+ let time = Rational::approximate(instant(index)).unwrap_or(Rational::ZERO);let frame = match decoder.seek(time).and_then(|()| decoder.next_frame()) {Ok(Some(frame)) => Some(frame),// Past the last frame, or a stretch that will not decode: repeat@@ -501,9 +607,13 @@ mod tests {// Pinned: a changed hash would orphan every project's caches.assert_eq!(fnv1a(b""), 0xcbf2_9ce4_8422_2325);assert_eq!(- peaks_key("/a.mp4"),+ peaks_key("/a.mp4", None),format!("{:016x}-b200.peaks", fnv1a(b"/a.mp4")));+ assert_eq!(+ peaks_key("/a.mp4", Some(2)),+ format!("{:016x}-s2-b200.peaks", fnv1a(b"/a.mp4"))+ );}#[test]@@ -525,12 +635,15 @@ mod tests {let strip = filmstrip(&path.to_string_lossy(), 4, 32).expect("strips");assert_eq!((strip.width(), strip.height()), (4 * 64, 32));- // Later slices come from later in the file: the red ramps up.- let first = strip.pixel(32, 16).expect("in bounds")[0];- let last = strip.pixel(3 * 64 + 32, 16).expect("in bounds")[0];+ // Later slices come from later in the file: the red ramps up, tile+ // by tile - a two-second file is read straight through, so every+ // tile is its own frame even though the file has one keyframe.+ let reds: Vec<u8> = (0..4)+ .map(|tile| strip.pixel(tile * 64 + 32, 16).expect("in bounds")[0])+ .collect();assert!(- last > first,- "strip is not in time order: {first} then {last}"+ reds.windows(2).all(|pair| pair[1] > pair[0]),+ "strip is not in time order: {reds:?}");let poster = still_at(&path.to_string_lossy(), 1.0, 32).expect("still");diff --git a/engine/crates/concat-host/src/playback.rs b/engine/crates/concat-host/src/playback.rsindex 739bc0f8..43327563 100644--- a/engine/crates/concat-host/src/playback.rs+++ b/engine/crates/concat-host/src/playback.rs@@ -95,6 +95,9 @@ fn linear_ease() -> [f64; 4] {pub struct ClipSpec {/// The media file.pub path: String,+ /// Which of the file's audio streams, by index; absent is the first.+ #[serde(default)]+ pub audio_stream: Option<u32>,/// Timeline seconds.pub start: f64,/// Timeline seconds the clip occupies.@@ -687,6 +690,12 @@ fn decode_key(spec: &ClipSpec) -> String {eat(&spec.speed.to_bits().to_le_bytes());eat(&[u8::from(spec.preserve_pitch)]);eat(spec.chain.as_bytes());+ // Only when a stream is named, so every key from before streams were+ // named still finds its file.+ if let Some(stream) = spec.audio_stream {+ eat(b"stream");+ eat(&stream.to_le_bytes());+ }format!("{hash:016x}")}@@ -731,6 +740,7 @@ fn decode(spec: &ClipSpec, project: &Path, key: &str) -> Result<Pcm, String> {rate: PCM_RATE,channels: 2,format: SampleFormat::I16,+ stream: spec.audio_stream.map(|index| index as usize),},).map_err(|error| error.to_string())?;diff --git a/engine/crates/concat-host/src/templates.rs b/engine/crates/concat-host/src/templates.rsindex d4ff6a70..d1ad8d30 100644--- a/engine/crates/concat-host/src/templates.rs+++ b/engine/crates/concat-host/src/templates.rs@@ -443,6 +443,7 @@ mod tests {video_codec: Some("h264".to_owned()),audio_codec: None,has_audio: false,+ audio_tracks: Vec::new(),},}}diff --git a/engine/crates/concat-host/src/titles.rs b/engine/crates/concat-host/src/titles.rsindex e809cdc1..4b3cf426 100644--- a/engine/crates/concat-host/src/titles.rs+++ b/engine/crates/concat-host/src/titles.rs@@ -36,12 +36,18 @@ pub struct TitleClip {pub clip: ExportClip,/// The painted block's size in frame pixels, for an outline on a monitor.pub block: (u32, u32),+ /// Where that block's centre is, as an offset in frame pixels from the+ /// clip's own centre: zero for a centred title, half a block to the+ /// right for a left-aligned one, whose block starts at the clip's+ /// position. See `concat_text::Align`.+ pub offset: (i32, i32),}/// What one render left behind.#[derive(Clone, Copy, Debug)]struct Art {block: (u32, u32),+ offset: (i32, i32),}/// The title painter and its cache.@@ -86,7 +92,7 @@ impl Titles {};let track = &timeline.tracks[index];let text = clip.text.clone().unwrap_or_default();- let (path, block) = match self.painted(project, &text, width, height) {+ let (path, art) = match self.painted(project, &text, width, height) {Ok(art) => art,Err(error) => {eprintln!("concat: title {}: {error}", clip.id);@@ -97,6 +103,7 @@ impl Titles {clip_id: clip.id.clone(),clip: ExportClip {path: path.to_string_lossy().into_owned(),+ audio_stream: None,kind: ClipKind::Image,start: clip.start,duration: clip.duration,@@ -138,7 +145,8 @@ impl Titles {mask_dir: String::new(),highlighted: false,},- block,+ block: art.block,+ offset: art.offset,});}out@@ -152,7 +160,7 @@ impl Titles {style: &TextStyle,width: u32,height: u32,- ) -> Result<(PathBuf, (u32, u32)), String> {+ ) -> Result<(PathBuf, Art), String> {let key = key_of(project, style, width, height);let png = self.dir.join(format!("{key:016x}.png"));let side = self.dir.join(format!("{key:016x}.json"));@@ -164,15 +172,15 @@ impl Titles {.get(&key)&& png.is_file(){- return Ok((png, art.block));+ return Ok((png, *art));}// On disk from an earlier session: the sidecar says how big the- // block was, which the PNG alone cannot.+ // block was and where, which the PNG alone cannot.if png.is_file()- && let Some(block) = read_block(&side)+ && let Some(art) = read_block(&side){- self.remember(key, block);- return Ok((png, block));+ self.remember(key, art);+ return Ok((png, art));}let title = title_style(style);@@ -190,17 +198,26 @@ impl Titles {};std::fs::create_dir_all(&self.dir).map_err(|error| error.to_string())?;std::fs::write(&png, &rendered.png).map_err(|error| error.to_string())?;- let block = (rendered.block_width, rendered.block_height);- let _ = std::fs::write(&side, format!("{{\"w\":{},\"h\":{}}}", block.0, block.1));- self.remember(key, block);- Ok((png, block))+ let art = Art {+ block: (rendered.block_width, rendered.block_height),+ offset: (rendered.block_dx, rendered.block_dy),+ };+ let _ = std::fs::write(+ &side,+ format!(+ "{{\"w\":{},\"h\":{},\"x\":{},\"y\":{}}}",+ art.block.0, art.block.1, art.offset.0, art.offset.1+ ),+ );+ self.remember(key, art);+ Ok((png, art))}- fn remember(&self, key: u64, block: (u32, u32)) {+ fn remember(&self, key: u64, art: Art) {self.memo.lock().unwrap_or_else(|e| e.into_inner())- .insert(key, Art { block });+ .insert(key, art);}}@@ -225,8 +242,9 @@ fn key_of(project: &Project, style: &TextStyle, width: u32, height: u32) -> u64bytes.push(0);}// Bumped when the painter's output changes for the same input, so stale- // files are not mistaken for current ones.- bytes.extend_from_slice(&2u32.to_le_bytes());+ // files are not mistaken for current ones. 3: left- and right-aligned+ // blocks moved to their anchors.+ bytes.extend_from_slice(&3u32.to_le_bytes());crate::media::fnv1a(&bytes)}@@ -260,13 +278,17 @@ fn sweep(dir: &Path, keep: usize) {}}-fn read_block(side: &Path) -> Option<(u32, u32)> {+fn read_block(side: &Path) -> Option<Art> {let text = std::fs::read_to_string(side).ok()?;let value: serde_json::Value = serde_json::from_str(&text).ok()?;- Some((- value.get("w")?.as_u64()? as u32,- value.get("h")?.as_u64()? as u32,- ))+ let offset = |name: &str| value.get(name).and_then(|v| v.as_i64()).unwrap_or(0) as i32;+ Some(Art {+ block: (+ value.get("w")?.as_u64()? as u32,+ value.get("h")?.as_u64()? as u32,+ ),+ offset: (offset("x"), offset("y")),+ })}/// The document's style, field for field, in the painter's terms.@@ -335,6 +357,11 @@ mod tests {assert_eq!(title.clip.offset_y, 0.3);assert_eq!(title.clip.media_width, Some(640));assert!(title.block.0 > 0 && title.block.1 > 0);+ assert_eq!(+ title.offset,+ (0, 0),+ "a centred title's block is on the clip"+ );let painted = Path::new(&title.clip.path);assert!(painted.is_file(), "the PNG is on disk");let stamp = std::fs::metadata(painted).and_then(|m| m.modified()).ok();@@ -350,6 +377,29 @@ mod tests {// A different frame is a different picture.let wide = titles.clips(editor.project(), 1280, 720);assert_ne!(wide[0].clip.path, title.clip.path);++ // Aligned left, the block starts at the clip's position and its+ // centre is reported half a block to the right - and a second+ // painter, reading the sidecar cold, says the same.+ let style = concat_project::model::TextStyle {+ align: concat_project::model::TextAlign::Left,+ ..Default::default()+ };+ editor+ .apply(Command::UpdateClip {+ clip_id: id.clone(),+ patch: concat_project::commands::ClipPatch {+ text: Some(Some(style)),+ ..Default::default()+ },+ })+ .expect("aligns");+ let left = titles.clips(editor.project(), 640, 360);+ assert!(left[0].offset.0 > 0);+ assert!((left[0].offset.0 as u32).abs_diff(left[0].block.0 / 2) <= 1);+ let cold = Titles::new(&dirs).clips(editor.project(), 640, 360);+ assert_eq!(cold[0].offset, left[0].offset);+ assert_eq!(cold[0].block, left[0].block);let _ = std::fs::remove_dir_all(dirs.data.parent().unwrap());}}diff --git a/engine/crates/concat-media/src/audio.rs b/engine/crates/concat-media/src/audio.rsindex c63777e4..87ac7306 100644--- a/engine/crates/concat-media/src/audio.rs+++ b/engine/crates/concat-media/src/audio.rs@@ -43,6 +43,9 @@ pub fn clamp_speed(speed: f64) -> f64 {pub struct AudioClip {/// The media file.pub path: std::path::PathBuf,+ /// Which of the file's audio streams, by stream index; `None` is the+ /// first in file order. See `probe::audio_stream_index`.+ pub stream: Option<usize>,/// Where the clip sits on the timeline, in seconds.pub start: f64,/// How long it runs on the timeline, in seconds.@@ -376,13 +379,13 @@ pub fn mix_to_file(clips: &[AudioClip], duration: f64, destination: &Path) -> Relet path = clip.path.as_path();let mut input =ffmpeg::format::input(path).map_err(|error| ffi::fail("open", path, error))?;- let stream = input- .streams()- .best(ffmpeg::media::Type::Audio)- .ok_or_else(|| Error::NoAudioStream {- path: path.to_path_buf(),+ let stream_index =+ crate::probe::audio_stream_index(&input, clip.stream).ok_or_else(|| {+ Error::NoAudioStream {+ path: path.to_path_buf(),+ }})?;- let stream_index = stream.index();+ let stream = input.stream(stream_index).expect("just found");let time_base = stream.time_base();let decoder = ffmpeg::codec::Context::from_parameters(stream.parameters()).and_then(|context| context.decoder().audio())@@ -714,6 +717,7 @@ mod tests {fn clip(path: &str) -> AudioClip {AudioClip {path: path.into(),+ stream: None,start: 0.0,duration: 2.0,source_start: 0.0,diff --git a/engine/crates/concat-media/src/decode.rs b/engine/crates/concat-media/src/decode.rsindex 3cd6700c..d1583a4c 100644--- a/engine/crates/concat-media/src/decode.rs+++ b/engine/crates/concat-media/src/decode.rs@@ -89,6 +89,18 @@ pub struct DecodeOptions {/// A filter chain applied *before* the fit scale, in the source's own/// pixels: where a crop lives, since a crop changes what the fit is of.pub pre_chain: Option<String>,+ /// Decode keyframes only, and let a seek land on the keyframe at or+ /// before its target rather than walking forward to the exact frame.+ ///+ /// For a thumbnail or a filmstrip tile, the exact frame is not worth+ /// what it costs: reaching it means decoding every frame from the+ /// keyframe before it at full resolution, up to a whole group of+ /// pictures - hundreds of frames on a screen recording - to keep one.+ /// With this set the codec skips everything that is not a keyframe and a+ /// seek returns the first picture it lands on, so a tile costs one+ /// decoded frame. Never for anything a person will watch or export: the+ /// frame is near the instant, not at it.+ pub keyframes_only: bool,}impl DecodeOptions {@@ -135,6 +147,12 @@ impl DecodeOptions {self.filter_chain = (!chain.is_empty()).then_some(chain);self}++ /// Decodes keyframes only. See [`DecodeOptions::keyframes_only`].+ pub fn nearest_keyframes(mut self) -> Self {+ self.keyframes_only = true;+ self+ }}/// The filtergraph between the decoder and the caller, as one string.@@ -221,10 +239,13 @@ impl Decoder {let context = ffmpeg::codec::Context::from_parameters(parameters).map_err(|error| ffi::fail("codec parameters", path, error))?;- let decoder = context+ let mut decoder = context.decoder().video().map_err(|error| ffi::fail("open decoder", path, error))?;+ if options.keyframes_only {+ decoder.skip_frame(ffmpeg::Discard::NonKey);+ }let (width, height) = match options.size {Some(size) => size,@@ -277,7 +298,10 @@ impl Decoder {.seek(target, ..=target).map_err(|error| ffi::fail("seek", &self.path, error))?;self.decoder.flush();- self.discard_before = Some(to);+ // Keyframes only: the picture the container landed on is the one+ // wanted, and discarding up to the target would throw it away and+ // wait for the *next* keyframe, a whole group of pictures late.+ self.discard_before = (!self.options.keyframes_only).then_some(to);self.origin = to;self.tick = 0;self.current = None;diff --git a/engine/crates/concat-media/src/peaks.rs b/engine/crates/concat-media/src/peaks.rsindex 738e5b8f..401172fc 100644--- a/engine/crates/concat-media/src/peaks.rs+++ b/engine/crates/concat-media/src/peaks.rs@@ -82,15 +82,17 @@ impl Peaks {/// Decodes one file's audio and reduces it to peaks.///-/// The decode is mono 16-bit at [`PEAK_RATE`]. A file with no audio stream-/// is an error that says so.-pub fn extract(path: &Path, buckets_per_second: u32) -> Result<Peaks> {+/// The decode is mono 16-bit at [`PEAK_RATE`], of the audio stream `stream`+/// names or the file's first. A file with no audio stream is an error that+/// says so.+pub fn extract(path: &Path, buckets_per_second: u32, stream: Option<usize>) -> Result<Peaks> {let mut decoder = AudioDecoder::open(path,&AudioOptions {rate: PEAK_RATE,channels: 1,format: SampleFormat::I16,+ stream,..AudioOptions::default()},)?;@@ -196,7 +198,7 @@ mod tests {#[test]fn a_file_with_no_audio_is_an_error() {- assert!(extract(Path::new("does-not-exist.mp3"), 200).is_err());+ assert!(extract(Path::new("does-not-exist.mp3"), 200, None).is_err());}#[test]diff --git a/engine/crates/concat-media/src/probe.rs b/engine/crates/concat-media/src/probe.rsindex f2b7f1d2..6c44423c 100644--- a/engine/crates/concat-media/src/probe.rs+++ b/engine/crates/concat-media/src/probe.rs@@ -30,7 +30,7 @@ pub struct VideoStream {/// What an audio stream looks like.#[derive(Clone, Debug)]pub struct AudioStream {- /// Stream index within the file.+ /// Stream index within the file - what a clip names to play this one.pub index: u32,/// Codec short name, for example `aac`.pub codec: String,@@ -38,13 +38,22 @@ pub struct AudioStream {pub sample_rate: u32,/// Channel count.pub channels: u32,+ /// What the file calls the stream, when it says: the `title` a recorder+ /// writes per track ("Desktop Audio", "Mic/Aux"). Empty when it does not.+ pub title: String,+ /// The stream's language tag, for example `eng`; empty when unstated.+ pub language: String,}/// A summary of one media file.///-/// Concat only cares about the first video and first audio stream. Multi-stream-/// files exist, but nothing in the editor addresses them yet, and inventing an-/// API for a case we do not handle would be worse than not having one.+/// One video stream is all the editor addresses. Audio streams are listed in+/// full: a screen recording often carries the desktop's sound and the+/// microphone as two streams, and which one a clip plays is the clip's to+/// say. `audio` is the first of them in file order - the one a clip plays+/// unless it names another - and not libavformat's "best", which weighs+/// bitrate and frame counts and on such a recording tends to land on the+/// second track, so the microphone played and the desktop was never heard.#[derive(Clone, Debug)]pub struct MediaInfo {/// The file this describes.@@ -53,8 +62,10 @@ pub struct MediaInfo {pub duration: Option<Rational>,/// First video stream, if any.pub video: Option<VideoStream>,- /// First audio stream, if any.+ /// The first audio stream in file order, if any: the default.pub audio: Option<AudioStream>,+ /// Every audio stream, in file order. Empty for a file without sound.+ pub audio_streams: Vec<AudioStream>,}impl MediaInfo {@@ -80,27 +91,66 @@ pub fn probe(path: impl AsRef<Path>) -> Result<MediaInfo> {Some(stream) => Some(video_stream(&stream, path)?),None => None,};- let audio = input+ let audio_streams: Vec<AudioStream> = input.streams()- .best(ffmpeg::media::Type::Audio)+ .filter(is_audio).map(|stream| {let parameters = stream.parameters();+ let tag = |name: &str| {+ stream+ .metadata()+ .get(name)+ .map(str::trim)+ .filter(|value| !value.is_empty())+ .map(str::to_owned)+ .unwrap_or_default()+ };AudioStream {index: stream.index() as u32,codec: parameters.id().name().to_owned(),sample_rate: parameters.sample_rate(),channels: parameters.ch_layout().channels(),+ title: tag("title"),+ language: tag("language"),}- });+ })+ .collect();+ let audio = audio_streams.first().cloned();Ok(MediaInfo {path: path.to_path_buf(),duration,video,audio,+ audio_streams,})}+fn is_audio(stream: &ffmpeg::format::stream::Stream<'_>) -> bool {+ stream.parameters().medium() == ffmpeg::media::Type::Audio+}++/// The audio stream a caller means: `wanted`, when it names an audio stream+/// the file has, else the first audio stream in file order. `None` for a+/// file with no sound at all.+///+/// Every reader of samples picks its stream here - the waveform, playback,+/// the mix, the transcriber - so a clip that names a track hears the same+/// one everywhere, and a clip that names none hears the same default the+/// probe reported as `audio`. A named stream the file no longer has (the+/// file was replaced) degrades to that default rather than to silence.+pub(crate) fn audio_stream_index(+ input: &ffmpeg::format::context::Input,+ wanted: Option<usize>,+) -> Option<usize> {+ if let Some(index) = wanted+ && input.stream(index).as_ref().is_some_and(is_audio)+ {+ return Some(index);+ }+ input.streams().find(is_audio).map(|stream| stream.index())+}+fn video_stream(stream: &ffmpeg::format::stream::Stream<'_>, path: &Path) -> Result<VideoStream> {let parameters = stream.parameters();let (width, height) = (parameters.width(), parameters.height());diff --git a/engine/crates/concat-media/src/samples.rs b/engine/crates/concat-media/src/samples.rsindex ad04977b..b83fa4d9 100644--- a/engine/crates/concat-media/src/samples.rs+++ b/engine/crates/concat-media/src/samples.rs@@ -63,6 +63,10 @@ pub struct AudioOptions {pub channels: u16,/// The sample format to come back in.pub format: SampleFormat,+ /// Which audio stream of the file, by its index in the file. `None` is+ /// the first audio stream in file order, which is also what a file with+ /// one stream has; see `probe::audio_stream_index`.+ pub stream: Option<usize>,}impl Default for AudioOptions {@@ -74,6 +78,7 @@ impl Default for AudioOptions {rate: 48_000,channels: 2,format: SampleFormat::I16,+ stream: None,}}}@@ -132,7 +137,7 @@ pub struct AudioDecoder {}impl AudioDecoder {- /// Opens `path`'s first audio stream.+ /// Opens the audio stream `options.stream` names, or `path`'s first.pub fn open(path: impl AsRef<Path>, options: &AudioOptions) -> Result<Self> {ffi::init();for chain in &options.filters {@@ -151,13 +156,13 @@ impl AudioDecoder {let path = path.as_ref();let mut input =ffmpeg::format::input(path).map_err(|error| ffi::fail("open", path, error))?;- let stream = input- .streams()- .best(ffmpeg::media::Type::Audio)- .ok_or_else(|| Error::NoAudioStream {- path: path.to_path_buf(),+ let stream_index =+ crate::probe::audio_stream_index(&input, options.stream).ok_or_else(|| {+ Error::NoAudioStream {+ path: path.to_path_buf(),+ }})?;- let stream_index = stream.index();+ let stream = input.stream(stream_index).expect("just found");let time_base = stream.time_base();let context = ffmpeg::codec::Context::from_parameters(stream.parameters()).map_err(|error| ffi::fail("codec parameters", path, error))?;@@ -391,6 +396,7 @@ mod tests {rate: 16_000,channels: 1,format: SampleFormat::F32,+ stream: None,};assert_eq!(audio_filter(&options),diff --git a/engine/crates/concat-project/src/commands.rs b/engine/crates/concat-project/src/commands.rsindex 58a6c7e4..44cf0ff3 100644--- a/engine/crates/concat-project/src/commands.rs+++ b/engine/crates/concat-project/src/commands.rs@@ -12,9 +12,9 @@ use std::collections::HashSet;use serde::{Deserialize, Serialize};use crate::model::{- AnimationSlot, AppliedFilter, Clip, ClipAnimation, ClipKind, Crop, CustomFont, Cutout,- CutoutMode, KeyEase, KeyProperty, MediaItem, MediaKind, Project, SpeedPoint, Stroke, TextStyle,- Timeline, Track, Transition, VideoSettings,+ AnimationSlot, AppliedFilter, AudioTrack, Clip, ClipAnimation, ClipKind, Crop, CustomFont,+ Cutout, CutoutMode, KeyEase, KeyProperty, MediaItem, MediaKind, Project, SpeedPoint, Stroke,+ TextStyle, Timeline, Track, Transition, VideoSettings,};/// Fallback length for media whose container reports no duration.@@ -133,6 +133,15 @@ pub struct ClipPatch {skip_serializing_if = "Option::is_none")]pub text: Option<Option<TextStyle>>,+ /// Which of the media's audio streams the clip plays, by stream index:+ /// absent leaves it alone, null goes back to the file's first, a value+ /// names one. See `Clip::audio_stream`.+ #[serde(+ default,+ with = "double_option",+ skip_serializing_if = "Option::is_none"+ )]+ pub audio_stream: Option<Option<u32>>,}/// `Option<Option<T>>` over JSON: absent → None, null → Some(None).@@ -184,6 +193,10 @@ pub struct NewMedia {pub audio_codec: Option<String>,/// Whether the file carries an audio stream.pub has_audio: bool,+ /// Every audio stream, in file order; see `MediaItem::audio_tracks`.+ /// Defaulted so a caller from before the list can still add media.+ #[serde(default)]+ pub audio_tracks: Vec<AudioTrack>,}/// Every edit, as the window sends it: a tagged `op` plus camelCase@@ -741,6 +754,7 @@ fn default_clip(id: String, track_id: String, media: &MediaItem, start: f64) ->cutout: None,filters: Vec::new(),video_effects: Vec::new(),+ audio_stream: None,muted: None,detached_from: None,transition_in: None,@@ -873,6 +887,7 @@ pub fn apply(video_codec: item.video_codec,audio_codec: item.audio_codec,has_audio: item.has_audio,+ audio_tracks: item.audio_tracks,placeholder: false,});Ok(Outcome {@@ -919,6 +934,7 @@ pub fn apply(media.video_codec = item.video_codec;media.audio_codec = item.audio_codec;media.has_audio = item.has_audio;+ media.audio_tracks = item.audio_tracks;media.placeholder = false;let kind = match item.kind {MediaKind::Video => ClipKind::Video,@@ -938,6 +954,9 @@ pub fn apply(clip.source_start = 0.0;clip.kind = kind;clip.name = item.name.clone();+ // A stream index named against the old file means+ // nothing against the new one.+ clip.audio_stream = None;}}}@@ -1086,6 +1105,7 @@ pub fn apply(cutout: None,filters: Vec::new(),video_effects: Vec::new(),+ audio_stream: None,muted: None,detached_from: None,transition_in: None,@@ -1154,6 +1174,7 @@ pub fn apply(cutout: None,filters: Vec::new(),video_effects: vec![AppliedFilter::new(effect_id)],+ audio_stream: None,muted: None,detached_from: None,transition_in: None,@@ -1310,8 +1331,7 @@ pub fn apply(let Some(item) = still else {return Ok(Outcome::default());};- if let Some(existing) = project.media.iter().find(|media| media.path == item.path)- {+ if let Some(existing) = project.media.iter().find(|media| media.path == item.path) {existing.id.clone()} else {let id = mint.next("m");@@ -1328,6 +1348,7 @@ pub fn apply(video_codec: item.video_codec,audio_codec: None,has_audio: false,+ audio_tracks: Vec::new(),placeholder: false,});id@@ -1493,6 +1514,9 @@ pub fn apply(}applied |= assign(&mut clip.text, text);}+ if let Some(stream) = patch.audio_stream {+ applied |= assign(&mut clip.audio_stream, stream);+ }Ok(Outcome {created_id: None,applied,@@ -1772,72 +1796,97 @@ pub fn apply(}Command::DetachAudio { clip_id } => {- let has_audio = {+ // What comes off, as `(stream, name)`: one sound clip per audio+ // track when the file lists several - a recording that kept the+ // desktop and the microphone apart stays apart, each on a lane+ // of its own - else the one stream the video clip was playing.+ let sounds: Vec<(Option<u32>, String)> = {let timeline = project.active();let Some(clip) = timeline.clip(&clip_id) else {return Ok(Outcome::default());};- clip.kind == ClipKind::Video+ let media = project.media_by_id(&clip.media_id);+ let has_audio = clip.kind == ClipKind::Video&& clip.muted != Some(true)- && project- .media_by_id(&clip.media_id)- .is_some_and(|media| media.has_audio)+ && media.is_some_and(|media| media.has_audio)&& !timeline.clips.iter()- .any(|other| other.detached_from.as_deref() == Some(clip_id.as_str()))+ .any(|other| other.detached_from.as_deref() == Some(clip_id.as_str()));+ if !has_audio {+ return Ok(Outcome::default());+ }+ match media {+ Some(media) if media.audio_tracks.len() > 1 => media+ .audio_tracks+ .iter()+ .enumerate()+ .map(|(position, track)| {+ let label = if track.title.is_empty() {+ format!("Track {}", position + 1)+ } else {+ track.title.clone()+ };+ (Some(track.index), format!("{} · {label}", clip.name))+ })+ .collect(),+ _ => vec![(clip.audio_stream, clip.name.clone())],+ }};- if !has_audio {- return Ok(Outcome::default());- }let timeline = project.active_mut();let clip = timeline.clip(&clip_id).expect("checked above").clone();- // A lane free for the whole span, or a fresh one.- let track_id = {- let end = clip.start + clip.duration;- let free = timeline.tracks.iter().find(|track| {- !timeline.clips.iter().any(|other| {- other.track_id == track.id- && other.start < end- && clip.start < other.start + other.duration- })- });- match free {- Some(track) => track.id.clone(),- None => {- let id = mint.next("t");- let name = next_numbered(- "Track",- timeline.tracks.iter().map(|track| track.name.clone()),- );- timeline.tracks.push(Track {- id: id.clone(),- name,- visible: true,- muted: false,- });- id+ let mut first_sound = None;+ for (stream, name) in sounds {+ // A lane free for the whole span, or a fresh one. Each sound+ // placed takes its lane, so the next looks past it.+ let track_id = {+ let end = clip.start + clip.duration;+ let free = timeline.tracks.iter().find(|track| {+ !timeline.clips.iter().any(|other| {+ other.track_id == track.id+ && other.start < end+ && clip.start < other.start + other.duration+ })+ });+ match free {+ Some(track) => track.id.clone(),+ None => {+ let id = mint.next("t");+ let name = next_numbered(+ "Track",+ timeline.tracks.iter().map(|track| track.name.clone()),+ );+ timeline.tracks.push(Track {+ id: id.clone(),+ name,+ visible: true,+ muted: false,+ });+ id+ }}- }- };+ };- let mut sound = clip.clone();- sound.id = mint.next("c");- sound.track_id = track_id;- sound.kind = ClipKind::Audio;- sound.video_effects = Vec::new();- sound.transition_in = None;- sound.detached_from = Some(clip_id.clone());- sound.muted = None;- let sound_id = sound.id.clone();- timeline.clips.push(sound);+ let mut sound = clip.clone();+ sound.id = mint.next("c");+ sound.track_id = track_id;+ sound.name = name;+ sound.kind = ClipKind::Audio;+ sound.video_effects = Vec::new();+ sound.transition_in = None;+ sound.detached_from = Some(clip_id.clone());+ sound.muted = None;+ sound.audio_stream = stream;+ first_sound.get_or_insert_with(|| sound.id.clone());+ timeline.clips.push(sound);+ }let video = timeline.clip_mut(&clip_id).expect("still present");video.muted = Some(true);video.filters = Vec::new();Ok(Outcome {- created_id: Some(sound_id),+ created_id: first_sound,applied: true,})}diff --git a/engine/crates/concat-project/src/doc.rs b/engine/crates/concat-project/src/doc.rsindex 1d4e0071..96c14db2 100644--- a/engine/crates/concat-project/src/doc.rs+++ b/engine/crates/concat-project/src/doc.rs@@ -24,9 +24,9 @@ use serde_json::{Map, Value, json};use crate::commands::{MAX_STRETCH, MIN_STRETCH};use crate::model::{- AppliedFilter, Clip, ClipAnimation, ClipKey, ClipKind, Crop, CustomFont, Cutout, KeyEase,- KeyProperty, MediaItem, MediaKind, ParamKey, Project, SpeedPoint, TextAlign, TextStyle,- Timeline, Track, Transition, VideoSettings,+ AppliedFilter, AudioTrack, Clip, ClipAnimation, ClipKey, ClipKind, Crop, CustomFont, Cutout,+ KeyEase, KeyProperty, MediaItem, MediaKind, ParamKey, Project, SpeedPoint, TextAlign,+ TextStyle, Timeline, Track, Transition, VideoSettings,};/// Bumped only when a change cannot be absorbed by defaulting.@@ -82,6 +82,13 @@ fn read_media(raw: Option<&Value>) -> Vec<MediaItem> {video_codec: opt_string(entry.get("videoCodec")),audio_codec: opt_string(entry.get("audioCodec")),has_audio: flag(entry.get("hasAudio"), false),+ // Tolerated like everything else: a list the reader cannot+ // make sense of is no list, and clips fall back to the+ // file's first stream.+ audio_tracks: entry+ .get("audioTracks")+ .and_then(|value| serde_json::from_value::<Vec<AudioTrack>>(value.clone()).ok())+ .unwrap_or_default(),placeholder: flag(entry.get("placeholder"), false),id,path,@@ -290,6 +297,7 @@ fn read_clips(raw: Option<&Value>, tracks: &[Track], media: &[MediaItem]) -> Vecpreserve_pitch: flag(entry.get("preservePitch"), true),filters: read_filters(entry.get("filters")),video_effects: read_filters(entry.get("videoEffects")),+ audio_stream: opt_u32(entry.get("audioStream")),muted: flag(entry.get("muted"), false).then_some(true),detached_from: opt_string(entry.get("detachedFrom")),transition_in: entry.get("transitionIn").and_then(|transition| {@@ -485,7 +493,8 @@ fn read_keys(raw: Option<&Value>) -> Vec<ClipKey> {let property = KeyProperty::from_name(entry.get("property")?.as_str()?)?;let at = number(entry.get("at"), -1.0);let value = number(entry.get("value"), f64::NAN);- (0.0..=1.0).contains(&at)+ (0.0..=1.0)+ .contains(&at).then_some(ClipKey {property,at,diff --git a/engine/crates/concat-project/src/lib.rs b/engine/crates/concat-project/src/lib.rsindex 0ab941f4..ee4f7bec 100644--- a/engine/crates/concat-project/src/lib.rs+++ b/engine/crates/concat-project/src/lib.rs@@ -40,7 +40,7 @@ mod tests {use crate::commands::{ClipMove, ClipPatch, Command, NewMedia, TrackFlag, TrimEdge};use crate::doc::DocumentSettings;use crate::editor::Editor;- use crate::model::{ClipKind, MediaKind, TextStyle};+ use crate::model::{AudioTrack, ClipKind, MediaKind, TextStyle};fn media(path: &str, duration: f64, has_audio: bool) -> Command {Command::AddMedia {@@ -56,10 +56,38 @@ mod tests {video_codec: Some("h264".to_owned()),audio_codec: has_audio.then(|| "aac".to_owned()),has_audio,+ audio_tracks: Vec::new(),},}}+ /// A recording that kept its desktop sound and microphone apart: two+ /// audio streams, the second named.+ fn two_track_media(path: &str) -> Command {+ let Command::AddMedia { mut item } = media(path, 10.0, true) else {+ unreachable!()+ };+ item.audio_tracks = vec![+ AudioTrack {+ index: 1,+ codec: "aac".to_owned(),+ channels: 2,+ sample_rate: 48_000,+ title: String::new(),+ language: String::new(),+ },+ AudioTrack {+ index: 2,+ codec: "aac".to_owned(),+ channels: 1,+ sample_rate: 48_000,+ title: "Mic/Aux".to_owned(),+ language: String::new(),+ },+ ];+ Command::AddMedia { item }+ }+fn settings() -> DocumentSettings {DocumentSettings {name: "Test".to_owned(),@@ -121,6 +149,7 @@ mod tests {video_codec: None,audio_codec: None,has_audio: false,+ audio_tracks: Vec::new(),};let freeze_id = editor.apply(Command::FreezeFrame {@@ -135,7 +164,10 @@ mod tests {let clips = &editor.project().active().clips;assert_eq!(clips.len(), 3, "head + freeze + tail");- let freeze = clips.iter().find(|clip| clip.id == freeze_id).expect("freeze");+ let freeze = clips+ .iter()+ .find(|clip| clip.id == freeze_id)+ .expect("freeze");assert_eq!(freeze.kind, ClipKind::Image);assert_eq!(freeze.start, 4.0);assert_eq!(freeze.duration, 1.0);@@ -292,6 +324,122 @@ mod tests {assert_eq!(timeline.clip(&clip_id).expect("exists").muted, None);}+ /// A file with two audio tracks detaches as two sound clips, one per+ /// track, each on its own lane and named for its track; reattaching+ /// takes both back. A clip's chosen track survives the document.+ #[test]+ fn detaching_a_two_track_recording_gives_one_sound_clip_per_track() {+ let mut editor = Editor::new();+ let media_id = editor+ .apply(two_track_media("/rec.mkv"))+ .expect("adds")+ .created_id+ .expect("id");+ assert_eq!(editor.project().media[0].audio_tracks.len(), 2);+ let track_id = editor.project().active().tracks[0].id.clone();+ let clip_id = editor+ .apply(Command::AddClip {+ media_id: media_id.clone(),+ track_id,+ start: 0.0,+ })+ .expect("adds")+ .created_id+ .expect("id");++ editor+ .apply(Command::DetachAudio {+ clip_id: clip_id.clone(),+ })+ .expect("detaches");+ let timeline = editor.project().active();+ let sounds: Vec<&crate::model::Clip> = timeline+ .clips+ .iter()+ .filter(|clip| clip.kind == ClipKind::Audio)+ .collect();+ assert_eq!(sounds.len(), 2);+ assert_eq!(sounds[0].audio_stream, Some(1));+ assert_eq!(sounds[1].audio_stream, Some(2));+ assert!(sounds[0].name.ends_with("Track 1"), "{}", sounds[0].name);+ assert!(sounds[1].name.ends_with("Mic/Aux"), "{}", sounds[1].name);+ assert_ne!(+ sounds[0].track_id, sounds[1].track_id,+ "each on its own lane"+ );+ assert!(+ sounds+ .iter()+ .all(|sound| sound.detached_from.as_deref() == Some(clip_id.as_str()))+ );++ // The document keeps the choice and the list.+ let saved = crate::doc::to_document(&settings(), editor.project());+ let loaded = crate::doc::from_document(&saved).expect("loads");+ assert_eq!(+ loaded.media[0].audio_tracks,+ editor.project().media[0].audio_tracks+ );+ let back: Vec<Option<u32>> = loaded+ .active()+ .clips+ .iter()+ .filter(|clip| clip.kind == ClipKind::Audio)+ .map(|clip| clip.audio_stream)+ .collect();+ assert_eq!(back, vec![Some(1), Some(2)]);+ assert_eq!(loaded.media[0].audio_track_position(Some(2)), 1);+ assert_eq!(loaded.media[0].audio_track_position(None), 0);+ assert_eq!(loaded.media[0].audio_track_position(Some(9)), 0);++ editor+ .apply(Command::ReattachAudio {+ clip_id: sounds[1].id.clone(),+ })+ .expect("reattaches");+ let timeline = editor.project().active();+ assert_eq!(timeline.clips.len(), 1);+ assert_eq!(timeline.clip(&clip_id).expect("exists").muted, None);++ // A video clip can be told which track to play, and told to forget.+ editor+ .apply(Command::UpdateClip {+ clip_id: clip_id.clone(),+ patch: ClipPatch {+ audio_stream: Some(Some(2)),+ ..Default::default()+ },+ })+ .expect("picks");+ assert_eq!(+ editor+ .project()+ .active()+ .clip(&clip_id)+ .unwrap()+ .audio_stream,+ Some(2)+ );+ editor+ .apply(Command::UpdateClip {+ clip_id: clip_id.clone(),+ patch: ClipPatch {+ audio_stream: Some(None),+ ..Default::default()+ },+ })+ .expect("forgets");+ assert_eq!(+ editor+ .project()+ .active()+ .clip(&clip_id)+ .unwrap()+ .audio_stream,+ None+ );+ }+#[test]fn timelines_add_switch_and_delete_with_a_floor_of_one() {let mut editor = Editor::new();@@ -647,6 +795,7 @@ mod tests {video_codec: None,audio_codec: None,has_audio: false,+ audio_tracks: Vec::new(),},}).expect("fills");@@ -685,6 +834,7 @@ mod tests {video_codec: None,audio_codec: None,has_audio: false,+ audio_tracks: Vec::new(),},});assert!(@@ -1635,7 +1785,12 @@ mod tests {}).expect("keys");}- let link = &editor.project().active().clip(&clip_id).expect("clip").video_effects[0];+ let link = &editor+ .project()+ .active()+ .clip(&clip_id)+ .expect("clip")+ .video_effects[0];assert!(link.is_keyed("exposure"));assert!(!link.is_keyed("contrast"));// Halfway along a straight ride between -1 and 1 is 0; the constant@@ -1647,7 +1802,12 @@ mod tests {let document = editor.to_document(&settings());let restored = Editor::from_document(&document).expect("loads");- let link = &restored.project().active().clip(&clip_id).expect("clip").video_effects[0];+ let link = &restored+ .project()+ .active()+ .clip(&clip_id)+ .expect("clip")+ .video_effects[0];assert_eq!(link.keys_on("exposure").len(), 2);let mut editor = restored;@@ -1666,7 +1826,12 @@ mod tests {key: "exposure".to_owned(),}).expect("clears the rest");- let link = &editor.project().active().clip(&clip_id).expect("clip").video_effects[0];+ let link = &editor+ .project()+ .active()+ .clip(&clip_id)+ .expect("clip")+ .video_effects[0];assert!(!link.is_keyed("exposure"));assert_eq!(link.value_at("exposure", 0.5, 0.0), 1.0);}diff --git a/engine/crates/concat-project/src/model.rs b/engine/crates/concat-project/src/model.rsindex 9f7abc4b..ed04f64e 100644--- a/engine/crates/concat-project/src/model.rs+++ b/engine/crates/concat-project/src/model.rs@@ -55,6 +55,32 @@ impl ClipKind {}}+/// One audio stream of a media file, as the probe reported it. A screen+/// recording keeps the desktop's sound and the microphone as two of these;+/// a clip names the one it plays by `index`.+#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct AudioTrack {+ /// The stream's index within the file - what [`Clip::audio_stream`] holds.+ pub index: u32,+ /// Codec short name, e.g. "aac". Informational.+ #[serde(default)]+ pub codec: String,+ /// Channel count: 1 mono, 2 stereo.+ #[serde(default)]+ pub channels: u32,+ /// Samples per second.+ #[serde(default)]+ pub sample_rate: u32,+ /// The name the file gives the track, when it gives one ("Desktop+ /// Audio", "Mic/Aux"); empty otherwise, and the UI numbers it.+ #[serde(default, skip_serializing_if = "String::is_empty")]+ pub title: String,+ /// The track's language tag, e.g. "eng"; empty when unstated.+ #[serde(default, skip_serializing_if = "String::is_empty")]+ pub language: String,+}+/// One entry in the media bin: a file the user imported, plus what the host's/// probe learned about it. The probe metadata is stored, not re-derived, so a/// document opens meaningfully even when the file itself is missing.@@ -88,6 +114,14 @@ pub struct MediaItem {pub audio_codec: Option<String>,/// Whether the file carries an audio stream; gates `DetachAudio`.pub has_audio: bool,+ /// Every audio stream the file carries, in file order, when the probe+ /// listed them. Empty for a file without sound and for a document from+ /// before the list was kept - `has_audio` still says whether there is+ /// sound at all. More than one is a recording with its tracks apart, and+ /// what lets a clip choose between them. Skipped when empty, so+ /// documents without such media stay byte-identical.+ #[serde(default, skip_serializing_if = "Vec::is_empty")]+ pub audio_tracks: Vec<AudioTrack>,/// True when this item is a template slot: a stand-in whose metadata says/// what kind of media belongs here, waiting to be replaced by the user's/// own file (`Command::FillSlot`). In a creator's own project the path is@@ -714,17 +748,20 @@ pub struct Transition {pub duration: f64,}-/// How a title's lines sit within their block. The block itself is placed by-/// the clip's transform, so this only matters for multi-line text.+/// How a title's lines sit within their block, and which point of the block+/// the clip's position pins: a centred title is placed by its middle, a+/// left-aligned one by its block's left edge and a right-aligned one by its+/// right edge. Typing more into a left-aligned title grows it to the right+/// and leaves its left edge where it was, as alignment means everywhere.#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]#[serde(rename_all = "lowercase")]pub enum TextAlign {- /// Lines share a left edge.+ /// Lines share a left edge, which is where the clip's position is.Left,- /// Lines are centred on each other - the default, and what the reader- /// falls back to for an unrecognised value.+ /// Lines are centred on each other and on the clip's position - the+ /// default, and what the reader falls back to for an unrecognised value.Center,- /// Lines share a right edge.+ /// Lines share a right edge, which is where the clip's position is.Right,}@@ -899,6 +936,12 @@ pub struct Clip {/// Video effects, in order - the visual sibling of `filters`.#[serde(default)]pub video_effects: Vec<AppliedFilter>,+ /// Which of the media's audio streams this clip plays, by the stream's+ /// index in the file - see [`MediaItem::audio_tracks`]. None is the first+ /// in file order, which is every clip from before files with several+ /// were told apart and every clip of a file with one.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub audio_stream: Option<u32>,/// True when a video clip's embedded audio is detached out of it.#[serde(default, skip_serializing_if = "Option::is_none")]pub muted: Option<bool>,@@ -1007,8 +1050,20 @@ impl Clip {}let at = at.clamp(0.0, 1.0);match self.key_at(property, at) {- Some(index) => self.keys[index] = ClipKey { property, at, value, ease },- None => self.keys.push(ClipKey { property, at, value, ease }),+ Some(index) => {+ self.keys[index] = ClipKey {+ property,+ at,+ value,+ ease,+ }+ }+ None => self.keys.push(ClipKey {+ property,+ at,+ value,+ ease,+ }),}self.sort_keys();}@@ -1036,8 +1091,9 @@ impl Clip {/// Called by everything that can put a key in, including the document/// reader, so a hand-edited file cannot produce an unsorted track.pub fn sort_keys(&mut self) {- self.keys- .retain(|key| key.at.is_finite() && key.value.is_finite() && (0.0..=1.0).contains(&key.at));+ self.keys.retain(|key| {+ key.at.is_finite() && key.value.is_finite() && (0.0..=1.0).contains(&key.at)+ });self.keys.sort_by(|a, b| {(a.property as u8).cmp(&(b.property as u8))@@ -1201,6 +1257,22 @@ impl Project {}}+impl MediaItem {+ /// Which row of `audio_tracks` a clip's `audio_stream` is: the named+ /// stream's position, or the first row for a clip that names none or+ /// names a stream this file does not have - the same fallback the+ /// engine's readers make.+ pub fn audio_track_position(&self, stream: Option<u32>) -> usize {+ stream+ .and_then(|index| {+ self.audio_tracks+ .iter()+ .position(|track| track.index == index)+ })+ .unwrap_or(0)+ }+}+impl Default for Project {fn default() -> Self {Self::new()diff --git a/engine/crates/concat-speech/src/transcribe.rs b/engine/crates/concat-speech/src/transcribe.rsindex 82f987e9..44dafd62 100644--- a/engine/crates/concat-speech/src/transcribe.rs+++ b/engine/crates/concat-speech/src/transcribe.rs@@ -159,6 +159,10 @@ pub struct Segment {pub struct TranscribeRequest {/// The media file to transcribe.pub path: String,+ /// Which of the file's audio streams, by index; `None` is the first -+ /// the same choice the clip's playback makes, so the words come from+ /// the track that is heard.+ pub audio_stream: Option<u32>,/// Seconds into the file where the clip's source window begins.pub source_start: f64,/// How much source the clip covers, in seconds (`duration * speed`).@@ -314,6 +318,7 @@ impl Transcriber {rate: 16_000,channels: 1,format: SampleFormat::F32,+ stream: request.audio_stream.map(|index| index as usize),},).map_err(|error| error.to_string())?;diff --git a/engine/crates/concat-text/src/lib.rs b/engine/crates/concat-text/src/lib.rsindex 700003d6..83222e17 100644--- a/engine/crates/concat-text/src/lib.rs+++ b/engine/crates/concat-text/src/lib.rs@@ -12,9 +12,19 @@//! Frame-sized on purpose. The compositor places a picture by fitting it into//! the frame and then applying the clip's transform about its centre, so a//! canvas that *is* the frame fits at exactly one, decodes without resampling,-//! and puts the block's centre where the clip's centre is. The clip's offset+//! and puts the canvas's centre where the clip's centre is. The clip's offset//! and rotation then mean the same thing for a title as for footage.//!+//! Where the block sits on that canvas is the alignment's to say. A centred+//! title has its block centred, so the clip's position is the block's middle.+//! A left-aligned one has its block's left edge on the canvas's centre, a+//! right-aligned one its right edge: the position is the edge the words are+//! aligned to, and typing more grows the block *away* from that edge rather+//! than out from the middle - which is what left and right mean everywhere+//! else, and what a title that keeps its left margin while its words change+//! needs. [`Rendered`] reports where the block landed so a monitor can draw+//! its outline there.+//!//! Sizes in the style are fractions of the frame's height, as the document//! stores them, so a title looks the same at 720p and 4K. Everything here//! converts to pixels once, at the top.@@ -26,15 +36,17 @@ use tiny_skia::{Stroke, Transform,};-/// How a title's lines sit within their block.+/// How a title's lines sit within their block, and which point of the block+/// the clip's position holds still: its left edge, its centre or its right+/// edge. See the module docs.#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]pub enum Align {- /// Lines share a left edge.+ /// Lines share a left edge, and the block's left edge is the anchor.Left,- /// Lines are centred on each other.+ /// Lines are centred on each other, and the block's centre is the anchor.#[default]Center,- /// Lines share a right edge.+ /// Lines share a right edge, and the block's right edge is the anchor.Right,}@@ -85,6 +97,14 @@ pub struct Rendered {pub block_width: u32,/// The painted block's height, on the same terms.pub block_height: u32,+ /// Where the block's centre is, as an offset from the canvas's centre in+ /// pixels, x to the right. Zero for a centred title; half the block's+ /// width for a left-aligned one, whose block starts at the centre; minus+ /// that for a right-aligned one. An outline goes here, not on the clip.+ pub block_dx: i32,+ /// The vertical half of `block_dx`, y down. Always zero for now: the+ /// block is centred vertically whatever the alignment.+ pub block_dy: i32,}/// What can go wrong. Fonts fall back rather than fail, so this is short.@@ -336,7 +356,8 @@ fn blur(pixmap: &mut Pixmap, radius: usize) {}/// Paints `style` onto a `width` × `height` transparent canvas, the block-/// centred, and returns it PNG-encoded with the block's size.+/// anchored on the canvas's centre by its alignment (see [`Align`]), and+/// returns it PNG-encoded with the block's size and where it landed.pub fn render(fonts: &Fonts,style: &TitleStyle,@@ -383,6 +404,8 @@ pub fn render(height,block_width: 0,block_height: 0,+ block_dx: 0,+ block_dy: 0,});}@@ -396,8 +419,19 @@ pub fn render(};let outer_w = block_w + 2.0 * pad_x;let outer_h = block_h + 2.0 * pad_y;- let left = (width as f32 - outer_w) / 2.0 + pad_x;+ // The anchor is the canvas's centre - where the clip's position lands -+ // and the alignment says which edge of the block sits on it, plate and+ // all. Growing words then push the far edge and leave the anchored one+ // where it is.+ let anchor_x = width as f32 / 2.0;+ let outer_left = match style.align {+ Align::Left => anchor_x,+ Align::Center => anchor_x - outer_w / 2.0,+ Align::Right => anchor_x - outer_w,+ };+ let left = outer_left + pad_x;let top = (frame_h - outer_h) / 2.0 + pad_y;+ let block_dx = (outer_left + outer_w / 2.0 - anchor_x).round() as i32;// One path for all the words, placed. Each line is aligned within the// block's width and sits on its own baseline.@@ -426,6 +460,8 @@ pub fn render(height,block_width: outer_w.round() as u32,block_height: outer_h.round() as u32,+ block_dx,+ block_dy: 0,});};@@ -511,6 +547,8 @@ pub fn render(height,block_width: outer_w.round() as u32,block_height: outer_h.round() as u32,+ block_dx,+ block_dy: 0,})}@@ -562,6 +600,22 @@ mod tests {pixmap.pixels().iter().filter(|p| p.alpha() > 0).count()}+ /// The leftmost and rightmost columns holding any paint.+ fn painted_span(png: &[u8]) -> (u32, u32) {+ let pixmap = Pixmap::decode_png(png).expect("our own PNG decodes");+ let width = pixmap.width();+ let mut left = width;+ let mut right = 0;+ for (index, pixel) in pixmap.pixels().iter().enumerate() {+ if pixel.alpha() > 0 {+ let x = index as u32 % width;+ left = left.min(x);+ right = right.max(x);+ }+ }+ (left, right)+ }+#[test]fn colours_parse_both_lengths() {assert_eq!(colour("#ff0000"), Some(Color::from_rgba8(255, 0, 0, 255)));@@ -581,6 +635,57 @@ mod tests {assert!(opaque_pixels(&out.png) > 100);}+ /// Left-aligned words start at the anchor and run right; right-aligned+ /// ones end there; centred ones straddle it. Growing the words moves+ /// only the far edge.+ #[test]+ fn alignment_anchors_the_block_on_its_edge() {+ let fonts = Fonts::new();+ let (width, height) = (640, 360);+ let centre = width / 2;+ // The shadow reaches a hair past the words to the right and below;+ // this is that hair, generously.+ let slack = 4;++ let mut left = style("Hello");+ left.align = Align::Left;+ left.shadow = false;+ let out = render(&fonts, &left, width, height).expect("renders");+ let (first, _) = painted_span(&out.png);+ assert!(+ first + slack >= centre,+ "left-aligned words start at {first}, left of the anchor"+ );+ assert!(out.block_dx > 0);+ assert!((out.block_dx as u32).abs_diff(out.block_width / 2) <= 1);+ // More words: the left edge stays, the block grows rightwards.+ let mut longer = left.clone();+ longer.content = "Hello there".to_owned();+ let more = render(&fonts, &longer, width, height).expect("renders");+ let (again, _) = painted_span(&more.png);+ assert!(+ again.abs_diff(first) <= 1,+ "the left edge moved from {first} to {again}"+ );+ assert!(more.block_width > out.block_width);++ let mut right = style("Hello");+ right.align = Align::Right;+ right.shadow = false;+ let out = render(&fonts, &right, width, height).expect("renders");+ let (_, last) = painted_span(&out.png);+ assert!(+ last <= centre + slack,+ "right-aligned words end at {last}, right of the anchor"+ );+ assert!(out.block_dx < 0);++ let out = render(&fonts, &style("Hello"), width, height).expect("renders");+ let (first, last) = painted_span(&out.png);+ assert!(first < centre && last > centre);+ assert_eq!(out.block_dx, 0);+ }+/// The canvas is the frame; the block is not.#[test]fn empty_content_is_an_empty_canvas() {diff --git a/engine/crates/concat/locales/en.json b/engine/crates/concat/locales/en.jsonindex 6cfa463a..56d0484e 100644--- a/engine/crates/concat/locales/en.json+++ b/engine/crates/concat/locales/en.json@@ -36,6 +36,7 @@"American": "American","Amount": "Amount","Analysis": "Analysis",+ "Angle": "Angle","Animation": "Animation","Appearance": "Appearance","Application": "Application",@@ -44,6 +45,7 @@"audio effect": "audio effect","Audio Effects": "Audio Effects","Audio effects": "Audio effects",+ "Audio track": "Audio track","Auto": "Auto","auto": "auto","Auto captions": "Auto captions",@@ -104,6 +106,7 @@"Choose where the project folder should go": "Choose where the project folder should go","Choose…": "Choose…","Chroma key": "Chroma key",+ "Chromatic Aberration": "Chromatic Aberration","Cinematic": "Cinematic","Clarity": "Clarity","Clean": "Clean",@@ -117,6 +120,7 @@"Colour bleeding sideways, scan lines, softness, tape noise and too much saturation, with the slight vertical smear of a worn head.": "Colour bleeding sideways, scan lines, softness, tape noise and too much saturation, with the slight vertical smear of a worn head.","Colour filter": "Colour filter","Colour that sings and contrast that bites, with a little edge: the look that reads as a better camera. Skin is held back from the boost.": "Colour that sings and contrast that bites, with a little edge: the look that reads as a better camera. Skin is held back from the boost.",+ "Colours pushed the wrong way, as film developed in the wrong chemicals.": "Colours pushed the wrong way, as film developed in the wrong chemicals.","Combo": "Combo","Concat {0}": "Concat {0}","Content": "Content",@@ -137,12 +141,14 @@"Creating…": "Creating…","Crisper edges.": "Crisper edges.","Crop": "Crop",+ "Cross Process": "Cross Process","Curve": "Curve","Custom": "Custom","Custom removal": "Custom removal","Cutout": "Cutout","Dancing grain, the texture of film stock.": "Dancing grain, the texture of film stock.","Dark": "Dark",+ "Dark lines across the picture every few pixels, the way a tube television drew it.": "Dark lines across the picture every few pixels, the way a tube television drew it.","Darken": "Darken","Darkened corners that draw the eye to the middle.": "Darkened corners that draw the eye to the middle.","Daydream": "Daydream",@@ -160,6 +166,7 @@"Depth": "Depth","Destination": "Destination","Detach audio": "Detach audio",+ "Detail": "Detail","DETAILS": "DETAILS","Digicam": "Digicam","Distort": "Distort",@@ -185,6 +192,7 @@"Effect library": "Effect library","Effects": "Effects","Elegant": "Elegant",+ "Emboss": "Emboss","Enable {0}": "Enable {0}","Engine": "Engine","English": "English",@@ -193,7 +201,9 @@"Eraser": "Eraser","Even": "Even","Every colour becomes its opposite.": "Every colour becomes its opposite.",+ "Every colour turned around the wheel by the same angle.": "Every colour turned around the wheel by the same angle.","Everyday": "Everyday",+ "Everything brighter than the threshold turns negative, as an over-exposed print does.": "Everything brighter than the threshold turns negative, as an over-exposed print does.","Everything on this machine. Transcription and speech run against models you download here, and nothing about a project leaves the device.": "Everything on this machine. Transcription and speech run against models you download here, and nothing about a project leaves the device.","Export": "Export","Export again": "Export again",@@ -209,6 +219,7 @@"Fade": "Fade","Fade in": "Fade in","Fade out": "Fade out",+ "Faded": "Faded","Fades": "Fades","Fair": "Fair","Falling Dust": "Falling Dust",@@ -256,6 +267,7 @@"Green": "Green","Green Screen": "Green Screen","Greens": "Greens",+ "Grey, hard and dark: a crime picture.": "Grey, hard and dark: a crime picture.","Half": "Half","Hall": "Hall","Haze": "Haze",@@ -268,6 +280,7 @@"Highlights": "Highlights","Hindi": "Hindi","Horizontal": "Horizontal",+ "Hue Shift": "Hue Shift","Images": "Images","Import a LUT (.cube)": "Import a LUT (.cube)","Import LUT": "Import LUT",@@ -307,6 +320,7 @@"Levels": "Levels","Library": "Library","Lift": "Lift",+ "Lifted blacks and quiet colour, a matte finish.": "Lifted blacks and quiet colour, a matte finish.","Lifts speech out of the mix: a little presence, a little control.": "Lifts speech out of the mix: a little presence, a little control.","Light": "Light","Light leak": "Light leak",@@ -349,6 +363,7 @@"Models": "Models","Modify": "Modify","Mono": "Mono",+ "mono": "mono","Montage": "Montage","Moody": "Moody","More low end.": "More low end.",@@ -373,6 +388,7 @@"No templates yet. Mark media as slots, then save this project as a template.": "No templates yet. Mark media as slots, then save this project as a template.","No voice to read with": "No voice to read with","No {0} matches that.": "No {0} matches that.",+ "Noir": "Noir","Noise": "Noise","Noise Reduction": "Noise Reduction","None": "None",@@ -433,6 +449,7 @@"Protect skin": "Protect skin","Pulse": "Pulse","Punch": "Punch",+ "Pure black and pure white, cut at one level of brightness.": "Pure black and pure white, cut at one level of brightness.","Put the detached sound back on its picture": "Put the detached sound back on its picture","Put this clip's sound on its own lane": "Put this clip's sound on its own lane","Quality": "Quality",@@ -450,6 +467,7 @@"Reattach": "Reattach","Recent": "Recent","Recommended": "Recommended",+ "Red and blue pulled apart, the colour fringing of a cheap lens.": "Red and blue pulled apart, the colour fringing of a cheap lens.","Redo": "Redo","Reduce noise": "Reduce noise","Regular": "Regular",@@ -480,6 +498,7 @@"Saved template “{0}”": "Saved template “{0}”","Scale": "Scale","Scale and position are fractions of the output frame, so a clip keeps its framing if the project is re-sized. The same numbers move with the box on the monitor. Width and height pull one axis beyond the scale; the edge grips on the monitor do the same.": "Scale and position are fractions of the output frame, so a clip keeps its framing if the project is re-sized. The same numbers move with the box on the monitor. Width and height pull one axis beyond the scale; the edge grips on the monitor do the same.",+ "Scanlines": "Scanlines","Screen": "Screen","Script": "Script","Search {0}s": "Search {0}s",@@ -502,6 +521,7 @@"Shape": "Shape","Share": "Share","Sharpen": "Sharpen",+ "Shift": "Shift","Shifts the picture towards candlelight.": "Shifts the picture towards candlelight.","Shifts the picture towards daylight shade.": "Shifts the picture towards daylight shade.","Show in folder": "Show in folder",@@ -510,6 +530,7 @@"Silent": "Silent","Similarity": "Similarity","Size": "Size",+ "Sketch": "Sketch","Skies deeper and bluer, foliage cooler and calmer, the rest of the picture and every face untouched. Travel footage.": "Skies deeper and bluer, foliage cooler and calmer, the rest of the picture and every face untouched. Travel footage.","Skin warmed toward peach and softened, shadows lifted so lines ease, contrast low. Portraits and talking heads; it is meant to run at half strength.": "Skin warmed toward peach and softened, shadows lifted so lines ease, contrast low. Portraits and talking heads; it is meant to run at half strength.","Sky": "Sky",@@ -528,8 +549,10 @@"Soft Light": "Soft Light","Soften": "Soften","Softness": "Softness",+ "Solarize": "Solarize","Soon": "Soon","Sound": "Sound",+ "Source": "Source","Space": "Space","Spanish": "Spanish","Speak": "Speak",@@ -539,6 +562,7 @@"Split at playhead": "Split at playhead","Split at playhead (S, or ⌘B for every clip)": "Split at playhead (S, or ⌘B for every clip)","Spooky": "Spooky",+ "stereo": "stereo","Stone and height: reflections that take a while to arrive.": "Stone and height: reflections that take a while to arrive.","Strength": "Strength","Stylised": "Stylised",@@ -569,12 +593,15 @@"The left half, reflected onto the right.": "The left half, reflected onto the right.","The manual colour panel every picture clip carries: exposure, tone, balance, sharpness and edge.": "The manual colour panel every picture clip carries: exposure, tone, balance, sharpness and edge.","The narrow band of a phone line.": "The narrow band of a phone line.",+ "The picture pressed into relief, lit from the top left.": "The picture pressed into relief, lit from the top left.",+ "The picture's edges drawn over it in its own colours.": "The picture's edges drawn over it in its own colours.","The project folder is created now; the edit autosaves into it.": "The project folder is created now; the edit autosaves into it.","The warm brown of an old photograph.": "The warm brown of an old photograph.","Theme": "Theme","There is nothing on the timeline to export": "There is nothing on the timeline to export","These settings apply to the whole edit and cannot be changed later.": "These settings apply to the whole edit and cannot be changed later.","This clip has no file to transcribe": "This clip has no file to transcribe",+ "Threshold": "Threshold","TIMELINE": "TIMELINE","Timeline": "Timeline","Timeline 1": "Timeline 1",@@ -585,6 +612,7 @@"Tone": "Tone","Toolchain": "Toolchain","Top": "Top",+ "Track {0}": "Track {0}","Tracking": "Tracking","Tracks": "Tracks","Transcriber": "Transcriber",@@ -627,6 +655,7 @@"Zoom Out": "Zoom Out","Zoom out": "Zoom out","Zoom to fit": "Zoom to fit",+ "{0} channels": "{0} channels","{0} days ago": "{0} days ago","{0} Hz": "{0} Hz","{0} installed · {1} MB on disk": "{0} installed · {1} MB on disk",diff --git a/engine/crates/concat/src/host.rs b/engine/crates/concat/src/host.rsindex 81d1c183..b1758ba2 100644--- a/engine/crates/concat/src/host.rs+++ b/engine/crates/concat/src/host.rs@@ -12,8 +12,9 @@//! which is why it can live in a plain `RefCell` with no lock.use std::cell::RefCell;+use std::collections::VecDeque;use std::rc::Rc;-use std::sync::Arc;+use std::sync::{Arc, Condvar, Mutex, OnceLock};use concat_host::export::Exporter;use concat_host::playback::{Playback, PlaybackEvents};@@ -170,20 +171,90 @@ pub fn spawn<T: Send + 'static>(work: impl FnOnce() -> T + Send + 'static,then: impl FnOnce(&mut Studio, &App, &Models, T) + Send + 'static,) {- spawn_detached(move || {- let result = work();- let _ = slint::invoke_from_event_loop(move || {- Shell::with(|shell, app| {- {- let mut studio = shell.studio.borrow_mut();- then(&mut studio, &app, &shell.models, result);- }- shell.studio.borrow().publish(&app, &shell.models);- });+ spawn_detached(move || deliver(work(), then));+}++/// Hands a worker's result to the event-loop thread: `then` with the state+/// and the window, and a full publish after it.+fn deliver<T: Send + 'static>(+ result: T,+ then: impl FnOnce(&mut Studio, &App, &Models, T) + Send + 'static,+) {+ let _ = slint::invoke_from_event_loop(move || {+ Shell::with(|shell, app| {+ {+ let mut studio = shell.studio.borrow_mut();+ then(&mut studio, &app, &shell.models, result);+ }+ shell.studio.borrow().publish(&app, &shell.models);});});}+/// The bin's artwork lane: its own few threads, taking jobs off one queue.+///+/// Not the shared workers. Those are the monitor's: a frame, its prefetch,+/// and one more, and an import of twenty files asks for twenty thumbnails,+/// twenty filmstrips and twenty waveforms at once - on the shared workers+/// the monitor would wait behind all of it, and as one thread per job it+/// was every core at a hundred percent until the bin had its pictures, the+/// machine unusable for a minute (#52). The artwork is decoration; it can+/// arrive a few files at a time. So it queues here, on threads the monitor+/// never needs, and only [`art_workers`] decoders ever run together,+/// whatever the size of the import.+struct ArtLane {+ queue: Mutex<VecDeque<Box<dyn FnOnce() + Send>>>,+ ready: Condvar,+}++/// How many artwork decoders run at once: a quarter of the machine's+/// threads, one at least and three at most. A decoder is single-threaded,+/// so this is roughly the share of the machine the bin's pictures may take+/// while the window, playback and the monitor keep the rest.+fn art_workers() -> usize {+ std::thread::available_parallelism().map_or(1, |threads| (threads.get() / 4).clamp(1, 3))+}++/// Like [`spawn`], on the artwork lane: `work` waits its turn behind the+/// other artwork rather than starting a thread of its own.+pub fn spawn_art<T: Send + 'static>(+ work: impl FnOnce() -> T + Send + 'static,+ then: impl FnOnce(&mut Studio, &App, &Models, T) + Send + 'static,+) {+ static LANE: OnceLock<Arc<ArtLane>> = OnceLock::new();+ let lane = LANE.get_or_init(|| {+ let lane = Arc::new(ArtLane {+ queue: Mutex::new(VecDeque::new()),+ ready: Condvar::new(),+ });+ for index in 0..art_workers() {+ let lane = Arc::clone(&lane);+ let _ = std::thread::Builder::new()+ .name(format!("media-art-{index}"))+ .spawn(move || {+ loop {+ let job = {+ let mut queue = lane.queue.lock().unwrap_or_else(|e| e.into_inner());+ loop {+ if let Some(job) = queue.pop_front() {+ break job;+ }+ queue = lane.ready.wait(queue).unwrap_or_else(|e| e.into_inner());+ }+ };+ job();+ }+ });+ }+ lane+ });+ lane.queue+ .lock()+ .unwrap_or_else(|e| e.into_inner())+ .push_back(Box::new(move || deliver(work(), then)));+ lane.ready.notify_one();+}+/// Runs `body` on the event-loop thread from anywhere, with a full publish/// after. For progress reports from a worker.pub fn on_ui(body: impl FnOnce(&mut Studio, &App, &Models) + Send + 'static) {@@ -222,6 +293,10 @@ pub fn probe_error(error: &str) -> String {pub struct MediaArt {/// The media id the art belongs to.pub id: String,+ /// Which audio stream the peaks are of: `None` for the file's first,+ /// which is the media's own art, or a named one a clip plays. Named+ /// streams get peaks only, never a picture.+ pub stream: Option<u32>,/// A small first frame, for footage and stills. A frame rather than an/// image: a Slint image cannot cross a thread, and this is made on one.pub thumbnail: Option<concat_core::frame::Frame>,@@ -241,7 +316,8 @@ const STRIP_FRAMES: u32 = 24;const STRIP_HEIGHT: u32 = 64;/// Decodes the art for one media item. `project` is where the peaks cache-/// lives.+/// lives. With a `stream` named, only that stream's peaks: the pictures are+/// the media's, made once with the default stream's peaks.pub fn media_art(id: String,path: String,@@ -249,17 +325,31 @@ pub fn media_art(has_audio: bool,duration: Option<f64>,project: String,+ stream: Option<u32>,) -> MediaArt {use concat_project::model::MediaKind;+ if stream.is_some() {+ return MediaArt {+ id,+ stream,+ thumbnail: None,+ peaks: media::peaks(&path, stream, Some(&project))+ .ok()+ .map(Arc::new),+ strip: None,+ };+ }let thumbnail = match kind {MediaKind::Video | MediaKind::Image => {+ // Within the first two seconds, so the walk from the keyframe+ // before it is at most those two seconds of frames.let at = duration.map_or(0.0, |seconds| (seconds * 0.25).min(2.0));media::still_at(&path, if kind == MediaKind::Image { 0.0 } else { at }, 160).ok()}MediaKind::Audio => None,};let peaks = (kind == MediaKind::Audio || has_audio)- .then(|| media::peaks(&path, Some(&project)).ok().map(Arc::new))+ .then(|| media::peaks(&path, None, Some(&project)).ok().map(Arc::new)).flatten();let strip = match kind {MediaKind::Video => media::filmstrip(&path, STRIP_FRAMES, STRIP_HEIGHT)@@ -270,6 +360,7 @@ pub fn media_art(};MediaArt {id,+ stream: None,thumbnail,peaks,strip,diff --git a/engine/crates/concat/src/lib.rs b/engine/crates/concat/src/lib.rsindex a480816b..d93b26c8 100644--- a/engine/crates/concat/src/lib.rs+++ b/engine/crates/concat/src/lib.rs@@ -104,8 +104,10 @@ pub fn run() -> Result<(), slint::PlatformError> {editor.set_visual_params(ModelRc::from(models.visual_params.clone()));editor.set_audio_params(ModelRc::from(models.audio_params.clone()));editor.set_adjust_params(ModelRc::from(models.adjust_params.clone()));- app.global::<Keyframes>().set_rows(ModelRc::from(models.key_rows.clone()));- app.global::<Library>().set_views(ModelRc::from(models.library_views.clone()));+ app.global::<Keyframes>()+ .set_rows(ModelRc::from(models.key_rows.clone()));+ app.global::<Library>()+ .set_views(ModelRc::from(models.library_views.clone()));editor.set_menu_items(ModelRc::from(models.menu.clone()));app.set_caption_models(ModelRc::from(models.caption_models.clone()));app.set_speech_models(ModelRc::from(models.speech_models.clone()));@@ -836,6 +838,11 @@ pub fn run() -> Result<(), slint::PlatformError> {// it; see Editor.blur. The chords that are also menu rows go through the// menu's handler, so the key and the row cannot come apart.editor.on_blur(|| Shell::with(|_, app| app.invoke_blur()));+ // A field that is done being typed into - Enter, Escape - releases the+ // focus the same way, rather than clearing it: a cleared focus is a+ // window where no key reaches anything. See Focus in util.slint.+ app.global::<Focus>()+ .on_release(|| Shell::with(|_, app| app.invoke_blur()));editor.on_shortcut(move |action: SharedString| match action.as_str() {"import" | "export" | "settings" | "zoom-in" | "zoom-out" | "start" | "end" | "snap" => {Shell::with(|_, app| app.invoke_app_menu_selected(action.clone()));diff --git a/engine/crates/concat/src/presets.rs b/engine/crates/concat/src/presets.rsindex 0fe82439..1d92e081 100644--- a/engine/crates/concat/src/presets.rs+++ b/engine/crates/concat/src/presets.rs@@ -90,7 +90,9 @@ impl PresetStyle {let base = TextStyle::default();TextStyle {content: self.content.unwrap_or_else(|| name.to_owned()),- font_family: self.font_family.unwrap_or_else(|| "Helvetica Neue".to_owned()),+ font_family: self+ .font_family+ .unwrap_or_else(|| "Helvetica Neue".to_owned()),font_size: self.font_size.unwrap_or(base.font_size).clamp(0.005, 1.0),font_weight: self.font_weightdiff --git a/engine/crates/concat/src/studio.rs b/engine/crates/concat/src/studio.rsindex 9981a833..db502f59 100644--- a/engine/crates/concat/src/studio.rs+++ b/engine/crates/concat/src/studio.rs@@ -45,7 +45,7 @@ use crate::dock::{use crate::format::{bytes, colour_of, eta, frames_timecode, hex_of, hex_with_alpha, wave_path, when_phrase,};-use crate::host::{Host, MediaArt, image_at, image_of, media_art, on_ui, spawn};+use crate::host::{Host, MediaArt, image_at, image_of, media_art, on_ui, spawn, spawn_art};use crate::i18n::{self, t, tf};use crate::prefs::Preferences;use crate::presets::{self, TextPreset};@@ -713,7 +713,12 @@ pub struct Studio {/// included - and the revision and output size it was made at. Shared/// with the monitor by pointer, so it can keep its plan for as long as/// the list is the same one.- flat: Option<(u64, u32, u32, std::sync::Arc<Vec<concat_export::ExportClip>>)>,+ flat: Option<(+ u64,+ u32,+ u32,+ std::sync::Arc<Vec<concat_export::ExportClip>>,+ )>,/// An inspector commit waiting to land: a knob being dragged commits/// on every move, and each commit was a command, an undo of the last,/// a rebuild of the mix and a full publish. The commit is held until@@ -736,7 +741,9 @@ pub struct Studio {/// Each title's painted block in frame pixels, by clip id, as of the/// last time the monitor asked for a frame. What the stage box for a/// text clip is drawn from; see `footprint`.- pub title_blocks: HashMap<String, (u32, u32)>,+ /// Per text clip: the painted block's size in frame pixels, and its+ /// centre's offset from the clip's centre - see `TitleClip::offset`.+ pub title_blocks: HashMap<String, ((u32, u32), (i32, i32))>,pub drop: Option<DropPlan>,pub project_sheet: ProjectSheet,pub captions: CaptionsSheet,@@ -769,6 +776,32 @@ pub struct Studio {// ── conversions between the document and the window ─────────────────────+/// The key a media item's art is kept under: the media id for its pictures+/// and its default stream's waveform, and the id with the stream for the+/// waveform of another stream a clip plays. Peaks and pending jobs share it.+fn art_key(media_id: &str, stream: Option<u32>) -> String {+ match stream {+ None => media_id.to_owned(),+ Some(index) => format!("{media_id}#{index}"),+ }+}++/// One audio track's row in the Audio panel's list: the name the file gave+/// it or its number, and its channel layout.+fn audio_track_label(track: &model::AudioTrack, position: usize) -> String {+ let name = if track.title.is_empty() {+ tf("Track {0}", &[&(position + 1)])+ } else {+ track.title.clone()+ };+ let layout = match track.channels {+ 1 => t("mono"),+ 2 => t("stereo"),+ channels => tf("{0} channels", &[&channels]),+ };+ format!("{name} · {layout}")+}+fn kind_of(clip: &Clip) -> ClipKind {match clip.kind {model::ClipKind::Video => ClipKind::Video,@@ -1079,12 +1112,15 @@ fn import_cube(dir: &std::path::Path, path: &std::path::Path) -> Result<String,}out.push(255);}- let file = std::fs::File::create(folder.join("preview.png")).map_err(|error| error.to_string())?;+ let file =+ std::fs::File::create(folder.join("preview.png")).map_err(|error| error.to_string())?;let mut encoder = png::Encoder::new(std::io::BufWriter::new(file), width, height);encoder.set_color(png::ColorType::Rgba);encoder.set_depth(png::BitDepth::Eight);let mut writer = encoder.write_header().map_err(|error| error.to_string())?;- writer.write_image_data(&out).map_err(|error| error.to_string())?;+ writer+ .write_image_data(&out)+ .map_err(|error| error.to_string())?;Ok(id)}@@ -1649,6 +1685,7 @@ impl Studio {.flat_map(concat_export::audio_pieces).map(|piece| ClipSpec {path: piece.path.to_string_lossy().into_owned(),+ audio_stream: piece.stream.map(|index| index as u32),start: piece.start,duration: piece.duration,source_start: piece.source_start,@@ -1724,7 +1761,8 @@ impl Studio {);// Titles, painted to pictures and rejoined; see concat-host's titles.for title in self.host.titles.clips(self.project(), width, height) {- self.title_blocks.insert(title.clip_id, title.block);+ self.title_blocks+ .insert(title.clip_id, (title.block, title.offset));clips.push(title.clip);}let clips = std::sync::Arc::new(clips);@@ -1749,48 +1787,54 @@ impl Studio {if let Some(filter_id) = self.audition.clone() {let effects = vec![AppliedFilter::new(filter_id)];let video_filter_chain = concat_export::chains::video_effect_chain(&effects);- let track = clips.iter().map(|flat| flat.track).max().map_or(0, |top| top + 1);- own.get_or_insert_with(|| (*clips).clone()).push(concat_export::ExportClip {- path: String::new(),- kind: concat_export::ClipKind::Layer,- start: 0.0,- duration: f64::from(self.duration()).max(1.0),- source_start: 0.0,- track,- hidden: false,- muted: true,- volume: 0.0,- fade_in: 0.0,- fade_out: 0.0,- filter_chain: String::new(),- speed: 1.0,- preserve_pitch: true,- speed_curve: Vec::new(),- reverse: false,- animation: Vec::new(),- flip_h: false,- flip_v: false,- blend: String::new(),- crop: None,- effects,- transition_chain: String::new(),- scale: 1.0,- offset_x: 0.0,- offset_y: 0.0,- rotation: 0.0,- stretch_x: 1.0,- stretch_y: 1.0,- opacity: 1.0,- video_filter_chain,- transition: None,- video_fade_in: 0.0,- media_width: None,- media_height: None,- has_audio: Some(false),- cutout: None,- mask_dir: String::new(),- highlighted: false,- });+ let track = clips+ .iter()+ .map(|flat| flat.track)+ .max()+ .map_or(0, |top| top + 1);+ own.get_or_insert_with(|| (*clips).clone())+ .push(concat_export::ExportClip {+ path: String::new(),+ audio_stream: None,+ kind: concat_export::ClipKind::Layer,+ start: 0.0,+ duration: f64::from(self.duration()).max(1.0),+ source_start: 0.0,+ track,+ hidden: false,+ muted: true,+ volume: 0.0,+ fade_in: 0.0,+ fade_out: 0.0,+ filter_chain: String::new(),+ speed: 1.0,+ preserve_pitch: true,+ speed_curve: Vec::new(),+ reverse: false,+ animation: Vec::new(),+ flip_h: false,+ flip_v: false,+ blend: String::new(),+ crop: None,+ effects,+ transition_chain: String::new(),+ scale: 1.0,+ offset_x: 0.0,+ offset_y: 0.0,+ rotation: 0.0,+ stretch_x: 1.0,+ stretch_y: 1.0,+ opacity: 1.0,+ video_filter_chain,+ transition: None,+ video_fade_in: 0.0,+ media_width: None,+ media_height: None,+ has_audio: Some(false),+ cutout: None,+ mask_dir: String::new(),+ highlighted: false,+ });}// While the brushes are out, the clip being painted is drawn with// its cutout tinted over the whole picture rather than cut, so a@@ -1837,7 +1881,9 @@ impl Studio {{let monitor = monitor.clone();let settings = settings.clone();- crate::host::spawn_detached(move || monitor.prefetch(clips, &settings, spec, 2));+ crate::host::spawn_detached(move || {+ monitor.prefetch(clips, &settings, spec, 2)+ });}frame},@@ -2070,14 +2116,27 @@ impl Studio {);}- /// Decodes art for every media item that has none yet.+ /// Decodes art for every media item that has none yet: its pictures+ /// and the waveform of its default audio stream - and, for every clip+ /// that plays another of its media's streams, that stream's waveform+ /// too, so a lane shows the sound it will make.fn request_media_art(&mut self) {let Some(session) = self.session.as_ref() else {return;};let project_path = session.path().to_owned();- let wanted: Vec<(String, String, model::MediaKind, bool, Option<f64>)> = self- .project()+ /// One job: the art key it fills, and what to decode.+ struct Want {+ key: String,+ id: String,+ path: String,+ kind: model::MediaKind,+ has_audio: bool,+ duration: Option<f64>,+ stream: Option<u32>,+ }+ let project = self.project();+ let mut wanted: Vec<Want> = project.media.iter().filter(|item| !item.placeholder && !item.path.is_empty())@@ -2089,23 +2148,70 @@ impl Studio {&& !self.peaks.contains_key(&item.id);needs_thumb || needs_peaks})- .map(|item| {- (- item.id.clone(),- item.path.clone(),- item.kind,- item.has_audio,- item.duration,- )+ .map(|item| Want {+ key: item.id.clone(),+ id: item.id.clone(),+ path: item.path.clone(),+ kind: item.kind,+ has_audio: item.has_audio,+ duration: item.duration,+ stream: None,}).collect();- for (id, path, kind, has_audio, duration) in wanted {- self.art_pending.insert(id.clone());+ // The other streams clips have chosen, once each.+ let mut asked: HashSet<String> = wanted.iter().map(|want| want.key.clone()).collect();+ for clip in project+ .timelines+ .iter()+ .flat_map(|timeline| timeline.clips.iter())+ {+ let Some(stream) = clip.audio_stream else {+ continue;+ };+ let Some(item) = project.media_by_id(&clip.media_id) else {+ continue;+ };+ if item.placeholder+ || item.path.is_empty()+ || !(item.kind == model::MediaKind::Audio || item.has_audio)+ {+ continue;+ }+ let key = art_key(&item.id, Some(stream));+ if self.peaks.contains_key(&key)+ || self.art_pending.contains(&key)+ || !asked.insert(key.clone())+ {+ continue;+ }+ wanted.push(Want {+ key,+ id: item.id.clone(),+ path: item.path.clone(),+ kind: item.kind,+ has_audio: item.has_audio,+ duration: item.duration,+ stream: Some(stream),+ });+ }+ for want in wanted {+ self.art_pending.insert(want.key);let project = project_path.clone();- spawn(- move || media_art(id, path, kind, has_audio, duration, project),+ let Want {+ id,+ path,+ kind,+ has_audio,+ duration,+ stream,+ ..+ } = want;+ // On the artwork lane, a few at a time - see host.rs.+ spawn_art(+ move || media_art(id, path, kind, has_audio, duration, project, stream),|studio, _, _, art: MediaArt| {- studio.art_pending.remove(&art.id);+ let key = art_key(&art.id, art.stream);+ studio.art_pending.remove(&key);if let Some(frame) = art.thumbnail {studio.thumbs.insert(art.id.clone(), image_of(&frame));}@@ -2119,8 +2225,8 @@ impl Studio {studio.strips.insert(art.id.clone(), strip);}if let Some(peaks) = art.peaks {- studio.peaks.insert(art.id.clone(), peaks);- let prefix = format!("{}|", art.id);+ let prefix = format!("{key}|");+ studio.peaks.insert(key, peaks);studio.waves.borrow_mut()@@ -2171,16 +2277,17 @@ impl Studio {/// second so a trim revisits a handful of entries rather than minting/// one per pointer event.fn wave(&self, clip: &Clip) -> SharedString {- let Some(peaks) = self.peaks.get(&clip.media_id) else {+ // The stream this clip plays; its peaks come when they are decoded,+ // and until then the lane is bare rather than showing another+ // track's shape.+ let art = art_key(&clip.media_id, clip.audio_stream);+ let Some(peaks) = self.peaks.get(&art) else {return SharedString::new();};let step = |seconds: f32| (seconds * WAVE_STEPS).round() / WAVE_STEPS;let (source_start, duration) = (step(clip.source_start as f32), step(clip.duration as f32));let gain = clip.volume as f32;- let key = format!(- "{}|{:.3}|{:.3}|{:.3}",- clip.media_id, source_start, duration, gain- );+ let key = format!("{art}|{source_start:.3}|{duration:.3}|{gain:.3}");if let Some(cached) = self.waves.borrow().get(&key) {return cached.clone();}@@ -2961,6 +3068,16 @@ impl Studio {let Some(id) = self.sole_selection() else {return;};+ // The media's tracks, read before the echo is borrowed: a row of+ // the Audio panel's list is a stream index of the file.+ let audio_tracks: Vec<u32> = if field == ClipField::AudioTrack {+ self.clip(&id)+ .and_then(|clip| self.project().media_by_id(&clip.media_id))+ .map(|item| item.audio_tracks.iter().map(|track| track.index).collect())+ .unwrap_or_default()+ } else {+ Vec::new()+ };self.begin_echo();let value = f64::from(value);let Some(clip) = self.echo_clip_mut(&id) else {@@ -2969,6 +3086,13 @@ impl Studio {let text = clip.text.get_or_insert_with(TextStyle::default);match field {ClipField::Scale => clip.scale = value.clamp(0.05, 8.0),+ ClipField::AudioTrack => {+ // The first row is the file's default and is stored as such,+ // so a clip on the first track saves as every clip did before+ // there were tracks to choose.+ let row = value.max(0.0) as usize;+ clip.audio_stream = (row > 0).then(|| audio_tracks.get(row).copied()).flatten();+ }ClipField::StretchX => clip.stretch_x = value.clamp(0.1, 10.0),ClipField::StretchY => clip.stretch_y = value.clamp(0.1, 10.0),ClipField::OffsetX => clip.offset_x = value.clamp(-1.0, 1.0),@@ -3237,6 +3361,9 @@ impl Studio {if after.preserve_pitch != before.preserve_pitch {patch.preserve_pitch = Some(after.preserve_pitch);}+ if after.audio_stream != before.audio_stream {+ patch.audio_stream = Some(after.audio_stream);+ }if after.reverse != before.reverse {patch.reverse = Some(after.reverse);}@@ -3341,7 +3468,7 @@ impl Studio {let painted = (clip.kind == model::ClipKind::Text).then(|| self.title_blocks.get(&clip.id).copied()).flatten();- let (w, h) = if let Some((bw, bh)) = painted {+ let (w, h) = if let Some(((bw, bh), _)) = painted {// The painter said how big the block came out.(f64::from(bw) * clip.scale / width,@@ -3414,9 +3541,25 @@ impl Studio {_ => base,};let factor = placed.scale / clip.scale.max(1e-6);+ // A title anchored on an edge paints its block beside the clip's+ // position, not on it; the box goes where the block is. The offset+ // is canvas pixels, so it scales, stretches and turns with the clip+ // before it becomes a fraction of the frame.+ let (ax, ay) = match painted {+ Some((_, (dx, dy))) if dx != 0 || dy != 0 => {+ let px = f64::from(dx) * placed.scale * clip.stretch_x;+ let py = f64::from(dy) * placed.scale * clip.stretch_y;+ let (sin, cos) = placed.rotation.to_radians().sin_cos();+ (+ (px * cos - py * sin) / width,+ (px * sin + py * cos) / height,+ )+ }+ _ => (0.0, 0.0),+ };Footprint {- cx: 0.5 + placed.offset_x,- cy: 0.5 + placed.offset_y,+ cx: 0.5 + placed.offset_x + ax,+ cy: 0.5 + placed.offset_y + ay,w: w * factor,h: h * factor,rotation: placed.rotation,@@ -3951,9 +4094,7 @@ impl Studio {// One analysis per media and subject, keyed the way the job map is.let wanted: Vec<(String, AnalyseRequest)> = Cutouts::requests(self.project(), &project).into_iter()- .map(|(media_id, request)| {- (Self::analysis_key(&media_id, request.subject), request)- })+ .map(|(media_id, request)| (Self::analysis_key(&media_id, request.subject), request)).collect();let Some((id, request)) = wanted.into_iter()@@ -4487,9 +4628,9 @@ impl Studio {self.start.error.clear();self.recents = projects::list(&self.host.dirs.config);self.host.monitor.clear();- self.audition = None;- self.revision += 1;- self.flat = None;+ self.audition = None;+ self.revision += 1;+ self.flat = None;self.sync_audio();self.request_media_art();self.request_preview();@@ -4954,6 +5095,7 @@ impl Studio {let font_size = CAPTION_SIZES[self.captions.size.min(2)];let request = concat_speech::transcribe::TranscribeRequest {path: media.path.clone(),+ audio_stream: clip.audio_stream,source_start: clip.source_start,window: clip.duration * clip.speed,language: language.to_owned(),@@ -5208,7 +5350,8 @@ impl Studio {/// shader that reads it - with a card still rendered through the table/// here, and the catalogue is rebuilt so the Filters page shows them.pub fn import_lut(&mut self) {- let Some(paths) = crate::platform::pick_files(&t("Import LUT"), Some((t("LUT").as_str(), &["cube"])))+ let Some(paths) =+ crate::platform::pick_files(&t("Import LUT"), Some((t("LUT").as_str(), &["cube"])))else {return;};@@ -5221,7 +5364,10 @@ impl Studio {imported += 1;}Err(error) => {- self.notify(&tf("Could not import {0}: {1}", &[&path.display(), &error]), true);+ self.notify(+ &tf("Could not import {0}: {1}", &[&path.display(), &error]),+ true,+ );}}}@@ -5233,7 +5379,10 @@ impl Studio {}self.library[0].query.clear();self.notify(- &tf("Imported {0} look(s); find them under Imported", &[&imported]),+ &tf(+ "Imported {0} look(s); find them under Imported",+ &[&imported],+ ),false,);}@@ -5433,6 +5582,18 @@ impl Studio {});editor.set_selected_clip(self.selected());+ // Written only when it differs: a fresh model is a change to every+ // binding that reads it, and this one is read on every publish.+ let labels = self.audio_track_labels();+ let shown = editor.get_audio_tracks();+ let same = shown.row_count() == labels.len()+ && labels+ .iter()+ .enumerate()+ .all(|(row, label)| shown.row_data(row).as_ref() == Some(label));+ if !same {+ editor.set_audio_tracks(slint::ModelRc::new(VecModel::from(labels)));+ }// The keyframe cluster's state, on its own global: every keyable row// in the inspector reads it, and none of them is threaded to.let keys = app.global::<Keyframes>();@@ -5692,6 +5853,26 @@ impl Studio {self.seek((start + target * duration) as f32);}+ /// The audio tracks the Audio panel offers for the selection: one label+ /// per track of the selected clip's media when it has more than one,+ /// else nothing - a file with one track has nothing to choose.+ fn audio_track_labels(&self) -> Vec<SharedString> {+ let Some(item) = self+ .sole_selection()+ .and_then(|id| self.clip(&id))+ .and_then(|clip| self.project().media_by_id(&clip.media_id))+ else {+ return Vec::new();+ };+ if item.audio_tracks.len() < 2 {+ return Vec::new();+ }+ item.audio_tracks+ .iter()+ .enumerate()+ .map(|(position, track)| audio_track_label(track, position).into())+ .collect()+ }/// The selection, flattened for the inspector: exactly one clip or/// nothing.@@ -5722,6 +5903,11 @@ impl Studio {stretch_y: clip.stretch_y as f32,opacity: clip.opacity as f32,volume: clip.volume as f32,+ audio_track: self+ .project()+ .media_by_id(&clip.media_id)+ .map_or(0, |item| item.audio_track_position(clip.audio_stream))+ as i32,speed: clip.speed as f32,preserve_pitch: clip.preserve_pitch,speed_curve: match &clip.speed_curve {@@ -5956,13 +6142,16 @@ impl Studio {favourites: starred.clone(),};if self.shelf_stamp.borrow().as_ref() != Some(&stamp) {- let (groups, entries) = shelves(SHELF_KINDS[0], &self.library[0], starred, &self.look_art);+ let (groups, entries) =+ shelves(SHELF_KINDS[0], &self.library[0], starred, &self.look_art);sync(&models.filter_groups, groups);sync(&models.catalogue_filters, entries);- let (groups, entries) = shelves(SHELF_KINDS[1], &self.library[1], starred, &self.look_art);+ let (groups, entries) =+ shelves(SHELF_KINDS[1], &self.library[1], starred, &self.look_art);sync(&models.effect_groups, groups);sync(&models.catalogue_effects, entries);- let (groups, entries) = shelves(SHELF_KINDS[2], &self.library[2], starred, &self.look_art);+ let (groups, entries) =+ shelves(SHELF_KINDS[2], &self.library[2], starred, &self.look_art);sync(&models.audio_groups, groups);sync(&models.catalogue_audio, entries);*self.shelf_stamp.borrow_mut() = Some(stamp);diff --git a/engine/crates/concat/ui/app.slint b/engine/crates/concat/ui/app.slintindex 9836d87a..bf380414 100644--- a/engine/crates/concat/ui/app.slint+++ b/engine/crates/concat/ui/app.slint@@ -18,7 +18,7 @@ import "fonts/Synonym-Medium.ttf";import "fonts/Synonym-SemiBold.ttf";import { Theme } from "theme/theme.slint";-import { Bezier, Curves, Fmt, Payload } from "util.slint";+import { Bezier, Curves, Fmt, Focus, Payload } from "util.slint";import { EffectData } from "demo/effects-panel.slint";import { MediaFilter, MediaItemData, MediaKind, TextPresetData } from "workspace/media-pane.slint";import { ContextMenu, MenuItemData, MenuRow } from "primitives/context-menu.slint";@@ -47,7 +47,7 @@ import {// Re-exported so main.rs can reach the globals and the struct.export {- Curves, Fmt, Payload, Theme, Bezier, Editor, I18n,+ Curves, Fmt, Focus, Payload, Theme, Bezier, Editor, I18n,AppliedEntryData, AppliedParamData, CatalogueEntryData, Library, LibraryViewData, ParamFormat,DockDivider, DockSide, PaneKind, SeatBox,EffectData, MediaFilter, MediaItemData, MediaKind, TextPresetData,@@ -77,6 +77,27 @@ export component App inherits Window {// delivered from until the first press landed somewhere.forward-focus: keys;+ // The same hole opens again later, and `forward-focus` does not close+ // it: it is read once, when the window first focuses. Slint delivers a+ // key to the focused item and its ancestors, and when that item has+ // been torn down - the launch screen's name field once a project opens,+ // a sheet's field once the sheet closes - there is no item and the key+ // is dropped. Nothing walks up to `keys`, so every shortcut is dead+ // until a press lands on a pane's floor. Each of those teardowns hands+ // the focus back here on the way out.+ changed on-start => {+ if (!root.on-start) {+ keys.focus();+ }+ }+ property <bool> sheet-open: root.export.open || root.settings.open+ || root.project-sheet.open || root.captions.open || root.speech.open;+ changed sheet-open => {+ if (!root.sheet-open) {+ keys.focus();+ }+ }+// The editor's own state and actions live on the Editor global rather// than here — see editor.slint. What is left on the window is the window:// its title bar, its menus, and the two sheets that cover everything.diff --git a/engine/crates/concat/ui/assets/effect-previews/chromatic-aberration.jpg b/engine/crates/concat/ui/assets/effect-previews/chromatic-aberration.jpgnew file mode 100644index 00000000..4f2b8ab0Binary files /dev/null and b/engine/crates/concat/ui/assets/effect-previews/chromatic-aberration.jpg differdiff --git a/engine/crates/concat/ui/assets/effect-previews/emboss.jpg b/engine/crates/concat/ui/assets/effect-previews/emboss.jpgnew file mode 100644index 00000000..98ff1da7Binary files /dev/null and b/engine/crates/concat/ui/assets/effect-previews/emboss.jpg differdiff --git a/engine/crates/concat/ui/assets/effect-previews/hue-shift.jpg b/engine/crates/concat/ui/assets/effect-previews/hue-shift.jpgnew file mode 100644index 00000000..79aedab1Binary files /dev/null and b/engine/crates/concat/ui/assets/effect-previews/hue-shift.jpg differdiff --git a/engine/crates/concat/ui/assets/effect-previews/scanlines.jpg b/engine/crates/concat/ui/assets/effect-previews/scanlines.jpgnew file mode 100644index 00000000..4e61944aBinary files /dev/null and b/engine/crates/concat/ui/assets/effect-previews/scanlines.jpg differdiff --git a/engine/crates/concat/ui/assets/effect-previews/sketch.jpg b/engine/crates/concat/ui/assets/effect-previews/sketch.jpgnew file mode 100644index 00000000..523b559aBinary files /dev/null and b/engine/crates/concat/ui/assets/effect-previews/sketch.jpg differdiff --git a/engine/crates/concat/ui/assets/effect-previews/solarize.jpg b/engine/crates/concat/ui/assets/effect-previews/solarize.jpgnew file mode 100644index 00000000..15c5eb50Binary files /dev/null and b/engine/crates/concat/ui/assets/effect-previews/solarize.jpg differdiff --git a/engine/crates/concat/ui/assets/effect-previews/threshold.jpg b/engine/crates/concat/ui/assets/effect-previews/threshold.jpgnew file mode 100644index 00000000..5c997eacBinary files /dev/null and b/engine/crates/concat/ui/assets/effect-previews/threshold.jpg differdiff --git a/engine/crates/concat/ui/demo/interpolation-panel.slint b/engine/crates/concat/ui/demo/interpolation-panel.slintindex d6b6d629..e7209c75 100644--- a/engine/crates/concat/ui/demo/interpolation-panel.slint+++ b/engine/crates/concat/ui/demo/interpolation-panel.slint@@ -2,7 +2,7 @@// SPDX-FileCopyrightText: 2026 Jareer and Concat contributorsimport {- Theme, Bezier, Curves, Fmt, BezierEditor, NumberField, Row,+ Theme, Bezier, Curves, Fmt, Focus, BezierEditor, NumberField, Row,Section, SectionBody, SectionHeader, SegmentedControl, Select, Stepper, FocusRing,} from "../primitives/primitives.slint";import { AlignToolbar, EditorHeader } from "editor-chrome.slint";@@ -104,11 +104,11 @@ component BezierField inherits Rectangle {self.text = root.value;}}- accepted => { self.clear-focus(); }+ accepted => { Focus.release(); }key-pressed(event) => {if (event.text == Key.Escape) {self.text = root.value;- self.clear-focus();+ Focus.release();return EventResult.accept;}return EventResult.reject;diff --git a/engine/crates/concat/ui/editor.slint b/engine/crates/concat/ui/editor.slintindex 066fb4fb..e3b1cf71 100644--- a/engine/crates/concat/ui/editor.slint+++ b/engine/crates/concat/ui/editor.slint@@ -109,6 +109,12 @@ export global Editor {// ── the inspector ──in-out property <SelectedClipData> selected-clip;+ /// The selected clip's media's audio tracks, one label each, in file+ /// order - "Track 1 · stereo", "Mic/Aux · mono". Empty unless the file+ /// has more than one, which is when the Audio panel offers the choice.+ /// Beside `selected-clip` rather than inside it: a list in a struct is a+ /// fresh model on every publish, and every panel would re-seed for it.+ in-out property <[string]> audio-tracks;callback clip-set(field: ClipField, value: float);callback clip-set-text(field: ClipTextField, value: string);callback clip-set-colour(field: ClipTextField, value: color);diff --git a/engine/crates/concat/ui/inspector/clip-inspector.slint b/engine/crates/concat/ui/inspector/clip-inspector.slintindex b53668ba..b4d1f886 100644--- a/engine/crates/concat/ui/inspector/clip-inspector.slint+++ b/engine/crates/concat/ui/inspector/clip-inspector.slint@@ -1323,6 +1323,12 @@ export component VideoPanel inherits VerticalLayout {/// ear works and how anyone mixing thinks.export component AudioPanel inherits VerticalLayout {in property <SelectedClipData> selected;+ /// The media's audio tracks, one label each. A recording that kept the+ /// desktop and the microphone apart has two, and gets a row to choose+ /// which one this clip plays; a file with one has nothing to choose and+ /// shows no row.+ in property <[string]> audio-tracks;+ property <bool> choosing: root.audio-tracks.length > 1;callback set(field: ClipField, value: float);callback commit();@@ -1343,8 +1349,32 @@ export component AudioPanel inherits VerticalLayout {init => { root.seed(); }changed selected => { root.seed(); }- Section {+ if root.choosing: Section {first: true;+ source := SectionHeader {+ title: I18n.t("Source");+ collapsible: true;+ summary: (root.selected.audio-track >= 0+ && root.selected.audio-track < root.audio-tracks.length)+ ? root.audio-tracks[root.selected.audio-track] : "";+ }+ if source.open: SectionBody {+ Row {+ label: I18n.t("Audio track");+ Select {+ options: root.audio-tracks;+ current: root.selected.audio-track;+ changed(index) => {+ root.set(ClipField.audio-track, index);+ root.commit();+ }+ }+ }+ }+ }++ Section {+ first: !root.choosing;volume := SectionHeader {title: I18n.t("Volume");collapsible: true;diff --git a/engine/crates/concat/ui/primitives/number-field.slint b/engine/crates/concat/ui/primitives/number-field.slintindex 9b08df6b..810a01f5 100644--- a/engine/crates/concat/ui/primitives/number-field.slint+++ b/engine/crates/concat/ui/primitives/number-field.slint@@ -2,7 +2,7 @@// SPDX-FileCopyrightText: 2026 Jareer and Concat contributorsimport { Theme } from "../theme/theme.slint";-import { Fmt } from "../util.slint";+import { Fmt, Focus } from "../util.slint";import { Glyph, Icon } from "../icons.slint";import { FocusRing } from "focus-ring.slint";@@ -130,14 +130,14 @@ export component NumberField inherits Rectangle {}accepted => {- self.clear-focus();+ Focus.release();}key-pressed(event) => {if (event.text == Key.Escape) {// put the untouched display back, so the blur commits a no-opself.text = root.display;- self.clear-focus();+ Focus.release();return EventResult.accept;}if (event.text == Key.UpArrow || event.text == Key.DownArrow) {diff --git a/engine/crates/concat/ui/primitives/primitives.slint b/engine/crates/concat/ui/primitives/primitives.slintindex f59355d6..ee3cbe83 100644--- a/engine/crates/concat/ui/primitives/primitives.slint+++ b/engine/crates/concat/ui/primitives/primitives.slint@@ -16,6 +16,6 @@ export { Modal, Button, Progress, DialogSection, DialogRow, Field } from "modal.export { ThinScrollbar } from "scrollbar.slint";export { Glyph, Icon } from "../icons.slint";export { Theme } from "../theme/theme.slint";-export { Fmt, Curves, Geom, Bezier, Payload } from "../util.slint";+export { Fmt, Curves, Focus, Geom, Bezier, Payload } from "../util.slint";export { Tip, Tips, TooltipLayer } from "tooltip.slint";export { ToastData, ToastLayer } from "toast.slint";diff --git a/engine/crates/concat/ui/timeline/model.slint b/engine/crates/concat/ui/timeline/model.slintindex 37c2aee0..613fde46 100644--- a/engine/crates/concat/ui/timeline/model.slint+++ b/engine/crates/concat/ui/timeline/model.slint@@ -157,6 +157,9 @@ export enum ClipField {font-size, font-weight, italic, text-opacity, align,stroke-width, shadow, line-height, tracking,cutout-feather,+ /// which of the media's audio tracks the clip plays, as a row of+ /// Editor.audio-tracks+ audio-track,}/// One keyable property's state at the playhead, as the inspector needs it.@@ -261,6 +264,10 @@ export struct SelectedClipData {// sound — everything but a still/// linear gain; the fader works in decibels and convertsvolume: float,+ /// which of the media's audio tracks plays, as a row of+ /// Editor.audio-tracks; 0 is the file's first, and the only row a file+ /// with one track has+ audio-track: int,speed: float,preserve-pitch: bool,/// Which preset shape the speed curve is: -1 for a constant rate, thediff --git a/engine/crates/concat/ui/timeline/tabs.slint b/engine/crates/concat/ui/timeline/tabs.slintindex 8a7758a3..900a78a8 100644--- a/engine/crates/concat/ui/timeline/tabs.slint+++ b/engine/crates/concat/ui/timeline/tabs.slint@@ -2,7 +2,7 @@// SPDX-FileCopyrightText: 2026 Jareer and Concat contributorsimport { Theme } from "../theme/theme.slint";-import { Payload } from "../util.slint";+import { Focus, Payload } from "../util.slint";import { Glyph, Icon } from "../icons.slint";import { TimelineTabData } from "model.slint";@@ -161,13 +161,13 @@ component Tab inherits Rectangle {}accepted => {- self.clear-focus();+ Focus.release();}key-pressed(event) => {if (event.text == Key.Escape) {self.text = root.name;- self.clear-focus();+ Focus.release();return EventResult.accept;}return EventResult.reject;diff --git a/engine/crates/concat/ui/timeline/track-header.slint b/engine/crates/concat/ui/timeline/track-header.slintindex d41f6c03..42f4acdb 100644--- a/engine/crates/concat/ui/timeline/track-header.slint+++ b/engine/crates/concat/ui/timeline/track-header.slint@@ -3,6 +3,7 @@import { I18n } from "../i18n.slint";import { Theme } from "../theme/theme.slint";+import { Focus } from "../util.slint";import { Glyph, Icon } from "../icons.slint";import { IconButton } from "../primitives/panel.slint";import { ThinScrollbar } from "../primitives/scrollbar.slint";@@ -213,12 +214,12 @@ component Header inherits Rectangle {}}- accepted => { self.clear-focus(); }+ accepted => { Focus.release(); }key-pressed(event) => {if (event.text == Key.Escape) {self.text = root.track.name;- self.clear-focus();+ Focus.release();return EventResult.accept;}return EventResult.reject;diff --git a/engine/crates/concat/ui/util.slint b/engine/crates/concat/ui/util.slintindex a87090b5..4e4ead81 100644--- a/engine/crates/concat/ui/util.slint+++ b/engine/crates/concat/ui/util.slint@@ -1,7 +1,7 @@// SPDX-License-Identifier: AGPL-3.0-or-later// SPDX-FileCopyrightText: 2026 Jareer and Concat contributors-// Small numeric and string helpers shared by the primitives, plus the two+// Small numeric and string helpers shared by the primitives, plus the// globals whose bodies live in Rust.export struct Bezier {@@ -136,3 +136,17 @@ export global Geom {return cy + radius * Math.sin(degrees * 1deg);}}++/// Where focus goes when a field is done with it.+///+/// Slint delivers a key to the focused item and its ancestors, and to+/// nothing at all when there is no focused item: `clear-focus()` leaves the+/// window in exactly that state, and so does tearing down whatever had the+/// focus. Every shortcut in the window then stops working until a press+/// lands somewhere that takes focus - which reads as "the keybindings are+/// broken". A field that is finished being typed into calls `release`+/// instead, and the window answers by focusing the scope its shortcuts live+/// on (App.blur), so the next keystroke has somewhere to go.+export global Focus {+ callback release();+}diff --git a/engine/crates/concat/ui/workspace/config-pane.slint b/engine/crates/concat/ui/workspace/config-pane.slintindex 4004d503..3e060d53 100644--- a/engine/crates/concat/ui/workspace/config-pane.slint+++ b/engine/crates/concat/ui/workspace/config-pane.slint@@ -99,6 +99,8 @@ export component ConfigPane inherits Rectangle {// ── details ──────────────────────────────────────────────────────────/// The selected clip, or `present: false` for the project details.in property <SelectedClipData> selected;+ /// The selected clip's media's audio tracks; see Editor.audio-tracks.+ in property <[string]> audio-tracks;property <bool> has-selection: root.selected.present;in property <string> project-name: I18n.t("Untitled project");in property <string> project-folder: "—";@@ -563,6 +565,7 @@ export component ConfigPane inherits Rectangle {if root.showing == "Sound": VerticalLayout {AudioPanel {selected: root.selected;+ audio-tracks: root.audio-tracks;set(field, value) => { root.clip-set(field, value); }commit => { root.clip-commit(); }}diff --git a/engine/crates/concat/ui/workspace/media-pane.slint b/engine/crates/concat/ui/workspace/media-pane.slintindex c8fdc880..6c33b002 100644--- a/engine/crates/concat/ui/workspace/media-pane.slint+++ b/engine/crates/concat/ui/workspace/media-pane.slint@@ -719,8 +719,8 @@ struct CatalogueItemData {id: string,label: string,/// False keeps the card browsable with a "Soon" badge, the honest version- /// of a disabled control, used by the motion transitions the engine does- /// not animate.+ /// of a disabled control, for anything listed before the engine can do+ /// it.enabled: bool,/// The rendered preview, for effects; empty for transitions and filters,/// whose tiles carry a mark on a sunken ground instead.@@ -1374,6 +1374,13 @@ export component MediaPane inherits Rectangle {if (id == "concat.digicam") { return @image-url("../assets/effect-previews/digicam.jpg"); }if (id == "concat.marquee") { return @image-url("../assets/effect-previews/marquee.jpg"); }if (id == "concat.matcha") { return @image-url("../assets/effect-previews/matcha.jpg"); }+ if (id == "concat.hue-shift") { return @image-url("../assets/effect-previews/hue-shift.jpg"); }+ if (id == "concat.threshold") { return @image-url("../assets/effect-previews/threshold.jpg"); }+ if (id == "concat.chromatic-aberration") { return @image-url("../assets/effect-previews/chromatic-aberration.jpg"); }+ if (id == "concat.emboss") { return @image-url("../assets/effect-previews/emboss.jpg"); }+ if (id == "concat.sketch") { return @image-url("../assets/effect-previews/sketch.jpg"); }+ if (id == "concat.solarize") { return @image-url("../assets/effect-previews/solarize.jpg"); }+ if (id == "concat.scanlines") { return @image-url("../assets/effect-previews/scanlines.jpg"); }return root.no-art;}/// The category column. In-out, because it is dragged.@@ -1401,11 +1408,14 @@ export component MediaPane inherits Rectangle {{ id: "fade-black", label: "Fade to Black", enabled: true, glyph: Glyph.transition },{ id: "fade-white", label: "Fade to White", enabled: true, glyph: Glyph.transition },];+ // The engine resolves these in concat-export's `resolve_transitions`:+ // the slides and the zoom as animation keys the monitor plays too, the+ // wipes as a mask the export bakes and the monitor shows as a dissolve.property <[CatalogueItemData]> transitions-motion: [- { id: "wipe-left", label: "Wipe Left", enabled: false, glyph: Glyph.transition },- { id: "wipe-right", label: "Wipe Right", enabled: false, glyph: Glyph.transition },- { id: "push", label: "Push", enabled: false, glyph: Glyph.transition },- { id: "zoom", label: "Zoom", enabled: false, glyph: Glyph.transition },+ { id: "wipe-left", label: "Wipe Left", enabled: true, glyph: Glyph.transition },+ { id: "wipe-right", label: "Wipe Right", enabled: true, glyph: Glyph.transition },+ { id: "push", label: "Push", enabled: true, glyph: Glyph.transition },+ { id: "zoom", label: "Zoom", enabled: true, glyph: Glyph.transition },];function narrow(to: MediaFilter) {diff --git a/engine/crates/concat/ui/workspace/seat.slint b/engine/crates/concat/ui/workspace/seat.slintindex cd362afa..9e4e2389 100644--- a/engine/crates/concat/ui/workspace/seat.slint+++ b/engine/crates/concat/ui/workspace/seat.slint@@ -192,6 +192,7 @@ export component Seat inherits Rectangle {video-effects: Editor.video-effects;audio-effects: Editor.audio-effects;+ audio-tracks: Editor.audio-tracks;level: Editor.level;peak: Editor.peak;add-effect(audio) => { Editor.add-effect(audio); }diff --git a/scripts/locales.py b/scripts/locales.pyindex 4dc761c5..cb4271c6 100755--- a/scripts/locales.py+++ b/scripts/locales.py@@ -76,7 +76,7 @@ def write_inventory(inventory: set[str]) -> None:body[key] = keyLOCALES.mkdir(exist_ok=True)(LOCALES / "en.json").write_text(- json.dumps(body, ensure_ascii=False, indent=2) + "\n"+ json.dumps(body, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")