Skip to content

The VS Code of video editing, built for agents.

TypeScript

feat: add export current frame and video commands with preset menus

konstantin-paulus9617d8adec47
diff --git a/apps/web/src/components/canvas/action-bar.tsx b/apps/web/src/components/canvas/action-bar.tsx
index 01bba4a2..3d059c4e 100644
--- a/apps/web/src/components/canvas/action-bar.tsx
+++ b/apps/web/src/components/canvas/action-bar.tsx
@@ -6,10 +6,11 @@
// up while it works: what it applies to, and the way out. The pieces here are
// what bars share; what goes between them is each bar's own.
-import { Match, Show, Switch, onCleanup, onMount } from "solid-js";
+import { Match, Show, Switch, onCleanup, onMount, splitProps } from "solid-js";
import { useTrait, useWorld } from "@diffusionstudio/koota-solid";
import { Name } from "@diffusionstudio/runtime";
import { Button } from "@/components/ui/button";
+import { DropdownMenuItem } from "@/components/ui/dropdown-menu";
import { Icon } from "@/components/ui/icon";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { getClipFallbackName } from "@/engine/timeline/style";
@@ -146,3 +147,47 @@ export function ActionBarConfirm(props: ActionBarConfirmProps) {
</Tooltip>
);
}
+
+/** The menus a bar opens: headed lists, laid out like the toolbar's own. */
+export const ACTION_BAR_MENU_CLASS =
+ "rounded-xl border-border bg-background px-2 pt-0 pb-2 gap-0 shadow-[0px_12px_24px_0px_rgba(0,0,0,0.24)]";
+
+export function ActionBarMenuHeader(props: { children: JSX.Element }) {
+ return <div class="flex h-8 items-center px-1 text-xs text-muted-foreground">{props.children}</div>;
+}
+
+type ActionBarMenuItemProps = {
+ checked: boolean;
+ onSelect(): void;
+ onHover?(row: HTMLElement | null): void;
+ children: JSX.Element;
+};
+
+/** A menu row that is the current pick: raised, with a check at its end. */
+export function ActionBarMenuItem(props: ActionBarMenuItemProps) {
+ const [local, rest] = splitProps(props, ["checked", "onHover", "children"]);
+
+ return (
+ <DropdownMenuItem
+ {...rest}
+ onPointerEnter={(event: PointerEvent) => local.onHover?.(event.currentTarget as HTMLElement)}
+ onPointerLeave={() => local.onHover?.(null)}
+ onFocus={(event: FocusEvent) => local.onHover?.(event.currentTarget as HTMLElement)}
+ onBlur={() => local.onHover?.(null)}
+ tone="neutral"
+ class={cx("pr-0", local.checked ? "bg-accent text-foreground" : "text-muted-foreground")}
+ >
+ <span class="min-w-0 flex-1 truncate">{local.children}</span>
+ <span class="flex size-7 items-center justify-center">
+ <Show when={local.checked}>
+ <Icon name="confirm-check" class="text-foreground" />
+ </Show>
+ </span>
+ </DropdownMenuItem>
+ );
+}
+
+/** Esc closes an open menu; it goes no further, where it would take the bar or the selection down. */
+export function keepMenuEscape(event: KeyboardEvent) {
+ event.stopPropagation();
+}
diff --git a/apps/web/src/components/canvas/object-mask-bar.tsx b/apps/web/src/components/canvas/object-mask-bar.tsx
index d4c57195..008e12d7 100644
--- a/apps/web/src/components/canvas/object-mask-bar.tsx
+++ b/apps/web/src/components/canvas/object-mask-bar.tsx
@@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-import { For, Show, createResource, createSignal, onMount, splitProps } from "solid-js";
+import { For, Show, createResource, createSignal, onMount } from "solid-js";
import { Portal } from "solid-js/web";
import { SAM2_MODELS, downloadSize, sam2Model } from "@diffusionstudio/sam2/models";
import { useWorld } from "@diffusionstudio/koota-solid";
@@ -16,12 +16,11 @@ import { Popover, PopoverContent, PopoverPortal, PopoverTrigger } from "@/compon
import {
DropdownMenu,
DropdownMenuContent,
- DropdownMenuItem,
DropdownMenuPortal,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { cx } from "@/lib/cva";
-import { ActionBar } from "./action-bar";
+import { ACTION_BAR_MENU_CLASS, ActionBar, ActionBarMenuHeader, ActionBarMenuItem, keepMenuEscape } from "./action-bar";
import { useDerived, useObjectMaskTool } from "@/engine/hooks";
import {
cancelObjectMask,
@@ -63,9 +62,6 @@ const MODEL_NOTES: Record<Sam2ModelId, string> = {
/** The brush's size on its slider: its diameter, in percent of the frame's height. */
const BRUSH_SIZE = { min: 1, max: 30 };
-/** The menus the bar opens: headed lists, laid out like the toolbar's own. */
-const MENU_CLASS = "rounded-xl border-border bg-background px-2 pt-0 pb-2 gap-0 shadow-[0px_12px_24px_0px_rgba(0,0,0,0.24)]";
-
type SessionState = {
status: ObjectTrackStatus | null;
completed: number;
@@ -195,18 +191,18 @@ function ModelMenu(props: { disabled: boolean }) {
<TooltipContent>{load() ? describeModelLoad(load()!) : "Segmentation model"}</TooltipContent>
</Tooltip>
<DropdownMenuPortal>
- <DropdownMenuContent class={cx(MENU_CLASS, "min-w-40")} onEscapeKeyDown={keepEscape}>
- <MenuHeader>Model</MenuHeader>
+ <DropdownMenuContent class={cx(ACTION_BAR_MENU_CLASS, "min-w-40")} onEscapeKeyDown={keepMenuEscape}>
+ <ActionBarMenuHeader>Model</ActionBarMenuHeader>
<div class="flex flex-col gap-1 py-0.5">
<For each={SAM2_MODELS}>
{(model) => (
- <CheckedItem
+ <ActionBarMenuItem
checked={model.id === objectMaskModel()}
onSelect={() => pickObjectMaskModel(model.id)}
onHover={(row) => setHovered(row ? { model, row } : null)}
>
{model.label}
- </CheckedItem>
+ </ActionBarMenuItem>
)}
</For>
</div>
@@ -266,11 +262,11 @@ function ModePopover() {
</Tooltip>
<PopoverPortal>
<PopoverContent
- class={cx(MENU_CLASS, "w-44")}
- onEscapeKeyDown={keepEscape}
+ class={cx(ACTION_BAR_MENU_CLASS, "w-44")}
+ onEscapeKeyDown={keepMenuEscape}
onOpenAutoFocus={(event) => event.preventDefault()}
>
- <MenuHeader>Tool</MenuHeader>
+ <ActionBarMenuHeader>Tool</ActionBarMenuHeader>
<SegmentedIconTabs value={tool.mode} onChange={(mode) => tool.set({ mode })} items={MODE_TABS} class="my-0.5" />
<Show when={mode().value === "brush"}>
<div class="pt-2 pb-0.5">
@@ -316,14 +312,14 @@ function OpMenu(props: { disabled: boolean }) {
<TooltipContent shortcut="Hold ⌥ to swap">{op().hint}</TooltipContent>
</Tooltip>
<DropdownMenuPortal>
- <DropdownMenuContent class={cx(MENU_CLASS, "min-w-40")} onEscapeKeyDown={keepEscape}>
- <MenuHeader>Mode</MenuHeader>
+ <DropdownMenuContent class={cx(ACTION_BAR_MENU_CLASS, "min-w-40")} onEscapeKeyDown={keepMenuEscape}>
+ <ActionBarMenuHeader>Mode</ActionBarMenuHeader>
<div class="flex flex-col gap-1 py-0.5">
<For each={OPS}>
{(entry) => (
- <CheckedItem checked={entry.value === tool.op()} onSelect={() => tool.set({ op: entry.value })}>
+ <ActionBarMenuItem checked={entry.value === tool.op()} onSelect={() => tool.set({ op: entry.value })}>
{entry.label}
- </CheckedItem>
+ </ActionBarMenuItem>
)}
</For>
</div>
@@ -333,46 +329,6 @@ function OpMenu(props: { disabled: boolean }) {
);
}
-function MenuHeader(props: { children: JSX.Element }) {
- return <div class="flex h-8 items-center px-1 text-xs text-muted-foreground">{props.children}</div>;
-}
-
-type CheckedItemProps = {
- checked: boolean;
- onSelect(): void;
- onHover?(row: HTMLElement | null): void;
- children: JSX.Element;
-};
-
-/** A menu row that is the current pick: raised, with a check at its end. */
-function CheckedItem(props: CheckedItemProps) {
- const [local, rest] = splitProps(props, ["checked", "onHover", "children"]);
-
- return (
- <DropdownMenuItem
- {...rest}
- onPointerEnter={(event: PointerEvent) => local.onHover?.(event.currentTarget as HTMLElement)}
- onPointerLeave={() => local.onHover?.(null)}
- onFocus={(event: FocusEvent) => local.onHover?.(event.currentTarget as HTMLElement)}
- onBlur={() => local.onHover?.(null)}
- tone="neutral"
- class={cx("pr-0", local.checked ? "bg-accent text-foreground" : "text-muted-foreground")}
- >
- <span class="min-w-0 flex-1 truncate">{local.children}</span>
- <span class="flex size-7 items-center justify-center">
- <Show when={local.checked}>
- <Icon name="confirm-check" class="text-foreground" />
- </Show>
- </span>
- </DropdownMenuItem>
- );
-}
-
-/** Esc closes an open menu; it goes no further, where it would put the tool down. */
-function keepEscape(event: KeyboardEvent) {
- event.stopPropagation();
-}
-
/**
* Content as wide as `widest` whatever it says, centered in that width: the
* button around it holds still as a number in it counts up.
diff --git a/apps/web/src/components/commands/export-current-frame.tsx b/apps/web/src/components/commands/export-current-frame.tsx
new file mode 100644
index 00000000..fe5810f5
--- /dev/null
+++ b/apps/web/src/components/commands/export-current-frame.tsx
@@ -0,0 +1,90 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { createSignal } from "solid-js";
+import { computeOutputSize } from "@diffusionstudio/encoder";
+import { Computed } from "@diffusionstudio/runtime";
+import { Separator } from "@/components/ui/separator";
+import { useCommand, useCommands } from "@/context/commands";
+import { useExport } from "@/context/export";
+import { useActiveScene, useDerived } from "@/engine/hooks";
+import { store } from "@/init";
+import { ActionBar, ActionBarCancel, ActionBarConfirm, ActionBarTarget } from "@/components/canvas/action-bar";
+import { PresetMenu } from "./preset-menu";
+
+import type { PresetGroup } from "./preset-menu";
+
+/** The preset the bar opened on last, kept across sessions. */
+const LAST_PRESET_KEY = "commands.export-current-frame.preset";
+
+/** The sizes an image is offered at, named as the video presets name them; each is the shorter side. */
+const PRESETS = [
+ { id: "1080p", name: "Full HD", resolution: 1080 },
+ { id: "1440p", name: "2k Quad HD", resolution: 1440 },
+ { id: "2160p", name: "4K Ultra HD", resolution: 2160 },
+];
+
+export function ExportCurrentFrameCommand() {
+ useCommand({
+ id: "export-current-frame",
+ title: "Export current frame",
+ group: "Export",
+ icon: "image",
+ keywords: ["image", "still", "png", "screenshot", "1080p", "1440p", "4k"],
+ shortcut: ["⇧", "⌘", "E"],
+ bar: ExportCurrentFrameBar,
+ });
+
+ return null;
+}
+
+/** The frame at the active scene's playhead, exported as a PNG at the size picked. */
+function ExportCurrentFrameBar() {
+ const { close } = useCommands();
+ const { exportFrame } = useExport();
+ const scene = useActiveScene();
+ const size = useDerived(
+ () => {
+ const computed = scene()?.get(Computed);
+ return { width: computed?.width || 1920, height: computed?.height || 1080 };
+ },
+ (a, b) => a.width === b.width && a.height === b.height,
+ );
+
+ const [picked, setPicked] = createSignal(store.get<string>(LAST_PRESET_KEY));
+ const preset = () => PRESETS.find((entry) => entry.id === picked()) ?? PRESETS[0]!;
+
+ // Each preset with the pixel size it makes of this scene.
+ const groups = (): PresetGroup[] => [
+ {
+ label: "Size",
+ presets: PRESETS.map((entry) => {
+ const output = computeOutputSize(size().width, size().height, entry.resolution);
+ return { id: entry.id, name: entry.name, detail: `${output.width}×${output.height}` };
+ }),
+ },
+ ];
+
+ const pick = (id: string) => {
+ setPicked(id);
+ store.set(LAST_PRESET_KEY, id);
+ };
+
+ const confirm = () => {
+ const active = scene();
+ if (!active) return;
+ close();
+ void exportFrame(active, preset().resolution);
+ };
+
+ return (
+ <ActionBar>
+ <ActionBarTarget nodes={scene() ? [scene()!] : []} verb="Export frame of" empty="Open a scene to export" />
+ <PresetMenu groups={groups()} value={preset().id} onChange={pick} tooltip="Image size" />
+ <Separator orientation="vertical" class="min-h-5" />
+ <ActionBarCancel onCancel={close} />
+ <ActionBarConfirm disabled={!scene()} onConfirm={confirm} hint="Export current frame" />
+ </ActionBar>
+ );
+}
diff --git a/apps/web/src/components/commands/export-video.tsx b/apps/web/src/components/commands/export-video.tsx
new file mode 100644
index 00000000..7867acf3
--- /dev/null
+++ b/apps/web/src/components/commands/export-video.tsx
@@ -0,0 +1,108 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { createMemo, createSignal } from "solid-js";
+import { Separator } from "@/components/ui/separator";
+import { useCommand, useCommands } from "@/context/commands";
+import { useExport } from "@/context/export";
+import { useActiveScene } from "@/engine/hooks";
+import { useProjectConfig } from "@/engine/project-config";
+import { store } from "@/init";
+import {
+ DEFAULT_EXPORT_TEMPLATE_ID,
+ EXPORT_TEMPLATE_GROUPS,
+ TEMPLATE_BY_ID,
+} from "@/components/sidebar-right/inspector/export-templates";
+import { ActionBar, ActionBarCancel, ActionBarConfirm, ActionBarTarget } from "@/components/canvas/action-bar";
+import { PresetMenu } from "./preset-menu";
+
+import type { ExportConfig } from "@/components/sidebar-right/inspector/export-templates";
+import type { PresetGroup } from "./preset-menu";
+
+/** The preset the bar opened on last, kept across sessions. */
+const LAST_PRESET_KEY = "commands.export-video.preset";
+
+/** The scene's own settings, from the inspector's Export panel, as a pick. */
+const SCENE_SETTINGS = "scene";
+
+export function ExportVideoCommand() {
+ useCommand({
+ id: "export-video",
+ title: "Export video",
+ group: "Export",
+ icon: "film-video-export",
+ keywords: ["render", "mp4", "webm", "720p", "1080p", "1440p", "4k", "youtube", "tiktok", "instagram"],
+ shortcut: ["⌘", "E"],
+ bar: ExportVideoBar,
+ });
+
+ return null;
+}
+
+/** "1080p", "4K", with the container when it is not MP4: what a preset comes out as. */
+function describe(config: ExportConfig): string {
+ if (config.format === "ogg" || config.video?.enabled === false) return "Audio only";
+ const resolution = config.video?.resolution ?? 1080;
+ const size = resolution === 2160 ? "4K" : `${resolution}p`;
+ return config.format && config.format !== "mp4" ? `${size} · ${config.format.toUpperCase()}` : size;
+}
+
+/** The active scene, exported as a video with the preset picked. */
+function ExportVideoBar() {
+ const { close } = useCommands();
+ const { exportScene } = useExport();
+ const scene = useActiveScene();
+ const config = useProjectConfig();
+
+ const sceneSettings = createMemo(() => {
+ const active = scene();
+ return active ? config()?.exportOf(active) ?? null : null;
+ });
+
+ // The scene's own settings when it has them, else the last preset used.
+ const [picked, setPicked] = createSignal<string | null>(null);
+ const preset = () => {
+ const id = picked();
+ if (id === SCENE_SETTINGS && sceneSettings()) return id;
+ if (id !== null && TEMPLATE_BY_ID.has(id)) return id;
+ if (sceneSettings()) return SCENE_SETTINGS;
+ const last = store.get<string>(LAST_PRESET_KEY);
+ return last && TEMPLATE_BY_ID.has(last) ? last : DEFAULT_EXPORT_TEMPLATE_ID;
+ };
+
+ const groups = (): PresetGroup[] => {
+ const own = sceneSettings();
+ return [
+ ...(own ? [{ label: "Scene", presets: [{ id: SCENE_SETTINGS, name: "Scene settings", detail: describe(own) }] }] : []),
+ ...EXPORT_TEMPLATE_GROUPS.map((group) => ({
+ label: group.label,
+ presets: group.items.map((template) => ({ id: template.id, name: template.name, detail: describe(template) })),
+ })),
+ ];
+ };
+
+ const pick = (id: string) => {
+ setPicked(id);
+ if (id !== SCENE_SETTINGS) store.set(LAST_PRESET_KEY, id);
+ };
+
+ const confirm = () => {
+ const active = scene();
+ const id = preset();
+ const settings = id === SCENE_SETTINGS ? sceneSettings() : TEMPLATE_BY_ID.get(id);
+ if (!active || !settings) return;
+ close();
+ void exportScene(active, { format: settings.format, video: settings.video, audio: settings.audio });
+ };
+
+ return (
+ <ActionBar>
+ <ActionBarTarget nodes={scene() ? [scene()!] : []} verb="Export" empty="Open a scene to export" />
+ <PresetMenu groups={groups()} value={preset()} onChange={pick} tooltip="Export preset" />
+ <Separator orientation="vertical" class="min-h-5" />
+ <ActionBarCancel onCancel={close} />
+ <ActionBarConfirm disabled={!scene()} onConfirm={confirm} hint="Export video" />
+ </ActionBar>
+ );
+}
diff --git a/apps/web/src/components/commands/index.tsx b/apps/web/src/components/commands/index.tsx
index b3dbcbb9..7fcfd3d8 100644
--- a/apps/web/src/components/commands/index.tsx
+++ b/apps/web/src/components/commands/index.tsx
@@ -5,6 +5,8 @@
import { ToolType } from "@diffusionstudio/runtime";
import { AutoCaptionsCommand } from "./auto-captions";
import { EditWithPromptCommand } from "./edit-with-prompt";
+import { ExportCurrentFrameCommand } from "./export-current-frame";
+import { ExportVideoCommand } from "./export-video";
import { GenerateCommand } from "./generate";
import { MakeVideoCommand } from "./make-video";
import { RemoveBackgroundCommand } from "./remove-background";
@@ -36,6 +38,8 @@ export function CanvasCommands() {
<UpscaleCommand />
<RemoveBackgroundCommand />
<AutoCaptionsCommand />
+ <ExportVideoCommand />
+ <ExportCurrentFrameCommand />
<ToolCommand tool={ToolType.MOVE} />
<ToolCommand tool={ToolType.HAND} />
<ToolCommand tool={ToolType.OBJECT_MASK} />
diff --git a/apps/web/src/components/commands/preset-menu.tsx b/apps/web/src/components/commands/preset-menu.tsx
new file mode 100644
index 00000000..6523c7de
--- /dev/null
+++ b/apps/web/src/components/commands/preset-menu.tsx
@@ -0,0 +1,94 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { For, Show } from "solid-js";
+import { Button } from "@/components/ui/button";
+import { Icon } from "@/components/ui/icon";
+import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuGroup,
+ DropdownMenuGroupLabel,
+ DropdownMenuItem,
+ DropdownMenuItemDetail,
+ DropdownMenuPortal,
+ DropdownMenuSeparator,
+ DropdownMenuTrigger,
+} from "@/components/ui/dropdown-menu";
+import { keepMenuEscape } from "@/components/canvas/action-bar";
+
+export type Preset = {
+ id: string;
+ name: string;
+ /** What it comes out as, at the row's end: "1080p", "1920×1080". */
+ detail?: string;
+};
+
+export type PresetGroup = { label: string; presets: Preset[] };
+
+type PresetMenuProps = {
+ groups: PresetGroup[];
+ value: string;
+ onChange(id: string): void;
+ /** What the presets set, on hover. */
+ tooltip: string;
+};
+
+/** A bar's preset picker: the pick on the button, the presets by group in its menu. */
+export function PresetMenu(props: PresetMenuProps) {
+ const selected = () =>
+ props.groups.flatMap((group) => group.presets).find((preset) => preset.id === props.value);
+
+ return (
+ <DropdownMenu placement="top-start" gutter={8}>
+ <Tooltip>
+ <TooltipTrigger<typeof DropdownMenuTrigger>
+ as={(triggerProps: object) => (
+ <DropdownMenuTrigger<typeof Button>
+ {...triggerProps}
+ as={(buttonProps) => (
+ <Button {...buttonProps} variant="ghost" class="gap-1 pr-0.5 text-muted-foreground">
+ <span class="text-foreground">{selected()?.name}</span>
+ {selected()?.detail}
+ <Icon name="chevron-down" />
+ </Button>
+ )}
+ />
+ )}
+ />
+ <TooltipContent>{props.tooltip}</TooltipContent>
+ </Tooltip>
+ <DropdownMenuPortal>
+ <DropdownMenuContent class="min-w-60" onEscapeKeyDown={keepMenuEscape}>
+ <For each={props.groups}>
+ {(group, index) => (
+ <>
+ <Show when={index() > 0}>
+ <DropdownMenuSeparator />
+ </Show>
+ <DropdownMenuGroup>
+ <DropdownMenuGroupLabel>{group.label}</DropdownMenuGroupLabel>
+ <For each={group.presets}>
+ {(preset) => (
+ <DropdownMenuItem class="px-0 pr-2 gap-0.5" onSelect={() => props.onChange(preset.id)}>
+ <div classList={{ visible: preset.id === props.value }} class="invisible">
+ <Icon name="confirm-check" class="text-foreground" />
+ </div>
+ <span class="mx-1 min-w-0 flex-1 truncate">{preset.name}</span>
+ <Show when={preset.detail}>
+ <DropdownMenuItemDetail class="pl-6 whitespace-nowrap">{preset.detail}</DropdownMenuItemDetail>
+ </Show>
+ </DropdownMenuItem>
+ )}
+ </For>
+ </DropdownMenuGroup>
+ </>
+ )}
+ </For>
+ </DropdownMenuContent>
+ </DropdownMenuPortal>
+ </DropdownMenu>
+ );
+}
diff --git a/apps/web/src/context/commands.tsx b/apps/web/src/context/commands.tsx
index 26df5812..0cdef395 100644
--- a/apps/web/src/context/commands.tsx
+++ b/apps/web/src/context/commands.tsx
@@ -16,7 +16,7 @@ import type { Accessor, Component, JSX, Owner } from "solid-js";
* taken from the order commands register in, which depends on what mounted
* first and changes as parts of the UI come and go.
*/
-export const SECTIONS = ["Media", "Generate", "Assets", "Timeline", "Object", "Tools", "Edit", "Window"] as const;
+export const SECTIONS = ["Media", "Generate", "Assets", "Export", "Timeline", "Object", "Tools", "Edit", "Window"] as const;
export type Section = (typeof SECTIONS)[number];
diff --git a/apps/web/src/context/export.tsx b/apps/web/src/context/export.tsx
index e7988564..d57cccf1 100644
--- a/apps/web/src/context/export.tsx
+++ b/apps/web/src/context/export.tsx
@@ -6,10 +6,11 @@ import { createContext, useContext, onCleanup, onMount } from "solid-js";
import { toast } from "somoto";
import { canEncodeVideo } from "mediabunny";
import { useWorld } from "@diffusionstudio/koota-solid";
-import { computeOutputSize } from "@diffusionstudio/encoder";
-import { Computed, getActiveEntity } from "@diffusionstudio/runtime";
-import { assert, downloadObject, isInputTarget } from "@/utils";
+import { computeOutputSize, createImageEncoder } from "@diffusionstudio/encoder";
+import { Computed, Workarea, getActiveEntity, store } from "@diffusionstudio/runtime";
+import { assert, isInputTarget } from "@/utils";
import { useEngineContext } from "@/engine";
+import { createCapture } from "@/engine/capture";
import { useProject } from "@/context/project";
import { ExportProgress } from "@/components/sidebar-right/inspector/export-progress";
import { renderScene, renderOverlay, cancelRender, type ExportConfig } from "@/context/render";
@@ -23,7 +24,10 @@ import type { JSX, Accessor } from "solid-js";
type ExportContextValue = {
exportScene: (scene: Entity, config: ExportConfig) => Promise<void>;
+ /** Writes the frame at the active scene's playhead as a PNG, at the scene's own size. */
exportCurrentFrame: () => Promise<void>;
+ /** Writes the frame at `scene`'s playhead as a PNG, its shorter side `resolution` px (the scene's own when left out). */
+ exportFrame: (scene: Entity, resolution?: number) => Promise<void>;
exporting: Accessor<boolean>;
};
@@ -114,15 +118,68 @@ export function ExportProvider(props: { children: JSX.Element }) {
};
const exportCurrentFrame: ExportContextValue["exportCurrentFrame"] = async () => {
- const blob = await engine.snapshot();
+ const scene = getActiveEntity(world);
+ if (scene === null) {
+ toast("No active scene to export");
+ return;
+ }
+ await exportFrame(scene);
+ };
+
+ const exportFrame: ExportContextValue["exportFrame"] = async (scene, resolution) => {
+ if (!scene?.isAlive()) return;
+
+ // The frame under the playhead, counted the way the image encoder counts:
+ // from the export's first frame, the work area's start.
+ const computed = scene.get(Computed);
+ const workarea = scene.get(Workarea);
+ const start = workarea?.start ?? 0;
+ const end = workarea?.end ?? computed?.duration ?? 0;
+ const playhead = store(world, Computed).localTime[scene.id()] ?? 0;
+ const frame = Math.max(0, Math.min(playhead, end - 1) - start);
+
+ // The encoder sizes by height; a resolution names the shorter side.
+ const { height } = computeOutputSize(computed?.width || 1920, computed?.height || 1080, resolution);
+
+ const name = project.name().replace(/\s+/g, "-").toLowerCase();
- if (!blob) {
- toast.error("Failed to capture frame");
+ let target: FileSystemFileHandle;
+ try {
+ target = await window.showSaveFilePicker({
+ suggestedName: `${name}.png`,
+ types: [{ description: "png", accept: { "image/png": [".png"] } }],
+ });
+ } catch (e) {
+ if ((e as Error).name === "AbortError") return;
+ toast.error("Failed to start export", { description: (e as Error).message });
return;
}
- const projectName = project.name().replace(/\s+/g, "-").toLowerCase();
- await downloadObject(blob, `${projectName}-frame.png`);
+ const progress = toast.loading("Exporting image...");
+ let capture: Awaited<ReturnType<typeof createCapture>> | undefined;
+ try {
+ capture = await createCapture(world, scene, { dir: project.dir() });
+ const encoder = await createImageEncoder(capture.world, { frames: [frame], resolution: height });
+ const result = await encoder.render();
+ if (result.type === "canceled") {
+ toast.dismiss(progress);
+ return;
+ }
+ if (result.type === "error") throw result.error;
+
+ const writable = await target.createWritable();
+ // Copied onto a plain ArrayBuffer, which is what a file write takes.
+ await writable.write(new Uint8Array(result.data[0]!.png));
+ await writable.close();
+ toast.dismiss(progress);
+ toast("Export complete", { description: "Your image has been successfully exported" });
+ } catch (e) {
+ console.error("Image export failed:", e);
+ toast.dismiss(progress);
+ toast.error("Export failed", { description: (e as Error).message });
+ } finally {
+ capture?.dispose();
+ }
};
const exportActiveScene = () => {
@@ -136,7 +193,7 @@ export function ExportProvider(props: { children: JSX.Element }) {
/**
* Export is the provider's command, so its keys are bound here rather than
* in the engine's shortcut table: ⌘E writes the active scene, ⇧⌘E the frame
- * on screen — the same two the File menu lists.
+ * at its playhead — the same two the File menu lists.
*/
const handleShortcut = (event: KeyboardEvent) => {
if (!(event.metaKey || event.ctrlKey) || event.key.toLowerCase() !== "e") return;
@@ -151,7 +208,7 @@ export function ExportProvider(props: { children: JSX.Element }) {
onCleanup(() => window.removeEventListener("keydown", handleShortcut));
return (
- <ExportContext.Provider value={{ exportScene, exportCurrentFrame, exporting }}>
+ <ExportContext.Provider value={{ exportScene, exportCurrentFrame, exportFrame, exporting }}>
{props.children}
<ExportProgress
open={!!renderOverlay()}