Skip to content

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

updates

Jareer567e3ea07a99
diff --git a/desktop/src-tauri/capabilities/default.json b/desktop/src-tauri/capabilities/default.json
index f1fc3edb..496bb693 100644
--- a/desktop/src-tauri/capabilities/default.json
+++ b/desktop/src-tauri/capabilities/default.json
@@ -6,6 +6,7 @@
"permissions": [
"core:default",
"opener:default",
+ "opener:allow-reveal-item-in-dir",
"dialog:default",
"core:window:allow-minimize",
diff --git a/desktop/src/App.tsx b/desktop/src/App.tsx
index 81f71aee..9f6274f7 100644
--- a/desktop/src/App.tsx
+++ b/desktop/src/App.tsx
@@ -5,7 +5,7 @@ import { getCurrentWebview } from "@tauri-apps/api/webview";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { ContextMenu, type ContextTarget } from "./components/ContextMenu";
-import { ExportDialog } from "./components/ExportDialog";
+import { ExportDialog, type ExportTitle } from "./components/ExportDialog";
import { Icon } from "./components/Icon";
import { ALL_MEDIA, MediaBin, type BinFilter } from "./components/MediaBin";
import { Preview, type PreviewSource, type TextOverlay } from "./components/Preview";
@@ -763,6 +763,32 @@ function Editor({
[project],
);
+ // Titles for the exporter. They have no file to hand over - the dialog
+ // rasterises them into full-frame PNGs at export time - so all it needs is
+ // the style and where the clip sits. Hidden tracks drop out here, the same
+ // cut `exportClips` makes with `hidden`.
+ const exportTitles = useMemo<ExportTitle[]>(
+ () =>
+ project.clips.flatMap((clip) => {
+ if (clip.kind !== "text" || !clip.text) return [];
+ const track = findTrack(project, clip.trackId);
+ const index = project.tracks.findIndex((candidate) => candidate.id === clip.trackId);
+ if (!track || !track.visible || index < 0) return [];
+ return [
+ {
+ clipId: clip.id,
+ style: clip.text,
+ offsetX: clip.offsetX,
+ offsetY: clip.offsetY,
+ start: clip.start,
+ duration: clip.duration,
+ track: index,
+ },
+ ];
+ }),
+ [project],
+ );
+
// The inspector shows one clip's properties; with several selected there is
// no single set of values to show, so it falls back to the bin item.
const selectedClip =
@@ -1191,6 +1217,7 @@ function Editor({
rateDen={session.rateDen}
duration={duration}
clips={exportClips}
+ titles={exportTitles}
onClose={() => setExporting(false)}
/>
)}
diff --git a/desktop/src/components/ExportDialog.tsx b/desktop/src/components/ExportDialog.tsx
index 4ddf820c..890e0251 100644
--- a/desktop/src/components/ExportDialog.tsx
+++ b/desktop/src/components/ExportDialog.tsx
@@ -1,9 +1,12 @@
import { useEffect, useRef, useState } from "react";
import type { ReactNode } from "react";
import { save } from "@tauri-apps/plugin-dialog";
+import { revealItemInDir } from "@tauri-apps/plugin-opener";
import type { ExportClip, ExportProgress } from "../lib/engine";
-import { exportProject, onExportProgress } from "../lib/engine";
+import { exportProject, onExportProgress, writeCacheFile } from "../lib/engine";
+import { rasterizeTitle } from "../lib/rasterize";
+import type { TextStyle } from "../lib/text";
import { shortDuration } from "../lib/time";
import { ErrorNotice } from "./ErrorNotice";
import { Icon } from "./Icon";
@@ -15,18 +18,68 @@ const QUALITIES = [
{ label: "Small", crf: 27, preset: "fast", hint: "compact" },
] as const;
+/**
+ * A title bound for the file. The engine composites pixels, not fonts, so the
+ * dialog rasterises each of these into a full-frame transparent PNG at the
+ * output size just before the render starts.
+ */
+export interface ExportTitle {
+ clipId: string;
+ style: TextStyle;
+ /** Offset from centred, as a fraction of the frame. */
+ offsetX: number;
+ offsetY: number;
+ start: number;
+ duration: number;
+ /** Index into the track stack, zero being bottom-most. */
+ track: number;
+}
+
type Phase =
| { kind: "idle" }
| { kind: "running"; progress: ExportProgress | null }
| { kind: "done"; path: string }
| { kind: "failed"; message: string };
+/** A title's PNG, dressed as the flat clip the exporter understands. */
+function overlayClip(title: ExportTitle, path: string): ExportClip {
+ return {
+ path,
+ kind: "image",
+ start: title.start,
+ duration: title.duration,
+ sourceStart: 0,
+ track: title.track,
+ hidden: false,
+ muted: true,
+ volume: 0,
+ fadeIn: 0,
+ fadeOut: 0,
+ filterChain: "",
+ speed: 1,
+ preservePitch: true,
+ // Identity on purpose: the clip's offsets are baked into the PNG, and
+ // absent media dimensions make the exporter fill the frame edge to edge -
+ // exactly what a full-frame overlay wants.
+ scale: 1,
+ offsetX: 0,
+ offsetY: 0,
+ rotation: 0,
+ mediaWidth: null,
+ mediaHeight: null,
+ };
+}
+
/**
* The export sheet.
*
* Rendering happens on a blocking thread in the host and reports back through
* an event, so this listens rather than waiting - a two-minute export that
* says nothing until it finishes is indistinguishable from a hang.
+ *
+ * The backdrop dims rather than hides: the edit behind it is what is being
+ * exported, and watching the file happen over the thing it is made from is
+ * both orienting and honest. Only the sheet itself is opaque.
*/
export function ExportDialog({
projectName,
@@ -37,6 +90,7 @@ export function ExportDialog({
rateDen,
duration,
clips,
+ titles,
onClose,
}: {
projectName: string;
@@ -47,18 +101,22 @@ export function ExportDialog({
rateDen: number;
duration: number;
clips: ExportClip[];
+ titles: ExportTitle[];
onClose: () => void;
}) {
- const [output, setOutput] = useState(`${projectPath}\\${projectName}.mp4`);
+ const [output, setOutput] = useState(`${projectPath}/${projectName}.mp4`);
const [quality, setQuality] = useState<(typeof QUALITIES)[number]>(QUALITIES[1]);
const [phase, setPhase] = useState<Phase>({ kind: "idle" });
const unlisten = useRef<(() => void) | null>(null);
+ // When the engine started counting frames, for the time-left estimate.
+ const startedAt = useRef<number | null>(null);
useEffect(() => {
return () => unlisten.current?.();
}, []);
const running = phase.kind === "running";
+ const empty = clips.length === 0 && titles.length === 0;
const browse = async () => {
const chosen = await save({
@@ -70,14 +128,34 @@ export function ExportDialog({
};
const start = async () => {
- if (running || clips.length === 0) return;
+ if (running || empty) return;
setPhase({ kind: "running", progress: null });
- unlisten.current = await onExportProgress((progress) =>
- setPhase((current) => (current.kind === "running" ? { kind: "running", progress } : current)),
- );
+ startedAt.current = null;
try {
+ // Titles first, before the engine is involved: each becomes a PNG in
+ // the project cache and joins the clip list as one more still.
+ const overlays: ExportClip[] = [];
+ for (const [index, title] of titles.entries()) {
+ setPhase({
+ kind: "running",
+ progress: { frame: index, total: titles.length, stage: "drawing titles" },
+ });
+ const bytes = await rasterizeTitle(title.style, title.offsetX, title.offsetY, width, height);
+ const key = `title-${index}-${title.clipId.replace(/[^A-Za-z0-9_-]/g, "")}.png`;
+ const path = await writeCacheFile(projectPath, key, bytes);
+ overlays.push(overlayClip(title, path));
+ }
+
+ setPhase({ kind: "running", progress: null });
+ unlisten.current = await onExportProgress((progress) => {
+ startedAt.current ??= performance.now();
+ setPhase((current) =>
+ current.kind === "running" ? { kind: "running", progress } : current,
+ );
+ });
+
const path = await exportProject({
output,
width,
@@ -86,7 +164,7 @@ export function ExportDialog({
rateDen,
crf: quality.crf,
preset: quality.preset,
- clips,
+ clips: [...clips, ...overlays],
});
setPhase({ kind: "done", path });
} catch (cause) {
@@ -102,8 +180,28 @@ export function ExportDialog({
? Math.min(100, Math.round((phase.progress.frame / phase.progress.total) * 100))
: null;
+ // A time-left estimate from the frame rate so far. Held back until a second
+ // of footage has rendered - the first frames carry startup cost and would
+ // only produce a number that shrinks embarrassingly fast.
+ let remaining: string | null = null;
+ if (phase.kind === "running" && phase.progress && startedAt.current !== null) {
+ const { frame, total } = phase.progress;
+ const elapsed = (performance.now() - startedAt.current) / 1000;
+ if (frame > rateNum / rateDen && total > frame && elapsed > 1) {
+ remaining = `about ${shortDuration((elapsed / frame) * (total - frame))} left`;
+ }
+ }
+
return (
- <div className="fixed inset-0 z-50 flex items-center justify-center bg-sunken p-8">
+ <div
+ className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-8
+ backdrop-blur-[2px]"
+ onMouseDown={(event) => {
+ // The backdrop closes the sheet the way every sheet closes - but
+ // never out from under a running render.
+ if (event.target === event.currentTarget && !running) onClose();
+ }}
+ >
<div className="surface w-full max-w-md rounded-2xl p-6">
<div className="mb-5 flex items-center gap-2">
<Icon name="export" size={17} className="text-accent" />
@@ -123,7 +221,13 @@ export function ExportDialog({
<dl className="mb-5 space-y-1 rounded-lg bg-sunken px-3 py-2.5">
<Row label="Format" value={`${width} x ${height} · ${(rateNum / rateDen).toFixed(2)} fps`} />
<Row label="Duration" value={shortDuration(duration)} />
- <Row label="Clips" value={`${clips.length}`} />
+ <Row
+ label="Contents"
+ value={
+ `${clips.length} clip${clips.length === 1 ? "" : "s"}` +
+ (titles.length > 0 ? ` · ${titles.length} title${titles.length === 1 ? "" : "s"}` : "")
+ }
+ />
</dl>
{phase.kind === "idle" || phase.kind === "failed" ? (
@@ -141,7 +245,7 @@ export function ExportDialog({
type="button"
onClick={() => void browse()}
className="flex shrink-0 cursor-pointer items-center gap-1.5 rounded-lg bg-hover px-3
- text-xs text-primary transition-colors hover:bg-hover"
+ text-xs text-primary transition-colors hover:bg-active"
>
<Icon name="folder" size={13} />
Browse
@@ -160,7 +264,7 @@ export function ExportDialog({
transition-colors ${
option.label === quality.label
? "bg-accent text-on-accent"
- : "bg-hover text-secondary hover:bg-hover"
+ : "bg-hover text-secondary hover:bg-active"
}`}
>
<span className="text-xs">{option.label}</span>
@@ -176,22 +280,22 @@ export function ExportDialog({
<button
type="button"
onClick={() => void start()}
- disabled={clips.length === 0 || !output.trim()}
+ disabled={empty || !output.trim()}
className="flex w-full cursor-pointer items-center justify-center gap-2 rounded-lg
bg-accent px-4 py-2.5 text-sm font-medium text-on-accent transition-colors
hover:bg-accent-hover disabled:cursor-not-allowed disabled:opacity-40"
>
<Icon name="export" size={15} />
- {clips.length === 0 ? "Nothing to export" : "Export"}
+ {empty ? "Nothing to export" : phase.kind === "failed" ? "Try again" : "Export"}
</button>
</>
) : phase.kind === "running" ? (
<div className="py-2">
- <div className="mb-2 flex items-baseline justify-between">
- <span className="text-xs text-primary">
+ <div className="mb-2 flex items-baseline justify-between gap-3">
+ <span className="min-w-0 truncate text-xs text-primary">
{phase.progress?.stage ?? "starting"}
</span>
- <span className="font-technical text-[11px] tabular-nums text-secondary">
+ <span className="shrink-0 font-technical text-sm tabular-nums text-primary">
{percent === null ? "" : `${percent}%`}
</span>
</div>
@@ -201,27 +305,45 @@ export function ExportDialog({
style={{ width: `${percent ?? 3}%` }}
/>
</div>
- {phase.progress && phase.progress.total > 0 && (
- <p className="mt-2 font-technical text-[10px] text-tertiary">
- frame {phase.progress.frame} of {phase.progress.total}
- </p>
- )}
+ <div className="mt-2 flex items-baseline justify-between gap-3">
+ {phase.progress && phase.progress.total > 0 ? (
+ <p className="font-technical text-[10px] text-tertiary">
+ frame {phase.progress.frame} of {phase.progress.total}
+ </p>
+ ) : (
+ <span />
+ )}
+ {remaining && (
+ <p className="font-technical text-[10px] tabular-nums text-tertiary">{remaining}</p>
+ )}
+ </div>
</div>
) : (
<div className="py-1">
<p className="mb-1 flex items-center gap-2 text-sm text-success">
- <Icon name="play" size={14} />
+ <Icon name="check" size={14} />
Export finished
</p>
<p className="mb-5 wrap-break-word font-technical text-[10px] text-secondary">{phase.path}</p>
- <button
- type="button"
- onClick={onClose}
- className="w-full cursor-pointer rounded-lg bg-hover px-4 py-2.5 text-sm text-primary
- transition-colors hover:bg-hover"
- >
- Done
- </button>
+ <div className="flex gap-1.5">
+ <button
+ type="button"
+ onClick={() => void revealItemInDir(phase.path).catch(() => undefined)}
+ className="flex flex-1 cursor-pointer items-center justify-center gap-1.5 rounded-lg
+ bg-hover px-4 py-2.5 text-sm text-primary transition-colors hover:bg-active"
+ >
+ <Icon name="folder" size={13} />
+ Show in Finder
+ </button>
+ <button
+ type="button"
+ onClick={onClose}
+ className="flex-1 cursor-pointer rounded-lg bg-accent px-4 py-2.5 text-sm font-medium
+ text-on-accent transition-colors hover:bg-accent-hover"
+ >
+ Done
+ </button>
+ </div>
</div>
)}
</div>
diff --git a/desktop/src/lib/engine.ts b/desktop/src/lib/engine.ts
index 748ca023..70cbd21e 100644
--- a/desktop/src/lib/engine.ts
+++ b/desktop/src/lib/engine.ts
@@ -171,6 +171,21 @@ export async function exportProject(request: ExportRequest): Promise<string> {
return invoke<string>("export_project", { request });
}
+/**
+ * Writes one file into the project's cache folder and returns its path.
+ *
+ * The same store the artwork cache uses, so it travels with the project and
+ * vanishes with it. Keys are flat filenames; the host refuses anything else.
+ */
+export async function writeCacheFile(
+ project: string,
+ key: string,
+ bytes: Uint8Array,
+): Promise<string> {
+ await invoke<void>("write_artwork", { project, key, bytes: Array.from(bytes) });
+ return `${project}/cache/${key}`;
+}
+
/** Subscribes to export progress. Resolves to an unsubscribe function. */
export async function onExportProgress(
handler: (progress: ExportProgress) => void,
diff --git a/desktop/src/lib/rasterize.ts b/desktop/src/lib/rasterize.ts
new file mode 100644
index 00000000..68364562
--- /dev/null
+++ b/desktop/src/lib/rasterize.ts
@@ -0,0 +1,117 @@
+/**
+ * Turning a title into pixels for the exporter.
+ *
+ * The engine composites RGBA frames; it knows nothing about fonts. So a text
+ * clip goes into an export as a full-frame transparent PNG, drawn here at the
+ * exact output size with the same rules `textCss` gives the preview overlay -
+ * same fraction-of-frame-height sizing, same stroke-behind-fill, same plate.
+ * One PNG per title, full frame rather than tight bounds, so the clip's
+ * offsets are already baked in and the exporter places it edge to edge with
+ * an identity transform.
+ */
+
+import type { TextStyle } from "./text";
+
+/** Mirrors `textCss`'s shadow: offset 4% of the size, blur 12%. */
+const SHADOW = { offset: 0.04, blur: 0.12, color: "rgba(0,0,0,0.55)" };
+/** Mirrors `textCss`'s plate: padding 20%/35% of the size, radius 12%. */
+const PLATE = { padY: 0.2, padX: 0.35, radius: 0.12 };
+
+/**
+ * Draws one title into a `width` x `height` transparent PNG.
+ *
+ * Known divergence from the DOM: the preview wraps a line longer than 92% of
+ * the frame, canvas does not - a title that long is already unreadable, so
+ * explicit line breaks are the layout and overflow just runs off the frame.
+ */
+export async function rasterizeTitle(
+ style: TextStyle,
+ offsetX: number,
+ offsetY: number,
+ width: number,
+ height: number,
+): Promise<Uint8Array> {
+ const size = style.fontSize * height;
+ const font = `${style.italic ? "italic " : ""}${style.fontWeight} ${size}px ${style.fontFamily}`;
+
+ // Make sure the face is actually resident before measuring: a fallback font
+ // would bake the wrong glyphs into the file, which is worse than a slow
+ // start. Best-effort - an unknown family falls back exactly like CSS does.
+ await document.fonts.load(font, style.content).catch(() => undefined);
+
+ const canvas = document.createElement("canvas");
+ canvas.width = Math.max(1, Math.round(width));
+ canvas.height = Math.max(1, Math.round(height));
+ const context = canvas.getContext("2d");
+ if (!context) throw new Error("no 2d canvas for the title rasteriser");
+
+ context.font = font;
+ context.textBaseline = "middle";
+ // `letterSpacing` is newer canvas API; where it is missing the export just
+ // loses tracking, matching nothing worse than an older browser.
+ if ("letterSpacing" in context) context.letterSpacing = `${style.tracking * size}px`;
+
+ const lines = style.content.split("\n");
+ const lineHeight = style.lineHeight * size;
+ const blockWidth = Math.max(...lines.map((line) => context.measureText(line).width), 1);
+ const blockHeight = lines.length * lineHeight;
+
+ const centerX = width / 2 + offsetX * width;
+ const centerY = height / 2 + offsetY * height;
+
+ context.globalAlpha = style.opacity;
+
+ if (style.background) {
+ const padX = size * PLATE.padX;
+ const padY = size * PLATE.padY;
+ context.fillStyle = style.background;
+ context.beginPath();
+ context.roundRect(
+ centerX - blockWidth / 2 - padX,
+ centerY - blockHeight / 2 - padY,
+ blockWidth + padX * 2,
+ blockHeight + padY * 2,
+ size * PLATE.radius,
+ );
+ context.fill();
+ }
+
+ // The shadow goes on after the plate: `textCss` shadows the letters, never
+ // the plate behind them.
+ if (style.shadow) {
+ context.shadowColor = SHADOW.color;
+ context.shadowOffsetY = size * SHADOW.offset;
+ context.shadowBlur = size * SHADOW.blur;
+ }
+
+ context.textAlign = style.align;
+ const alignX =
+ style.align === "left"
+ ? centerX - blockWidth / 2
+ : style.align === "right"
+ ? centerX + blockWidth / 2
+ : centerX;
+
+ for (const [index, line] of lines.entries()) {
+ const y = centerY - blockHeight / 2 + (index + 0.5) * lineHeight;
+ // Stroke first, fill over it - the canvas spelling of
+ // `paint-order: stroke fill`, so an outline thickens outwards instead of
+ // eating into the letterforms.
+ if (style.strokeWidth > 0) {
+ context.strokeStyle = style.strokeColor;
+ context.lineWidth = style.strokeWidth * size;
+ context.lineJoin = "round";
+ context.strokeText(line, alignX, y);
+ }
+ context.fillStyle = style.color;
+ context.fillText(line, alignX, y);
+ }
+
+ const blob = await new Promise<Blob>((resolve, reject) => {
+ canvas.toBlob(
+ (result) => (result ? resolve(result) : reject(new Error("title PNG encode failed"))),
+ "image/png",
+ );
+ });
+ return new Uint8Array(await blob.arrayBuffer());
+}