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
feat(effects): packages the community can write, check, share and load live
Jareer5a409fdd1976
diff --git a/src/crates/concat-cli/src/main.rs b/src/crates/concat-cli/src/main.rsindex 1949dc5e..4fbf8f5a 100644--- a/src/crates/concat-cli/src/main.rs+++ b/src/crates/concat-cli/src/main.rs@@ -10,12 +10,14 @@//! line, so a script in any language edits and exports a project; `serve`//! is the same API on a socket, for callers that are other processes.//! `preview` is how the window's effect cards get their pictures: one still-//! through one package at its defaults.+//! through one package at its defaults. `check` is what an author runs on+//! an effect package before sharing it: the same load the window does, and+//! its fixtures, with every fault named.use std::error::Error;use std::io::{BufRead, Write};use std::net::SocketAddr;-use std::path::PathBuf;+use std::path::{Path, PathBuf};use std::sync::Arc;use clap::{Parser, Subcommand};@@ -104,6 +106,16 @@ enum Command {#[arg(long, default_value_t = 180)]height: u32,},++ /// Check effect packages before sharing them: load each folder the way+ /// the window does, hold its id against the built-ins, run its+ /// fixtures, and name every fault. `path` is one package folder - the+ /// one with `effect.toml` in it - or a folder of them, such as the+ /// app's own effects folder. Exits non-zero when any package fails.+ Check {+ /// A package folder, or a folder of package folders.+ path: PathBuf,+ },}fn main() -> Result<(), Box<dyn Error>> {@@ -129,6 +141,7 @@ fn main() -> Result<(), Box<dyn Error>> {width,height,} => preview(&input, &output, &effect, width, height),+ Command::Check { path } => check(&path),}}@@ -249,6 +262,46 @@ fn preview(Ok(())}+/// Every package at `path` through the loader and its fixtures, one line+/// per package and one per fault. The built-ins are what the ids are held+/// against: a user package cannot take a name the window already knows.+fn check(path: &Path) -> Result<(), Box<dyn Error>> {+ let folders = if path.join("effect.toml").is_file() {+ vec![path.to_path_buf()]+ } else {+ let folders = concat_effects::package_folders(path)?;+ if folders.is_empty() {+ return Err(format!(+ "{}: no package here - a package is a folder with an effect.toml in it",+ path.display()+ )+ .into());+ }+ folders+ };+ let taken = concat_effects::Catalogue::builtin();+ let mut failed = 0;+ for folder in &folders {+ let problems = concat_effects::Package::check_folder(folder, taken);+ if problems.is_empty() {+ println!("ok {}", folder.display());+ } else {+ failed += 1;+ println!("FAIL {}", folder.display());+ for problem in problems {+ for line in problem.lines() {+ println!(" {line}");+ }+ }+ }+ }+ if failed > 0 {+ return Err(format!("{failed} of {} package(s) failed", folders.len()).into());+ }+ println!("{} package(s) checked", folders.len());+ Ok(())+}+fn probe(path: &PathBuf) -> Result<(), Box<dyn Error>> {let info = concat_media::probe(path)?;diff --git a/src/crates/concat-effects/src/catalogue.rs b/src/crates/concat-effects/src/catalogue.rsindex 5f0935a3..b45bdade 100644--- a/src/crates/concat-effects/src/catalogue.rs+++ b/src/crates/concat-effects/src/catalogue.rs@@ -9,7 +9,7 @@//! clip's applied effects into one FFmpeg chain.use std::collections::{BTreeMap, HashMap};-use std::path::Path;+use std::path::{Path, PathBuf};use std::sync::{Arc, RwLock};use concat_project::model::AppliedFilter;@@ -23,6 +23,10 @@ use crate::manifest::{Kind, Manifest};use crate::shader::{Shader, TransitionShader};use crate::template::Template;+/// The author every built-in package is under, and no other package may+/// be: the front of an id, before the dot.+pub const BUILTIN_AUTHOR: &str = "concat";+/// A compiled FFmpeg backend.#[derive(Clone, Debug)]struct Chain {@@ -237,6 +241,20 @@ impl Package {)));}}+ // A package is a folder anyone can share; its chain may+ // touch the frame and nothing else. See `filters`.+ for filter in template+ .filters()+ .map_err(|error| invalid(format!("chain: {error}")))?+ {+ if !crate::filters::allowed(&filter) {+ return Err(invalid(format!(+ "chain uses `{filter}`, which is not a filter a package may use: \+ a package's chain reads the frame and writes the frame, and \+ nothing else"+ )));+ }+ }Some(Chain { lets, template })}};@@ -519,7 +537,6 @@ impl Package {.map_err(|error| invalid(format!("chain: {error}")))}- /// Runs every fixture; returns one line per failure./// Loads a package from its folder: the manifest, and beside it the/// fixtures, the shader and the table, whichever the manifest names.pub fn from_folder(folder: &Path) -> Result<Package, Error> {@@ -533,6 +550,24 @@ impl Package {let fixtures = read("fixtures.toml").ok();let shader = read("effect.wgsl").ok();let manifest = Manifest::parse(&manifest_text)?;+ // The built-ins' author is theirs alone. A package of the user's+ // own under it would shadow, or be shadowed by, one that ships+ // with the app, and which won would turn on load order.+ let reserved = std::iter::once(manifest.effect.id.as_str())+ .chain(manifest.effect.aliases.iter().map(String::as_str))+ .find(|name| {+ name.split_once('.')+ .is_some_and(|(author, _)| author == BUILTIN_AUTHOR)+ });+ if let Some(name) = reserved {+ return Err(Error::Invalid {+ id: manifest.effect.id.clone(),+ message: format!(+ "`{name}` is under `{BUILTIN_AUTHOR}.`, the author of the packages that \+ ship with the app; a package of your own is `author.name`"+ ),+ });+ }let table = match &manifest.lut {Some(table) => {let text = read(&table.file)?;@@ -588,6 +623,84 @@ impl Package {}failures}++ /// Everything wrong with the package in `folder`, one line each, or+ /// nothing: the check an author runs before sharing a package, and+ /// what `concat-cli check` prints. The folder is loaded the way the+ /// window loads it, so a manifest, template or shader fault comes out+ /// here rather than in someone else's log; its id and aliases are held+ /// against `taken`, the catalogue it would join; and its fixtures run.+ pub fn check_folder(folder: &Path, taken: &Catalogue) -> Vec<String> {+ let package = match Package::from_folder(folder) {+ Ok(package) => package,+ Err(error) => return vec![error.to_string()],+ };+ let mut problems = Vec::new();+ let mut names = vec![package.id().to_owned()];+ names.extend(package.manifest.effect.aliases.iter().cloned());+ for name in names {+ if let Some(other) = taken.get(&name) {+ problems.push(format!(+ "{}: `{name}` is already taken by `{}`",+ package.id(),+ other.id()+ ));+ }+ }+ problems.extend(package.check_fixtures());+ problems+ }+}++/// A number that changes when anything in the package folders under `dir`+/// does: a folder added or taken away, a file in one written, added or+/// removed. What a watcher polls, cheaply - a stat per file, no reading -+/// to know when the folder is worth loading again. A missing `dir` is 0.+pub fn package_stamp(dir: &Path) -> u64 {+ use std::hash::{Hash, Hasher};+ let Ok(folders) = package_folders(dir) else {+ return 0;+ };+ let mut hasher = std::collections::hash_map::DefaultHasher::new();+ for folder in folders {+ folder.hash(&mut hasher);+ let Ok(entries) = std::fs::read_dir(&folder) else {+ continue;+ };+ let mut files: Vec<(std::ffi::OsString, u64, u128)> = entries+ .filter_map(Result::ok)+ .filter_map(|entry| {+ let meta = entry.metadata().ok()?;+ let modified = meta+ .modified()+ .ok()+ .and_then(|at| at.duration_since(std::time::UNIX_EPOCH).ok())+ .map_or(0, |since| since.as_nanos());+ Some((entry.file_name(), meta.len(), modified))+ })+ .collect();+ files.sort();+ files.hash(&mut hasher);+ }+ // Never the "missing" value by accident.+ hasher.finish().max(1)+}++/// The package folders directly under `dir`, sorted: every folder with an+/// `effect.toml` in it. Anything else there is left alone, so a stray file+/// or a folder of notes beside the packages costs nothing.+pub fn package_folders(dir: &Path) -> Result<Vec<PathBuf>, Error> {+ let entries = std::fs::read_dir(dir).map_err(|error| Error::Io {+ path: dir.to_path_buf(),+ message: error.to_string(),+ })?;+ let mut folders: Vec<PathBuf> = entries+ .filter_map(Result::ok)+ .map(|entry| entry.path())+ .filter(|path| path.join("effect.toml").is_file())+ .collect();+ folders.sort();+ Ok(folders)}/// A path as one FFmpeg option value: single-quoted, with the quote@@ -699,22 +812,11 @@ impl Catalogue {/// Loads every package folder directly under `dir`. A folder that fails/// is reported and skipped; the rest still load.pub fn load_dir(&mut self, dir: &Path) -> Vec<Error> {- let mut errors = Vec::new();- let entries = match std::fs::read_dir(dir) {- Ok(entries) => entries,- Err(error) => {- return vec![Error::Io {- path: dir.to_path_buf(),- message: error.to_string(),- }];- }+ let folders = match package_folders(dir) {+ Ok(folders) => folders,+ Err(error) => return vec![error],};- let mut folders: Vec<_> = entries- .filter_map(Result::ok)- .map(|entry| entry.path())- .filter(|path| path.join("effect.toml").is_file())- .collect();- folders.sort();+ let mut errors = Vec::new();for folder in folders {match Package::from_folder(&folder).and_then(|p| self.add(p)) {Ok(()) => {}diff --git a/src/crates/concat-effects/src/filters.rs b/src/crates/concat-effects/src/filters.rsnew file mode 100644index 00000000..6aa53830--- /dev/null+++ b/src/crates/concat-effects/src/filters.rs@@ -0,0 +1,238 @@+// SPDX-License-Identifier: AGPL-3.0-or-later+// SPDX-FileCopyrightText: 2026 Jareer and Concat contributors++//! The FFmpeg filters a package's chain may name.+//!+//! A chain runs inside the decoder's filtergraph with the process's own+//! rights, and FFmpeg has filters that reach past the frame: `movie` opens+//! any file, `drawtext` reads a text file and a font file, `frei0r`,+//! `ladspa` and `lv2` load plugins, `sendcmd` and `zmq` take commands,+//! `vidstabdetect` and `signature` write files, `arnndn` loads a model. A+//! package is a folder anyone can share, so its chain is held to this+//! list - filters that read the frame and write the frame - and one that+//! names anything else is refused at load, before it is ever run.+//!+//! The list is the union of what the built-ins use and the plain pixel+//! and sound filters an author is likely to reach for next. Adding one is+//! adding a line, in order; the test keeps the order.++/// Every filter a chain may use, sorted for [`allowed`]'s search.+pub const ALLOWED: &[&str] = &[+ // ── sound ──+ "acompressor",+ "acontrast",+ "acrusher",+ "adeclick",+ "adeclip",+ "adelay",+ "adenorm",+ "aecho",+ "aemphasis",+ "aexciter",+ "afade",+ "afftdn",+ "aformat",+ "afreqshift",+ "agate",+ "alimiter",+ "allpass",+ "anlmdn",+ "apad",+ "aphaser",+ "aphaseshift",+ "apulsator",+ "aresample",+ "asetrate",+ "asoftclip",+ "asubboost",+ "asubcut",+ "asupercut",+ "atempo",+ "atilt",+ "bandpass",+ "bandreject",+ "bass",+ "biquad",+ "bs2b",+ "chorus",+ "compand",+ "compensationdelay",+ "crossfeed",+ "crystalizer",+ "dcshift",+ "deesser",+ "dynaudnorm",+ "earwax",+ "equalizer",+ "extrastereo",+ "firequalizer",+ "flanger",+ "haas",+ "highpass",+ "highshelf",+ "loudnorm",+ "lowpass",+ "lowshelf",+ "pan",+ "rubberband",+ "speechnorm",+ "stereotools",+ "stereowiden",+ "superequalizer",+ "surround",+ "treble",+ "tremolo",+ "vibrato",+ "virtualbass",+ "volume",+ // ── picture ──+ "avgblur",+ "bilateral",+ "blend",+ "boxblur",+ "bwdif",+ "cas",+ "chromahold",+ "chromakey",+ "chromanr",+ "chromashift",+ "colorbalance",+ "colorchannelmixer",+ "colorcontrast",+ "colorcorrect",+ "colorhold",+ "colorize",+ "colorkey",+ "colorlevels",+ "colorspace",+ "colortemperature",+ "convolution",+ "crop",+ "curves",+ "dctdnoiz",+ "deband",+ "deblock",+ "deflicker",+ "dilation",+ "drawbox",+ "drawgrid",+ "edgedetect",+ "eq",+ "erosion",+ "exposure",+ "fade",+ "fftdnoiz",+ "fillborders",+ "format",+ "gblur",+ "geq",+ "gradfun",+ "hflip",+ "histeq",+ "hqdn3d",+ "hqx",+ "hstack",+ "hsvhold",+ "hsvkey",+ "hue",+ "huesaturation",+ "lagfun",+ "lenscorrection",+ "limiter",+ "lumakey",+ "lut",+ "lut1d",+ "lut3d",+ "lutrgb",+ "lutyuv",+ "median",+ "monochrome",+ "negate",+ "nlmeans",+ "noise",+ "normalize",+ "overlay",+ "pad",+ "perspective",+ "photosensitivity",+ "pixelize",+ "prewitt",+ "pseudocolor",+ "rgbashift",+ "roberts",+ "rotate",+ "sab",+ "scale",+ "selectivecolor",+ "shear",+ "shuffleplanes",+ "smartblur",+ "sobel",+ "split",+ "swapuv",+ "tblend",+ "tile",+ "tmix",+ "tonemap",+ "transpose",+ "unsharp",+ "vaguedenoiser",+ "vflip",+ "vibrance",+ "vignette",+ "vstack",+ "xbr",+ "zoompan",+];++/// Whether a chain may name this filter.+pub fn allowed(name: &str) -> bool {+ ALLOWED.binary_search(&name).is_ok()+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn the_list_is_sorted_and_free_of_repeats() {+ for pair in ALLOWED.windows(2) {+ assert!(pair[0] < pair[1], "`{}` before `{}`", pair[0], pair[1]);+ }+ }++ #[test]+ fn the_frame_is_the_only_thing_a_filter_may_touch() {+ for name in ["hue", "eq", "lut3d", "split", "blend", "volume", "atempo"] {+ assert!(allowed(name), "{name}");+ }+ for name in [+ "movie",+ "amovie",+ "drawtext",+ "frei0r",+ "ladspa",+ "lv2",+ "sendcmd",+ "asendcmd",+ "zmq",+ "azmq",+ "vidstabdetect",+ "vidstabtransform",+ "signature",+ "metadata",+ "ametadata",+ "arnndn",+ "subtitles",+ "ass",+ "ocr",+ "sr",+ "dnn_processing",+ "",+ "Hue",+ "hue ",+ ] {+ assert!(!allowed(name), "{name}");+ }+ }+}diff --git a/src/crates/concat-effects/src/lib.rs b/src/crates/concat-effects/src/lib.rsindex f21bab11..b0324c81 100644--- a/src/crates/concat-effects/src/lib.rs+++ b/src/crates/concat-effects/src/lib.rs@@ -21,6 +21,7 @@pub mod catalogue;pub mod cube;pub mod expr;+pub mod filters;pub mod manifest;pub mod shader;pub mod template;@@ -29,8 +30,8 @@ mod builtins {include!(concat!(env!("OUT_DIR"), "/builtins.rs"));}-pub use catalogue::{At, Catalogue, Fixture, Package};-pub use manifest::{Kind, Manifest, Param, ParamType};+pub use catalogue::{At, Catalogue, Fixture, Package, package_folders, package_stamp};+pub use manifest::{FORMAT, Kind, Manifest, Param, ParamType};pub use shader::{Shader, TransitionShader};/// Why a package could not be loaded.@@ -314,6 +315,36 @@ mod tests {let _ = std::fs::remove_dir_all(&dir);}+ #[test]+ fn a_chain_may_only_name_filters_that_touch_the_frame() {+ let with = |chain: &str| {+ Package::from_sources(+ &format!(+ "[effect]\nid = \"a.b\"\nname = \"B\"\nkind = \"effect\"\n[[param]]\nkey = \"hue\"\nlabel = \"Hue\"\nmax = 360\n[ffmpeg]\nchain = {chain:?}\n"+ ),+ None,+ None,+ )+ };+ with("hue=h={round(hue)},curves=all='0/0,1/1',negate").expect("plain filters load");+ with("split[a][b];[a]gblur=sigma=2[c];[b][c]blend=all_mode=screen").expect("graphs load");+ for (chain, needle) in [+ ("movie=/etc/passwd[m];[m]hue=h={hue}", "`movie`"),+ ("hue=h={hue},drawtext=textfile=/etc/passwd", "`drawtext`"),+ ("frei0r=filter_name=/tmp/evil.so,hue=h={hue}", "`frei0r`"),+ ("hue=h={hue},sendcmd=f=/tmp/cmd", "`sendcmd`"),+ (+ "hue=h={hue},vidstabdetect=result=/tmp/out",+ "`vidstabdetect`",+ ),+ ("{hue}=1", "spelt out"),+ ("hue=h={hue},", "no name"),+ ] {+ let error = with(chain).expect_err(chain).to_string();+ assert!(error.contains(needle), "{chain}: {error}");+ }+ }+#[test]fn a_duplicate_id_is_refused() {let mut catalogue = Catalogue::new();@@ -330,4 +361,185 @@ mod tests {assert_eq!(catalogue.video_chain(&[applied("a.b", &[])]), "negate");let _ = BTreeMap::<String, f64>::new();}++ /// A package folder `id` under a fresh temp dir, holding `files`.+ fn scratch(id: &str, files: &[(&str, &str)]) -> std::path::PathBuf {+ let dir = std::env::temp_dir().join(format!("concat-check-{}-{id}", std::process::id()));+ let _ = std::fs::remove_dir_all(&dir);+ let folder = dir.join(id);+ std::fs::create_dir_all(&folder).expect("mkdir");+ for (name, text) in files {+ std::fs::write(folder.join(name), text).expect("write");+ }+ folder+ }++ const TINT: &str = "[effect]\nid = \"alice.tint\"\nname = \"Tint\"\nkind = \"effect\"\n\+ [[param]]\nkey = \"hue\"\nlabel = \"Hue\"\nmax = 360\ndefault = 90\n\+ [ffmpeg]\nchain = \"hue=h={round(hue)}\"\n";++ #[test]+ fn a_sound_package_checks_clean() {+ let folder = scratch(+ "alice.tint",+ &[+ ("effect.toml", TINT),+ (+ "fixtures.toml",+ "[[case]]\nname = \"default\"\nchain = \"hue=h=90\"\n[[case]]\nat = \"max\"\nchain = \"hue=h=360\"\n",+ ),+ ],+ );+ let problems = Package::check_folder(&folder, Catalogue::builtin());+ assert!(problems.is_empty(), "{problems:?}");+ let _ = std::fs::remove_dir_all(folder.parent().unwrap());+ }++ #[test]+ fn check_folder_names_every_fault() {+ // A manifest that does not parse.+ let folder = scratch(+ "bob.broken",+ &[("effect.toml", "[effect]\nid = \"bob.broken\"\n")],+ );+ let problems = Package::check_folder(&folder, Catalogue::builtin());+ assert_eq!(problems.len(), 1, "{problems:?}");+ assert!(+ problems[0].contains("bob.broken") || problems[0].contains('?'),+ "{problems:?}"+ );+ let _ = std::fs::remove_dir_all(folder.parent().unwrap());++ // A fixture that pins a chain the template does not render.+ let folder = scratch(+ "carol.pinned",+ &[+ ("effect.toml", &TINT.replace("alice.tint", "carol.pinned")),+ (+ "fixtures.toml",+ "[[case]]\nname = \"wrong\"\nchain = \"hue=h=0\"\n",+ ),+ ],+ );+ let problems = Package::check_folder(&folder, Catalogue::builtin());+ assert_eq!(problems.len(), 1, "{problems:?}");+ assert!(+ problems[0].contains("wrong") && problems[0].contains("hue=h=90"),+ "{problems:?}"+ );+ let _ = std::fs::remove_dir_all(folder.parent().unwrap());++ // The built-ins' author, as an id and as an alias.+ let folder = scratch(+ "concat.sepia",+ &[("effect.toml", &TINT.replace("alice.tint", "concat.sepia"))],+ );+ let problems = Package::check_folder(&folder, Catalogue::builtin());+ assert_eq!(problems.len(), 1, "{problems:?}");+ assert!(problems[0].contains("author.name"), "{problems:?}");+ let _ = std::fs::remove_dir_all(folder.parent().unwrap());+ let folder = scratch(+ "frank.alias",+ &[(+ "effect.toml",+ &TINT.replace("alice.tint", "frank.alias").replace(+ "kind = \"effect\"\n",+ "kind = \"effect\"\naliases = [\"concat.sepia\"]\n",+ ),+ )],+ );+ let problems = Package::check_folder(&folder, Catalogue::builtin());+ assert_eq!(problems.len(), 1, "{problems:?}");+ assert!(problems[0].contains("concat.sepia"), "{problems:?}");+ let _ = std::fs::remove_dir_all(folder.parent().unwrap());++ // An id the catalogue it would join already answers to.+ let folder = scratch(+ "grace.twice",+ &[("effect.toml", &TINT.replace("alice.tint", "grace.twice"))],+ );+ let mut taken = Catalogue::new();+ assert!(taken.load_dir(folder.parent().unwrap()).is_empty());+ let problems = Package::check_folder(&folder, &taken);+ assert_eq!(problems.len(), 1, "{problems:?}");+ assert!(problems[0].contains("already taken"), "{problems:?}");+ let _ = std::fs::remove_dir_all(folder.parent().unwrap());++ // A shader that does not compile, found at load and not on the GPU.+ let folder = scratch(+ "dave.shady",+ &[+ (+ "effect.toml",+ "[effect]\nid = \"dave.shady\"\nname = \"Shady\"\nkind = \"effect\"\n[wgsl]\nentry = \"effect.wgsl\"\n",+ ),+ (+ "effect.wgsl",+ "fn effect(uv: vec2<f32>) -> vec4<f32> { return sample(uv) + ; }",+ ),+ ],+ );+ let problems = Package::check_folder(&folder, Catalogue::builtin());+ assert_eq!(problems.len(), 1, "{problems:?}");+ assert!(problems[0].contains("shader"), "{problems:?}");++ // A folder that is not there at all.+ let missing = folder.parent().unwrap().join("nobody.home");+ let problems = Package::check_folder(&missing, Catalogue::builtin());+ assert_eq!(problems.len(), 1, "{problems:?}");+ assert!(problems[0].contains("effect.toml"), "{problems:?}");+ let _ = std::fs::remove_dir_all(folder.parent().unwrap());+ }++ #[test]+ fn the_stamp_moves_with_the_folder_and_only_then() {+ let folder = scratch(+ "hank.still",+ &[("effect.toml", &TINT.replace("alice.tint", "hank.still"))],+ );+ let dir = folder.parent().unwrap().to_path_buf();+ assert_eq!(package_stamp(&dir.join("nowhere")), 0);+ let first = package_stamp(&dir);+ assert_ne!(first, 0);+ assert_eq!(package_stamp(&dir), first, "nothing changed");+ // A file in a package: its size changes even when the clock has+ // not ticked over.+ std::fs::write(+ folder.join("fixtures.toml"),+ "[[case]]\nchain = \"hue=h=90\"\n",+ )+ .expect("write");+ let second = package_stamp(&dir);+ assert_ne!(second, first, "a file was added");+ std::fs::write(+ folder.join("fixtures.toml"),+ "[[case]]\nchain = \"hue=h=90\"\n\n",+ )+ .expect("write");+ let third = package_stamp(&dir);+ assert_ne!(third, second, "a file grew");+ // Clutter beside the packages is not a package.+ std::fs::write(dir.join("notes.txt"), "x").expect("write");+ assert_eq!(package_stamp(&dir), third, "a file beside the packages");+ // A second package.+ std::fs::create_dir_all(dir.join("hank.other")).expect("mkdir");+ std::fs::write(dir.join("hank.other").join("effect.toml"), TINT).expect("write");+ assert_ne!(package_stamp(&dir), third, "a folder was added");+ let _ = std::fs::remove_dir_all(&dir);+ }++ #[test]+ fn package_folders_lists_only_folders_with_a_manifest() {+ let folder = scratch(+ "erin.one",+ &[("effect.toml", &TINT.replace("alice.tint", "erin.one"))],+ );+ let dir = folder.parent().unwrap().to_path_buf();+ std::fs::create_dir_all(dir.join("notes")).expect("mkdir");+ std::fs::write(dir.join("README.txt"), "not a package").expect("write");+ let folders = package_folders(&dir).expect("lists");+ assert_eq!(folders, vec![folder.clone()]);+ assert!(package_folders(&dir.join("nowhere")).is_err());+ let _ = std::fs::remove_dir_all(&dir);+ }}diff --git a/src/crates/concat-effects/src/manifest.rs b/src/crates/concat-effects/src/manifest.rsindex 44d246fc..f79049f7 100644--- a/src/crates/concat-effects/src/manifest.rs+++ b/src/crates/concat-effects/src/manifest.rs@@ -13,10 +13,22 @@ use serde::Deserialize;use crate::Error;+/// The package format this build reads: the shape of the manifest, the+/// template and expression syntax, and the shader's contract with the+/// compositor. Bumped when any of those changes in a way an older build+/// could not read, so a package written for a newer Concat says so rather+/// than failing in the shader compiler.+pub const FORMAT: u32 = 1;+/// A parsed `effect.toml`.#[derive(Deserialize, Clone, PartialEq, Debug)]#[serde(deny_unknown_fields)]pub struct Manifest {+ /// The package format the file is written for, `format = 1` at the top+ /// of the file, before the tables. Absent means 1, the first; see+ /// [`FORMAT`] for the one this build reads.+ #[serde(default = "one")]+ pub format: u32,/// Identity and placement.pub effect: Meta,/// The knobs, in the order the inspector shows them.@@ -283,6 +295,16 @@ impl Manifest {}fn validate(&self) -> Result<(), Error> {+ if self.format == 0 {+ return Err(self.invalid("format is 0; the first package format is 1"));+ }+ if self.format > FORMAT {+ return Err(self.invalid(format!(+ "written for package format {}, and this Concat reads up to {FORMAT}: \+ a newer Concat is needed",+ self.format+ )));+ }let id = &self.effect.id;let namespaced = id.split_once('.')@@ -499,6 +521,20 @@ mod tests {xfade = "fade""#;+ #[test]+ fn the_format_is_the_first_thing_read() {+ assert_eq!(Manifest::parse(GOOD).expect("parses").format, 1);+ assert_eq!(+ Manifest::parse(&format!("format = {FORMAT}\n{GOOD}"))+ .expect("parses")+ .format,+ FORMAT+ );+ rejects(&format!("format = {}\n{GOOD}", FORMAT + 1), "newer Concat");+ rejects(&format!("format = 0\n{GOOD}"), "format is 0");+ rejects(&format!("format = \"1\"\n{GOOD}"), "format");+ }+#[test]fn a_transition_manifest_parses() {let manifest = Manifest::parse(GOOD_TRANSITION).expect("parses");diff --git a/src/crates/concat-effects/src/template.rs b/src/crates/concat-effects/src/template.rsindex e9256ff6..11b998cb 100644--- a/src/crates/concat-effects/src/template.rs+++ b/src/crates/concat-effects/src/template.rs@@ -61,6 +61,98 @@ impl Template {Ok(Template { parts })}+ /// Every filter the chain names, in order, or why one cannot be told.+ ///+ /// A filtergraph is filters parted by `,` and `;`. Each is a run of+ /// `[labels]`, a name, `@instance` if it has one, and `=` with its+ /// options, then `[labels]` again for its outputs; a `'quoted'` option+ /// holds any character, and `\` escapes the one after it. A name has+ /// to be literal: a slot where a name goes would let a package pick at+ /// render time a filter it never showed at load, which is the one+ /// thing the check in [`crate::filters`] exists to stop.+ pub fn filters(&self) -> Result<Vec<String>, String> {+ #[derive(PartialEq, Clone, Copy)]+ enum At {+ Name,+ Options,+ }+ let mut names = Vec::new();+ let mut name = String::new();+ let mut at = At::Name;+ let mut quoted = false;+ let mut escaped = false;+ let mut labelled = false;+ let finish = |name: &mut String, names: &mut Vec<String>| -> Result<(), String> {+ if name.is_empty() {+ return Err("a filter with no name, from a stray `,` or `;`".to_owned());+ }+ names.push(std::mem::take(name));+ Ok(())+ };+ for part in &self.parts {+ let text = match part {+ Part::Slot(_) if at == At::Name && !labelled => {+ return Err(if name.is_empty() {+ "a `{slot}` where a filter's name goes: a name is spelt out".to_owned()+ } else {+ format!("a `{{slot}}` inside the filter name `{name}`: a name is spelt out")+ });+ }+ Part::Slot(_) => continue,+ Part::Literal(text) => text,+ };+ for c in text.chars() {+ if escaped {+ escaped = false;+ continue;+ }+ if quoted {+ quoted = c != '\'';+ continue;+ }+ if labelled {+ labelled = c != ']';+ continue;+ }+ match at {+ At::Name => match c {+ c if c.is_whitespace() => {}+ '[' if name.is_empty() => labelled = true,+ '[' => {+ finish(&mut name, &mut names)?;+ at = At::Options;+ labelled = true;+ }+ '=' | '@' => {+ finish(&mut name, &mut names)?;+ at = At::Options;+ }+ ',' | ';' => finish(&mut name, &mut names)?,+ c if c.is_ascii_alphanumeric() || c == '_' => name.push(c),+ c => return Err(format!("`{c}` is not part of a filter name")),+ },+ At::Options => match c {+ '\'' => quoted = true,+ '\\' => escaped = true,+ '[' => labelled = true,+ ',' | ';' => at = At::Name,+ _ => {}+ },+ }+ }+ }+ if quoted {+ return Err("an unclosed `'`".to_owned());+ }+ if labelled {+ return Err("an unclosed `[`".to_owned());+ }+ if at == At::Name {+ finish(&mut name, &mut names)?;+ }+ Ok(names)+ }+/// Every name any slot reads.pub fn names(&self, into: &mut Vec<String>) {for part in &self.parts {@@ -112,6 +204,60 @@ mod tests {assert_eq!(out, "trunc(val/64)*64");}+ fn filters_of(source: &str) -> Result<Vec<String>, String> {+ Template::parse(source).expect("parses").filters()+ }++ #[test]+ fn every_filter_in_a_chain_is_named() {+ assert_eq!(filters_of("negate").unwrap(), ["negate"]);+ assert_eq!(+ filters_of("hue=h={round(hue)},eq=brightness={b}:contrast=1").unwrap(),+ ["hue", "eq"]+ );+ // Labels before and after, an instance name, and a second graph.+ assert_eq!(+ filters_of("split[a][b];[a]gblur=sigma=2[c];[b][c]blend@mix=all_mode=screen").unwrap(),+ ["split", "gblur", "blend"]+ );+ // A comma inside a quoted option is not a part.+ assert_eq!(+ filters_of("curves=all='0/0,0.5/0.6,1/1',lutrgb=r='val*2'").unwrap(),+ ["curves", "lutrgb"]+ );+ // An escaped comma is not a part either, and nor is one in a slot.+ assert_eq!(+ filters_of("geq=lum='p(X\\,Y)':cb={fixed(a, 2)},negate").unwrap(),+ ["geq", "negate"]+ );+ // Whitespace and newlines between filters are nothing.+ assert_eq!(+ filters_of("hue=h=1,\n negate ,vflip").unwrap(),+ ["hue", "negate", "vflip"]+ );+ // The catalogue's own `{lut}` path is an option, not a name.+ assert_eq!(filters_of("lut3d=file={lut}").unwrap(), ["lut3d"]);+ }++ #[test]+ fn a_filter_name_is_spelt_out_or_the_chain_is_refused() {+ for (chain, needle) in [+ ("{name}=1", "spelt out"),+ ("hue=h=1,{name}", "spelt out"),+ ("hu{e}=h=1", "spelt out"),+ ("hue=h=1,", "no name"),+ (",hue", "no name"),+ ("hue=h=1;;negate", "no name"),+ ("hue=h='1", "unclosed `'`"),+ ("[in", "unclosed `[`"),+ ("hue-rotate=1", "not part"),+ ("$(rm -rf /)", "not part"),+ ] {+ let error = filters_of(chain).expect_err(chain);+ assert!(error.contains(needle), "{chain}: {error}");+ }+ }+#[test]fn malformed_templates_fail_at_parse() {assert!(Template::parse("gblur={radius").is_err());diff --git a/src/crates/concat/src/lib.rs b/src/crates/concat/src/lib.rsindex 5372e48a..d63707eb 100644--- a/src/crates/concat/src/lib.rs+++ b/src/crates/concat/src/lib.rs@@ -125,16 +125,16 @@ pub fn run() -> Result<(), slint::PlatformError> {}};- // The user's own packages - imported looks - sit beside the built-ins- // from the first frame. One that will not load is reported and skipped.- for error in concat_effects::Catalogue::install(&Studio::looks_dir(&host.dirs)) {- log::warn!("look: {error}");- }-let app = App::new()?;app.set_macos(platform::MACOS);- let studio = Studio::new(host);+ // The user's own packages - imported looks, and any effect folder they+ // or the community wrote - sit beside the built-ins from the first+ // frame. One that will not load is skipped, and its reason is the+ // first thing the window says.+ let mut studio = Studio::new(host);+ studio.reload_packages(false);+ studio.watch_packages();let dark = studio.prefs.dark.unwrap_or(true);app.global::<Theme>().set_dark(dark);@@ -560,6 +560,9 @@ pub fn run() -> Result<(), slint::PlatformError> {editor.on_library_import_lut(on_window!(|state| {state.import_lut();}));+ editor.on_library_reload_packages(on_window!(|state| {+ state.reload_packages(true);+ }));// ── the inspector's effect stacks ──editor.on_add_effect(on_window!(|state, _audio: bool| {diff --git a/src/crates/concat/src/studio.rs b/src/crates/concat/src/studio.rsindex abc54210..8db7180c 100644--- a/src/crates/concat/src/studio.rs+++ b/src/crates/concat/src/studio.rs@@ -35,7 +35,7 @@ use concat_host::playback::ClipSpec;use concat_host::{AnalyseRequest, Cutouts, ProjectInfo, RegionRequest, Session, media, projects, templates,};-use concat_media::{Pyramid, jpeg};+use concat_media::{DecodeOptions, Decoder, FrameSource, Pyramid, jpeg};use concat_project::commands::{ClipMove, ClipPatch, TrackFlag, TrimEdge};use concat_project::model::{self, AppliedFilter, Clip, Project, TextAlign, TextStyle, Timeline, Track, Transition,@@ -641,6 +641,16 @@ pub struct Studio {/// rebuilt on every publish and a picture read from disk each time/// would be the slowest thing in the window.look_art: std::cell::RefCell<HashMap<String, slint::Image>>,+ /// The watch on the user's effects folder: a poll every couple of+ /// seconds of what is in it, cheaper than a file-system watcher and+ /// one dependency fewer, at a cost nobody will notice.+ packages_watch: slint::Timer,+ /// The folder as it was when the catalogue was last built, and, once+ /// a change has been seen, the folder as it was at that sight: the+ /// catalogue is rebuilt only when two polls agree, so a package still+ /// being copied in is not loaded half-way.+ packages_seen: u64,+ packages_pending: Option<u64>,/// The last inspector commit: what it changed and when. A control that/// is dragged commits on every move, and each of those would be an undo/// step of its own; a commit that changes the same thing as the last@@ -801,7 +811,10 @@ fn shelves(.borrow_mut().entry(meta.id.clone()).or_insert_with(|| {- slint::Image::load_from_path(&folder.join("preview.png")).unwrap_or_default()+ // The author's own still, or the one rendered for them.+ slint::Image::load_from_path(&folder.join("preview.png"))+ .or_else(|_| slint::Image::load_from_path(&folder.join("preview.jpg")))+ .unwrap_or_default()}).clone(),None => slint::Image::default(),@@ -980,9 +993,10 @@ fn import_cube(dir: &std::path::Path, path: &std::path::Path) -> Result<String,std::fs::create_dir_all(&folder).map_err(|error| error.to_string())?;let name = stem.trim().to_owned();let manifest = format!(- "[effect]\nid = \"{id}\"\nname = {name:?}\nkind = \"filter\"\ncategory = \"Imported\"\n\+ "format = {}\n\n[effect]\nid = \"{id}\"\nname = {name:?}\nkind = \"filter\"\ncategory = \"Imported\"\n\description = \"A look imported from a .cube table.\"\n\n[lut]\nfile = \"look.cube\"\n\n\- [ffmpeg]\nchain = \"lut3d=file={{lut}}\"\n\n[wgsl]\nentry = \"effect.wgsl\"\n"+ [ffmpeg]\nchain = \"lut3d=file={{lut}}\"\n\n[wgsl]\nentry = \"effect.wgsl\"\n",+ concat_effects::FORMAT);std::fs::write(folder.join("effect.toml"), manifest).map_err(|error| error.to_string())?;std::fs::write(@@ -995,10 +1009,7 @@ fn import_cube(dir: &std::path::Path, path: &std::path::Path) -> Result<String,// The card still: the reference picture through the table, the same// arithmetic the GPU's sampler does. Read through Slint's decoder from// a copy on disk, since the bytes live in the binary.- let reference = dir.join("reference.jpg");- if !reference.is_file() {- std::fs::write(&reference, REFERENCE_STILL).map_err(|error| error.to_string())?;- }+ let reference = reference_still(dir)?;let image = slint::Image::load_from_path(&reference).map_err(|error| error.to_string())?;let Some(pixels) = image.to_rgba8() else {return Err("the reference picture would not decode".to_owned());@@ -1028,6 +1039,44 @@ fn import_cube(dir: &std::path::Path, path: &std::path::Path) -> Result<String,Ok(id)}+/// The reference picture every card is rendered from, as a file in the+/// effects folder: the bytes live in the binary, and both Slint's decoder+/// and FFmpeg's want a path.+fn reference_still(dir: &std::path::Path) -> Result<std::path::PathBuf, String> {+ let reference = dir.join("reference.jpg");+ if !reference.is_file() {+ std::fs::create_dir_all(dir).map_err(|error| error.to_string())?;+ std::fs::write(&reference, REFERENCE_STILL).map_err(|error| error.to_string())?;+ }+ Ok(reference)+}++/// The card still for a package of the user's own that brought none: the+/// reference picture through the package's own chain at its defaults,+/// written beside its manifest as `preview.jpg`, the way the built-ins'+/// cards were made. A package with a shader and no chain has no CPU+/// rendering to be had here, so its card stays blank until its author+/// puts a `preview.png` beside the manifest.+fn render_card(+ dir: &std::path::Path,+ package: &concat_effects::Package,+ chain: &str,+) -> Result<(), String> {+ let Some(folder) = package.folder.as_deref() else {+ return Ok(());+ };+ let reference = reference_still(dir)?;+ let mut decoder = Decoder::open(&reference, &DecodeOptions::default().scaled_to(320, 180))+ .map_err(|error| error.to_string())?;+ let frame = decoder+ .next_frame()+ .map_err(|error| error.to_string())?+ .ok_or_else(|| "the reference picture holds no frame".to_owned())?;+ let treated = concat_media::treat(&frame, chain).map_err(|error| error.to_string())?;+ let bytes = jpeg(&treated, 3).map_err(|error| error.to_string())?;+ std::fs::write(folder.join("preview.jpg"), bytes).map_err(|error| error.to_string())+}+/// The ease a key put on at `at` inherits: that of whichever key it joins/// behind, so laying a run of keys down does not alternate between shapes./// The first key on a parameter has nothing to inherit and gets the@@ -1263,6 +1312,9 @@ impl Studio {commit_timer: slint::Timer::default(),shelf_stamp: std::cell::RefCell::new(None),look_art: std::cell::RefCell::new(HashMap::new()),+ packages_watch: slint::Timer::default(),+ packages_seen: 0,+ packages_pending: None,last_commit: None,title_blocks: HashMap::new(),drop: None,@@ -5025,6 +5077,111 @@ impl Studio {dirs.config.join("effects")}+ /// Rebuilds the catalogue from the built-ins and the packages in+ /// `looks_dir`, and says in the window what would not load: the first+ /// reason and how many more, with every reason in the log, so an+ /// author sees a manifest or shader fault where they are looking+ /// rather than in a file they may not know exists. Returns how many+ /// failed. With `announce`, a clean reload is reported too, with the+ /// folder, which is how a newcomer learns where packages go.+ pub fn reload_packages(&mut self, announce: bool) -> usize {+ let dir = Self::looks_dir(&self.host.dirs);+ let errors = Catalogue::install(&dir);+ for error in &errors {+ log::warn!("package: {error}");+ }+ // A card for every package of the user's own that has none and+ // can have one. A card that fails is a line in the log, not a+ // notice: the package itself loaded.+ let catalogue = Catalogue::builtin();+ for package in catalogue.packages() {+ let Some(folder) = package.folder.as_deref() else {+ continue;+ };+ if !package.kind().is_visual()+ || package.kind() == PackageKind::Transition+ || folder.join("preview.png").is_file()+ || folder.join("preview.jpg").is_file()+ {+ continue;+ }+ let chain = catalogue.video_chain(&[AppliedFilter::new(package.id().to_owned())]);+ if chain.is_empty() {+ continue;+ }+ if let Err(error) = render_card(&dir, package, &chain) {+ log::warn!("package {}: card: {error}", package.id());+ }+ }+ // A changed shader means a changed card: the stills are read again.+ self.look_art.borrow_mut().clear();+ // The folder as the watch will next see it, cards included, so a+ // reload does not read as a change and set off another.+ self.packages_seen = concat_effects::package_stamp(&dir);+ self.packages_pending = None;+ if let Some(first) = errors.first() {+ let message = if errors.len() == 1 {+ tf("A custom package did not load: {0}", &[first])+ } else {+ tf(+ "{0} custom packages did not load; the first: {1}",+ &[&errors.len(), first],+ )+ };+ self.notify(&message, true);+ } else if announce {+ let count = Catalogue::builtin()+ .packages()+ .filter(|package| package.folder.is_some())+ .count();+ self.notify(+ &tf(+ "Loaded {0} custom package(s) from {1}",+ &[&count, &dir.display()],+ ),+ false,+ );+ }+ errors.len()+ }++ /// Starts the watch on the effects folder: a poll every two seconds,+ /// and a reload once two polls in a row see the same changed folder.+ /// A package written or dropped in while the app runs then shows up on+ /// its own, and one that will not load says why, without a restart or+ /// the button.+ pub fn watch_packages(&mut self) {+ self.packages_watch.start(+ slint::TimerMode::Repeated,+ std::time::Duration::from_secs(2),+ || {+ crate::host::Shell::with(|shell, app| {+ let changed = shell.studio.borrow_mut().poll_packages();+ if changed {+ shell.studio.borrow_mut().refresh_art();+ shell.studio.borrow().publish(&app, &shell.models);+ }+ });+ },+ );+ }++ /// One tick of the watch: whether the catalogue was rebuilt.+ fn poll_packages(&mut self) -> bool {+ let now = concat_effects::package_stamp(&Self::looks_dir(&self.host.dirs));+ if now == self.packages_seen {+ self.packages_pending = None;+ return false;+ }+ if self.packages_pending != Some(now) {+ // Seen once; a copy may still be in progress. Next tick decides.+ self.packages_pending = Some(now);+ return false;+ }+ self.reload_packages(true);+ true+ }+/// Imports one or more `.cube` tables as looks: each becomes a package/// folder under `looks_dir` - a manifest that names the table and a/// shader that reads it - with a card still rendered through the table@@ -5054,8 +5211,10 @@ impl Studio {if imported == 0 {return;}- for error in Catalogue::install(&dir) {- log::warn!("look: {error}");+ // A look that will not load has its reason on screen; the count of+ // the rest would only cover it.+ if self.reload_packages(false) > 0 {+ return;}self.library[0].query.clear();self.notify(diff --git a/src/crates/concat/ui/editor.slint b/src/crates/concat/ui/editor.slintindex 751a6696..92b4108c 100644--- a/src/crates/concat/ui/editor.slint+++ b/src/crates/concat/ui/editor.slint@@ -107,6 +107,10 @@ export global Editor {callback library-save-template();/// Filters page: import `.cube` tables as looks of the user's own.callback library-import-lut();+ /// Effects and Filters pages: read the user's effects folder again, so+ /// a package written or dropped in while the app runs shows up without+ /// a restart, and one that will not load says why.+ callback library-reload-packages();// ── the inspector ──in-out property <SelectedClipData> selected-clip;diff --git a/src/crates/concat/ui/workspace/media-pane.slint b/src/crates/concat/ui/workspace/media-pane.slintindex 6ef3e08b..ed62bd99 100644--- a/src/crates/concat/ui/workspace/media-pane.slint+++ b/src/crates/concat/ui/workspace/media-pane.slint@@ -1406,6 +1406,8 @@ export component MediaPane inherits Rectangle {callback save-template();/// Filters page: bring in `.cube` tables as looks of the user's own.callback import-lut();+ /// Effects and Filters pages: read the user's effects folder again.+ callback reload-packages();/// The corner button: show something else in this pane's seat.callback pane-picked(kind: PaneKind);@@ -2112,6 +2114,15 @@ export component MediaPane inherits Rectangle {}}if !root.audio-shelf: Rectangle { height: 6px; }+ // A package folder written while the app runs - the+ // user's own effect, or one shared with them - is read+ // in here, without a restart.+ ImportButton {+ label: I18n.t("Reload custom effects");+ glyph: Glyph.reset;+ clicked => { root.reload-packages(); }+ }+ Rectangle { height: 8px; }ShelfGrid {entries: root.audio-shelf ? root.catalogue-audio : root.catalogue-effects;glyph: root.audio-shelf ? Glyph.waveform : Glyph.sparkle;@@ -2143,10 +2154,18 @@ export component MediaPane inherits Rectangle {}// A table from any grading tool becomes a look here, the// way it does on every desktop editor's Filters page.- ImportButton {- label: I18n.t("Import a LUT (.cube)");- glyph: Glyph.import;- clicked => { root.import-lut(); }+ HorizontalLayout {+ spacing: 8px;+ ImportButton {+ label: I18n.t("Import a LUT (.cube)");+ glyph: Glyph.import;+ clicked => { root.import-lut(); }+ }+ ImportButton {+ label: I18n.t("Reload custom effects");+ glyph: Glyph.reset;+ clicked => { root.reload-packages(); }+ }}Rectangle { height: 8px; }ShelfGrid {diff --git a/src/crates/concat/ui/workspace/seat.slint b/src/crates/concat/ui/workspace/seat.slintindex acf6cc41..a6413e0b 100644--- a/src/crates/concat/ui/workspace/seat.slint+++ b/src/crates/concat/ui/workspace/seat.slint@@ -134,6 +134,7 @@ export component Seat inherits Rectangle {apply-audio(id) => { Editor.library-apply-audio(id); }save-template => { Editor.library-save-template(); }import-lut => { Editor.library-import-lut(); }+ reload-packages => { Editor.library-reload-packages(); }}if root.kind == PaneKind.preview: PreviewPane {