Skip to content

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

feat(media): an SVG imports as a still, drawn with resvg at 4K, instead of failing with "no usable size"

Jareerf5664c83a854
diff --git a/src/Cargo.lock b/src/Cargo.lock
index bf85c6a8..4f8fcafd 100644
--- a/src/Cargo.lock
+++ b/src/Cargo.lock
@@ -1283,6 +1283,7 @@ dependencies = [
"ffmpeg-the-third",
"libc",
"log",
+ "resvg",
"thiserror 2.0.20",
]
@@ -2219,7 +2220,9 @@ version = "0.24.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2660c5e9157bf76d2db1294e4a9feba604ef610819a3b591088d0d8392a3290f"
dependencies = [
+ "fontconfig-parser",
"log",
+ "memmap2",
"slotmap",
"tinyvec",
]
diff --git a/src/crates/concat-host/src/media.rs b/src/crates/concat-host/src/media.rs
index 5703b10a..152427a2 100644
--- a/src/crates/concat-host/src/media.rs
+++ b/src/crates/concat-host/src/media.rs
@@ -140,7 +140,8 @@ impl MediaSummary {
/// Extensions Concat is willing to treat as stills.
const IMAGE_EXTENSIONS: &[&str] = &[
- "png", "jpg", "jpeg", "webp", "bmp", "tif", "tiff", "avif", "heic", "heif", "gif",
+ "png", "jpg", "jpeg", "webp", "bmp", "tif", "tiff", "avif", "heic", "heif", "gif", "svg",
+ "svgz",
];
/// Decides whether a file is footage, sound or a still.
diff --git a/src/crates/concat-media/Cargo.toml b/src/crates/concat-media/Cargo.toml
index 9b509499..404d66f7 100644
--- a/src/crates/concat-media/Cargo.toml
+++ b/src/crates/concat-media/Cargo.toml
@@ -16,6 +16,9 @@ concat-core.workspace = true
ffmpeg-the-third = "6"
libc = "0.2"
log = "0.4"
+# SVG, drawn into a PNG FFmpeg can read; see svg.rs. The version Slint
+# already brings in, so nothing new is built.
+resvg = "0.48"
thiserror.workspace = true
[lints]
diff --git a/src/crates/concat-media/src/decode.rs b/src/crates/concat-media/src/decode.rs
index 14feb04d..bdb45595 100644
--- a/src/crates/concat-media/src/decode.rs
+++ b/src/crates/concat-media/src/decode.rs
@@ -642,7 +642,9 @@ impl Decoder {
// rule, every one of them failed to open on the CPU renderer. A
// chain that is not a graph fails at the parse, with its error.
- let path = path.as_ref();
+ // An SVG is read as its drawing, which FFmpeg can open; see svg.rs.
+ let drawn = crate::svg::readable(path.as_ref())?;
+ let path = drawn.as_path();
let input = ffmpeg::format::input(path).map_err(|error| ffi::fail("open", path, error))?;
let stream = input
.streams()
diff --git a/src/crates/concat-media/src/lib.rs b/src/crates/concat-media/src/lib.rs
index e2a93287..85a39f40 100644
--- a/src/crates/concat-media/src/lib.rs
+++ b/src/crates/concat-media/src/lib.rs
@@ -25,6 +25,7 @@ pub mod pool;
pub mod prefetch;
pub mod probe;
pub mod samples;
+pub mod svg;
pub mod treat;
pub use decode::{ColorRange, ColorSignal, DecodeOptions, Decoder, FrameSource, SeekableSource};
diff --git a/src/crates/concat-media/src/probe.rs b/src/crates/concat-media/src/probe.rs
index 471ea918..0d2f7ace 100644
--- a/src/crates/concat-media/src/probe.rs
+++ b/src/crates/concat-media/src/probe.rs
@@ -90,7 +90,9 @@ impl MediaInfo {
pub fn probe(path: impl AsRef<Path>) -> Result<MediaInfo> {
ffi::init();
let path = path.as_ref();
- let input = ffmpeg::format::input(path).map_err(|error| ffi::fail("open", path, error))?;
+ // An SVG is probed as its drawing, and described as itself; see svg.rs.
+ let drawn = crate::svg::readable(path)?;
+ let input = ffmpeg::format::input(&drawn).map_err(|error| ffi::fail("open", path, error))?;
let duration = input.duration();
let duration =
diff --git a/src/crates/concat-media/src/svg.rs b/src/crates/concat-media/src/svg.rs
new file mode 100644
index 00000000..8550d815
--- /dev/null
+++ b/src/crates/concat-media/src/svg.rs
@@ -0,0 +1,138 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2026 Jareer and Concat contributors
+
+//! SVG, which FFmpeg cannot draw.
+//!
+//! FFmpeg reads an SVG only through librsvg, which no build of Concat
+//! links; without it the file opened as a picture with no size, and the
+//! import said "video stream has no usable size". The SVG is drawn once
+//! with resvg - already in the build, through Slint - into a PNG in the
+//! temporary folder, and everything that reads pictures reads that: the
+//! probe, the monitor, the cards, proxies and export alike. The project
+//! keeps the SVG's own path, so a changed file is drawn again.
+
+use std::hash::{Hash, Hasher};
+use std::path::{Path, PathBuf};
+
+use crate::error::{Error, Result};
+
+/// The long side of the drawing, in pixels: a 4K frame's width, so a logo
+/// laid over a UHD export is as sharp as the canvas. A vector has no size
+/// of its own worth keeping - an icon says 24 px or 800 px for the same
+/// shape.
+const LONG_SIDE: f32 = 3840.0;
+
+/// Whether `path` names an SVG, by its extension.
+pub fn is_svg(path: &Path) -> bool {
+ path.extension()
+ .and_then(|extension| extension.to_str())
+ .is_some_and(|extension| {
+ extension.eq_ignore_ascii_case("svg") || extension.eq_ignore_ascii_case("svgz")
+ })
+}
+
+/// What FFmpeg should open for `path`: the SVG's drawing, drawn now if it
+/// is not drawn already, or `path` itself when it is not an SVG.
+pub fn readable(path: &Path) -> Result<PathBuf> {
+ if !is_svg(path) {
+ return Ok(path.to_path_buf());
+ }
+ let fail = |detail: String| Error::Probe {
+ path: path.to_path_buf(),
+ detail,
+ };
+ let metadata = std::fs::metadata(path).map_err(|error| fail(error.to_string()))?;
+ let drawn = drawing_of(path, &metadata);
+ if drawn.is_file() {
+ return Ok(drawn);
+ }
+ let bytes = std::fs::read(path).map_err(|error| fail(error.to_string()))?;
+ let png = draw(&bytes).map_err(fail)?;
+ if let Some(dir) = drawn.parent() {
+ let _ = std::fs::create_dir_all(dir);
+ }
+ // Written beside and renamed over, so a reader on another thread never
+ // opens half a PNG.
+ let partial = drawn.with_extension(format!("{}.part", std::process::id()));
+ std::fs::write(&partial, png).map_err(|error| fail(error.to_string()))?;
+ std::fs::rename(&partial, &drawn).map_err(|error| fail(error.to_string()))?;
+ Ok(drawn)
+}
+
+/// Where the drawing of `path`, as it is now, lives: named for its path,
+/// size and modification time, so an edited SVG gets a new drawing.
+fn drawing_of(path: &Path, metadata: &std::fs::Metadata) -> PathBuf {
+ let mut hasher = std::collections::hash_map::DefaultHasher::new();
+ path.hash(&mut hasher);
+ metadata.len().hash(&mut hasher);
+ metadata.modified().ok().hash(&mut hasher);
+ LONG_SIDE.to_bits().hash(&mut hasher);
+ std::env::temp_dir()
+ .join("concat-svg")
+ .join(format!("{:016x}.png", hasher.finish()))
+}
+
+/// The SVG in `bytes`, drawn with its long side at [`LONG_SIDE`], as a PNG.
+fn draw(bytes: &[u8]) -> std::result::Result<Vec<u8>, String> {
+ let mut options = resvg::usvg::Options::default();
+ options.fontdb_mut().load_system_fonts();
+ let tree = resvg::usvg::Tree::from_data(bytes, &options)
+ .map_err(|error| format!("not an SVG Concat can read: {error}"))?;
+ let size = tree.size();
+ let scale = LONG_SIDE / size.width().max(size.height());
+ let width = (size.width() * scale).round().max(1.0) as u32;
+ let height = (size.height() * scale).round().max(1.0) as u32;
+ let mut pixmap = resvg::tiny_skia::Pixmap::new(width, height)
+ .ok_or_else(|| format!("an SVG of {width}×{height} is too large to draw"))?;
+ resvg::render(
+ &tree,
+ resvg::tiny_skia::Transform::from_scale(scale, scale),
+ &mut pixmap.as_mut(),
+ );
+ pixmap
+ .encode_png()
+ .map_err(|error| format!("could not write the SVG's drawing: {error}"))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ const HELIX: &str = r##"<svg width="800px" height="400px" viewBox="0 0 1024 512" xmlns="http://www.w3.org/2000/svg"><rect x="8" y="8" width="1000" height="490" fill="#c0ff33"/></svg>"##;
+
+ #[test]
+ fn an_svg_is_drawn_at_the_long_side_and_probes_as_a_still() {
+ let dir = std::env::temp_dir().join(format!("concat-svg-test-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let svg = dir.join("helix.svg");
+ std::fs::write(&svg, HELIX).unwrap();
+
+ let drawn = readable(&svg).unwrap();
+ assert_ne!(drawn, svg);
+ assert_eq!(readable(&svg).unwrap(), drawn, "drawn once");
+
+ let info = crate::probe(&svg).unwrap();
+ assert_eq!(info.path, svg, "the project keeps the SVG");
+ let video = info.video.expect("a picture");
+ assert_eq!((video.width, video.height), (3840, 1920));
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
+ #[test]
+ fn not_an_svg_says_so() {
+ let dir = std::env::temp_dir().join(format!("concat-svg-bad-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let svg = dir.join("broken.svg");
+ std::fs::write(&svg, "not xml at all").unwrap();
+ let error = readable(&svg).unwrap_err().to_string();
+ assert!(error.contains("not an SVG"), "{error}");
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
+ #[test]
+ fn other_files_are_read_as_they_are() {
+ let path = Path::new("clip.mp4");
+ assert_eq!(readable(path).unwrap(), path);
+ assert!(is_svg(Path::new("logo.SVG")));
+ }
+}
diff --git a/src/crates/concat/src/lib.rs b/src/crates/concat/src/lib.rs
index 58d63317..63b352b4 100644
--- a/src/crates/concat/src/lib.rs
+++ b/src/crates/concat/src/lib.rs
@@ -862,6 +862,7 @@ fn start(project: Option<std::path::PathBuf>, smoke: bool) -> Result<(), slint::
&[
"mp4", "mov", "mkv", "webm", "avi", "m4v", "mp3", "wav", "aac", "m4a",
"flac", "ogg", "png", "jpg", "jpeg", "webp", "gif", "bmp", "tif", "tiff",
+ "svg",
],
)),
|studio, _, _, paths| studio.handle(Msg::Media(MediaMsg::Import(paths))),
diff --git a/src/crates/concat/src/platform.rs b/src/crates/concat/src/platform.rs
index 613a5da0..5d6f8445 100644
--- a/src/crates/concat/src/platform.rs
+++ b/src/crates/concat/src/platform.rs
@@ -806,7 +806,8 @@ fn media_kind(extension: &str) -> Option<MediaKind> {
"mp3" | "wav" | "aac" | "m4a" | "flac" | "ogg" | "opus" | "aif" | "aiff" => {
Some(MediaKind::Audio)
}
- "png" | "jpg" | "jpeg" | "webp" | "gif" | "bmp" | "tif" | "tiff" | "heic" | "heif" => {
+ "png" | "jpg" | "jpeg" | "webp" | "gif" | "bmp" | "tif" | "tiff" | "heic" | "heif"
+ | "svg" | "svgz" => {
Some(MediaKind::Image)
}
_ => None,