Skip to content

The VS Code of video editing, built for agents.

TypeScript

Enhance export functionality with resolution handling

konstantin-paulusf0c847fb2838
diff --git a/apps/web/src/components/sidebar-right/inspector/export-progress.tsx b/apps/web/src/components/sidebar-right/inspector/export-progress.tsx
index 89787ba9..c1ab7f4f 100644
--- a/apps/web/src/components/sidebar-right/inspector/export-progress.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/export-progress.tsx
@@ -19,6 +19,8 @@ type ExportProgressProps = {
progress: number;
remaining?: { minutes: number; seconds: number };
config?: ExportConfig;
+ width: number;
+ height: number;
duration: number;
onCancel: () => void;
};
@@ -26,12 +28,6 @@ type ExportProgressProps = {
export function ExportProgress(props: ExportProgressProps) {
const cfg = () => props.config;
- const width = () => {
- const resolution = cfg()?.video?.resolution ?? 1080;
- return Math.round((resolution * 16) / 9 / 2) * 2;
- };
-
- const height = () => cfg()?.video?.resolution ?? 1080;
const format = () => (cfg()?.format ?? "mp4").toUpperCase();
const fps = () => cfg()?.video?.fps ?? 30;
const videoCodec = () => cfg()?.video?.codec ?? "avc";
@@ -63,7 +59,7 @@ export function ExportProgress(props: ExportProgressProps) {
<p>{formatDuration(props.duration)}</p>
<p>
- {width()} x {height()}
+ {props.width} x {props.height}
</p>
<p>{bitrate()}</p>
<p>{fps()} FPS</p>
diff --git a/apps/web/src/components/sidebar-right/inspector/export.tsx b/apps/web/src/components/sidebar-right/inspector/export.tsx
index bd3d46ed..b505a6b8 100644
--- a/apps/web/src/components/sidebar-right/inspector/export.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/export.tsx
@@ -2,7 +2,9 @@
* 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 { Show, createMemo, createSignal } from "solid-js";
+import { Show, createMemo, createResource, createSignal } from "solid-js";
+import { canEncodeVideo } from "mediabunny";
+import { computeOutputSize } from "@diffusionstudio/encoder";
import { PanelSection } from "@/components/ui/panel-section";
import { Button } from "@/components/ui/button";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
@@ -52,6 +54,29 @@ type ExportPanelProps = {
selection: Entity[];
};
+/**
+ * One entry of the resolution picker: the stored setting (a target height)
+ * together with the pixel size it works out to for this scene, so the picker
+ * names what the file will actually be.
+ */
+type ResolutionOption = {
+ resolution: number;
+ width: number;
+ height: number;
+ /** 1× — the scene's own size. */
+ native: boolean;
+};
+
+/** The right-hand tag of a picker entry, set like a menu shortcut. */
+function resolutionTag(option: ResolutionOption): string {
+ return option.native ? "Original" : `${option.resolution}p`;
+}
+
+function resolutionLabel(option: ResolutionOption | null | undefined): string {
+ if (!option) return "";
+ return `${option.width}×${option.height} · ${resolutionTag(option)}`;
+}
+
/** The whole settings a template stands for: every field, so it replaces what was there. */
function templateSettings(id: string): ProjectExportConfig | null {
const template = TEMPLATE_BY_ID.get(id);
@@ -87,21 +112,65 @@ export function ExportPanel(props: ExportPanelProps) {
const frameRate = useTrait(world, FrameRate);
const duration = useDerived(() => entity().get(Computed)?.duration ?? 0);
const durationInSeconds = createMemo(() => duration() / (frameRate()?.value ?? 30));
+ const sceneWidth = useDerived(() => entity().get(Computed)?.width || 1920);
+ const sceneHeight = useDerived(() => entity().get(Computed)?.height || 1080);
const template = createMemo(() => {
const id = settings()?.template;
return id ? TEMPLATE_BY_ID.get(id) ?? null : null;
});
+ const resolutionOptions = createMemo<ResolutionOption[]>(() => {
+ const w = sceneWidth();
+ const h = sceneHeight();
+ return [...new Set([...RESOLUTION_OPTIONS, h])].sort((a, b) => a - b).map((resolution) => ({
+ resolution,
+ ...computeOutputSize(w, h, resolution),
+ native: resolution === h,
+ }));
+ });
+
+ const selectedResolution = createMemo(() => {
+ const resolution = settings()?.video?.resolution ?? 1080;
+ const match = resolutionOptions().find((option) => option.resolution === resolution);
+ if (match) return match;
+ const w = sceneWidth();
+ const h = sceneHeight();
+ return { resolution, ...computeOutputSize(w, h, resolution), native: resolution === h };
+ });
+
+ const [encodable] = createResource(
+ () => {
+ const current = settings();
+ const size = selectedResolution();
+ if (!current || !size) return undefined;
+ if (current.format === "ogg" || current.video?.enabled === false) return undefined;
+ return {
+ codec: current.video?.codec ?? ("avc" as const),
+ bitrate: current.video?.bitrate ?? 10e6,
+ width: size.width,
+ height: size.height,
+ };
+ },
+ ({ codec, bitrate, width, height }) => canEncodeVideo(codec, { width, height, bitrate }),
+ );
+
+ // Audio-only exports always encode
+ const exportSupported = createMemo(() => {
+ const current = settings();
+ if (!current || current.format === "ogg" || current.video?.enabled === false) return true;
+ return encodable() !== false;
+ });
+
// What the row says: the preset's name, or "Custom" for settings authored
- // without one, and the resolution the export actually uses (a preset's
+ // without one, and the pixel size the export actually produces (a preset's
// resolution can be changed in the inspector without leaving the preset).
const label = createMemo(() => {
const current = settings();
if (!current) return "";
const name = template()?.name ?? "Custom";
- const resolution = current.video?.resolution ?? template()?.video?.resolution;
- return resolution ? `${name} · ${resolution}p` : name;
+ const size = selectedResolution();
+ return size ? `${name} · ${size.width}×${size.height}` : name;
});
const write = (value: ProjectExportConfig | null) => {
@@ -148,7 +217,10 @@ export function ExportPanel(props: ExportPanelProps) {
});
};
- const estimatedFileSize = createMemo(() => estimateFileSize(settings(), durationInSeconds()));
+ const estimatedFileSize = createMemo(() => {
+ const size = selectedResolution();
+ return estimateFileSize(settings(), durationInSeconds(), size ? size.width * size.height : undefined);
+ });
let inspectorAnchorRef: HTMLDivElement | undefined;
@@ -192,9 +264,18 @@ export function ExportPanel(props: ExportPanelProps) {
<TooltipContent>Remove export</TooltipContent>
</Tooltip>
</ItemRow>
- <Button class="w-full" onClick={runExport} disabled={exporting()}>
- Export
- </Button>
+ <Tooltip disabled={exportSupported()}>
+ <TooltipTrigger as="div" class="w-full">
+ <Button class="w-full" onClick={runExport} disabled={exporting() || !exportSupported()}>
+ Export
+ </Button>
+ </TooltipTrigger>
+ <TooltipContent class="max-w-64">
+ This browser cannot encode {(settings()?.video?.codec ?? "avc").toUpperCase()} at{" "}
+ {selectedResolution()?.width}×{selectedResolution()?.height}. Choose a lower
+ resolution, a lower bitrate, or another codec.
+ </TooltipContent>
+ </Tooltip>
<div class="flex justify-between items-center h-7">
<span class="text-base text-muted-foreground">
Duration {formatDuration(durationInSeconds())}
@@ -208,6 +289,8 @@ export function ExportPanel(props: ExportPanelProps) {
{(current) => (
<ExportInspector
settings={current()}
+ resolutionOptions={resolutionOptions()}
+ selectedResolution={selectedResolution()}
anchorRef={inspectorAnchorRef}
onClose={() => setIsInspectorOpen(false)}
onSelectTemplate={applyTemplate}
@@ -221,6 +304,8 @@ export function ExportPanel(props: ExportPanelProps) {
type ExportInspectorProps = {
settings: ProjectExportConfig;
+ resolutionOptions: ResolutionOption[];
+ selectedResolution: ResolutionOption | null;
anchorRef: HTMLDivElement | undefined;
onClose: () => void;
onSelectTemplate: (id: string) => void;
@@ -293,16 +378,39 @@ function ExportInspector(props: ExportInspectorProps) {
}}
>
<ControlRow label="Resolution">
- <Select
- value={config().video?.resolution ?? 1080}
- options={[...RESOLUTION_OPTIONS]}
- onChange={(value) => value && write({ video: { resolution: value } })}
- itemComponent={(itemProps) => (
- <SelectItem item={itemProps.item}>{itemProps.item.rawValue}p</SelectItem>
- )}
+ <Select<ResolutionOption>
+ value={props.selectedResolution ?? undefined}
+ options={props.resolutionOptions}
+ optionValue="resolution"
+ optionTextValue={resolutionLabel}
+ onChange={(value) => value && write({ video: { resolution: value.resolution } })}
+ itemComponent={(itemProps) => {
+ const option = itemProps.item.rawValue;
+ return (
+ <SelectItem item={itemProps.item}>
+ <div class="flex min-w-0 flex-1 items-center">
+ <span>{option.width}×{option.height}</span>
+ <span class="ml-auto pl-3 text-xxs text-muted-foreground group-[[data-highlighted]]:text-[inherit]">
+ {resolutionTag(option)}
+ </span>
+ </div>
+ </SelectItem>
+ );
+ }}
>
<SelectTrigger>
- <SelectValue>{config().video?.resolution ?? 1080}p</SelectValue>
+ <SelectValue<ResolutionOption>>
+ <Show when={props.selectedResolution}>
+ {(selected) => (
+ <>
+ <span>{selected().width}×{selected().height}</span>
+ <span class="ml-auto text-xxs text-muted-foreground">
+ {resolutionTag(selected())}
+ </span>
+ </>
+ )}
+ </Show>
+ </SelectValue>
</SelectTrigger>
<SelectPortal>
<SelectContent />
@@ -437,6 +545,7 @@ function ExportInspector(props: ExportInspectorProps) {
const DEFAULT_ESTIMATE_VIDEO_BITRATE = 10e6;
const DEFAULT_ESTIMATE_AUDIO_BITRATE = 128e3;
const DEFAULT_ESTIMATE_RESOLUTION = 1080;
+const DEFAULT_ESTIMATE_PIXELS = 1920 * 1080;
const DEFAULT_ESTIMATE_FRAME_RATE = 30;
const VIDEO_CODEC_SIZE_FACTORS: Partial<Record<VideoCodec, number>> = {
@@ -459,7 +568,7 @@ const CONTAINER_SIZE_FACTORS: Record<ContainerFormat, number> = {
mov: 1.03,
};
-function estimateFileSize(config?: ExportConfig, duration?: number) {
+function estimateFileSize(config?: ExportConfig, duration?: number, outputPixels?: number) {
if (!duration || duration <= 0) return 0;
const format = config?.format ?? "mp4";
@@ -479,7 +588,12 @@ function estimateFileSize(config?: ExportConfig, duration?: number) {
const audioCodecFactor = AUDIO_CODEC_SIZE_FACTORS[audioCodec] ?? 1;
const containerFactor = CONTAINER_SIZE_FACTORS[format];
- const resolutionFactor = Math.pow(resolution / DEFAULT_ESTIMATE_RESOLUTION, 1.08);
+ // Scaled by pixel count when the actual output size is known (for 16:9
+ // content the two expressions agree); the height-only fallback stays for
+ // callers with nothing but the config.
+ const resolutionFactor = outputPixels
+ ? Math.pow(outputPixels / DEFAULT_ESTIMATE_PIXELS, 0.54)
+ : Math.pow(resolution / DEFAULT_ESTIMATE_RESOLUTION, 1.08);
const frameRateFactor = fps / DEFAULT_ESTIMATE_FRAME_RATE;
const effectiveVideoBitrate =
diff --git a/apps/web/src/context/export.tsx b/apps/web/src/context/export.tsx
index deb0eb90..43054dd6 100644
--- a/apps/web/src/context/export.tsx
+++ b/apps/web/src/context/export.tsx
@@ -4,7 +4,9 @@
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, FrameRate, getActiveEntity } from "@diffusionstudio/runtime";
import { assert, downloadObject, isInputTarget } from "@/utils";
import { useEngineContext } from "@/engine";
@@ -44,6 +46,33 @@ export function ExportProvider(props: { children: JSX.Element }) {
const format = config.format ?? "mp4";
const mimeType = MIME_TYPES[format];
+ // Fail before the save picker: an unencodable configuration is known
+ // right away, and the encoder would only find out after a file was
+ // picked and the render machinery spun up.
+ const videoEnabled = format !== "ogg" && config.video?.enabled !== false;
+ if (videoEnabled) {
+ const computed = scene.get(Computed);
+ const codec = config.video?.codec ?? "avc";
+ const { width, height } = computeOutputSize(
+ computed?.width || 1920,
+ computed?.height || 1080,
+ config.video?.resolution ?? 1080,
+ );
+ const encodable = await canEncodeVideo(codec, {
+ width,
+ height,
+ bitrate: config.video?.bitrate ?? 10e6,
+ });
+ if (!encodable) {
+ toast.error("Export not supported", {
+ description:
+ `This browser cannot encode ${codec.toUpperCase()} at ${width}×${height}. ` +
+ "Choose a lower resolution, a lower bitrate, or another codec.",
+ });
+ return;
+ }
+ }
+
const name = project.name().replace(/\s+/g, "-").toLowerCase();
let target: FileSystemFileHandle;
@@ -160,6 +189,8 @@ export function ExportProvider(props: { children: JSX.Element }) {
progress={renderOverlay()?.progress ?? 0}
remaining={renderOverlay()?.remaining}
config={renderOverlay()?.config as ExportConfig | undefined}
+ width={renderOverlay()?.width ?? 0}
+ height={renderOverlay()?.height ?? 0}
duration={renderOverlay()?.duration ?? 0}
onCancel={cancelRender}
/>
diff --git a/apps/web/src/context/render.ts b/apps/web/src/context/render.ts
index 17d2c94c..ab2eca7b 100644
--- a/apps/web/src/context/render.ts
+++ b/apps/web/src/context/render.ts
@@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { createSignal } from "solid-js";
-import { createEncoder } from "@diffusionstudio/encoder";
+import { createEncoder, computeOutputSize } from "@diffusionstudio/encoder";
import { Computed, FrameRate, Workarea } from "@diffusionstudio/runtime";
import { createCapture } from "@/engine/capture";
@@ -24,6 +24,9 @@ import type { ExportConfig } from "@/components/sidebar-right/inspector/export-p
export type RenderOverlayState = {
config?: Partial<ExportConfig>;
+ /** The encode's actual pixel size, from the scene's own aspect ratio. */
+ width: number;
+ height: number;
duration: number;
progress: number;
remaining?: { minutes: number; seconds: number };
@@ -58,13 +61,22 @@ export async function renderScene(
const world = engine.world;
const workarea = scene.get(Workarea);
+ const computed = scene.get(Computed);
const frames = workarea
? workarea.end - workarea.start
- : scene.get(Computed)?.duration ?? 0;
+ : computed?.duration ?? 0;
const duration = frames / (world.get(FrameRate)?.value || 30);
+ // The same size the encoder works out for itself, so the overlay reports
+ // the dimensions actually encoded — the scene's aspect ratio, not 16:9.
+ const { width, height } = computeOutputSize(
+ computed?.width || 1920,
+ computed?.height || 1080,
+ config?.video?.resolution ?? 1080,
+ );
+
cancelActive = undefined;
- setOverlay({ config, duration, progress: 0, remaining: undefined });
+ setOverlay({ config, width, height, duration, progress: 0, remaining: undefined });
engine.stop();
diff --git a/packages/encoder/src/encoder.ts b/packages/encoder/src/encoder.ts
index 61d75fda..71afb36e 100644
--- a/packages/encoder/src/encoder.ts
+++ b/packages/encoder/src/encoder.ts
@@ -7,6 +7,7 @@ import {
Output,
AudioSample,
AudioSampleSource,
+ canEncodeVideo,
} from 'mediabunny';
import { Not } from 'koota';
import {
@@ -23,7 +24,7 @@ import {
import { TargetBuffer } from './buffer';
import { createOutputFormat } from './format';
-import { createRenderEventDetail } from './utils';
+import { computeOutputSize, createRenderEventDetail } from './utils';
import type { Entity, World } from 'koota';
import type { EncoderConfig } from './interfaces';
@@ -99,11 +100,22 @@ export async function createEncoder(world: World, config: EncoderConfig) {
const videoCodec = config.video?.codec ?? 'avc';
const containerFormat = config.format ?? 'mp4';
const resolution = config.video?.resolution ?? 1080;
- const scale = Math.round(resolution * 1e6 / sceneHeight) / 1e6;
- const width = Math.round(sceneWidth * scale / 2) * 2;
- const height = Math.round(sceneHeight * scale / 2) * 2;
+ const { scale, width, height } = computeOutputSize(sceneWidth, sceneHeight, resolution);
const frameDuration = 1 / frameRate;
+ // Fail before anything is written: mediabunny would reject the config on
+ // the first frame anyway, with a message in codec strings; this one names
+ // the size the resolution setting worked out to, which is what the user
+ // can change.
+ if (videoEnabled) {
+ const encodable = await canEncodeVideo(videoCodec, { width, height, bitrate: videoBitrate });
+ assert(
+ encodable,
+ `This browser cannot encode ${videoCodec.toUpperCase()} at ${width}×${height}. ` +
+ 'Choose a lower resolution, a lower bitrate, or another codec.',
+ );
+ }
+
const canvas = world.get(RenderSurface)?.canvas;
assert(canvas instanceof HTMLCanvasElement, 'The capture world has no canvas to draw into');
canvas.width = width;
diff --git a/packages/encoder/src/image-encoder.ts b/packages/encoder/src/image-encoder.ts
index 85441422..0a22f632 100644
--- a/packages/encoder/src/image-encoder.ts
+++ b/packages/encoder/src/image-encoder.ts
@@ -10,6 +10,7 @@ import {
} from '@diffusionstudio/runtime';
import { captureScene, normalizeSceneTransform, resolverSystem, warmupAssets } from './encoder';
+import { computeOutputSize } from './utils';
import type { World } from 'koota';
import type { ImageEncoderConfig } from './interfaces';
@@ -82,10 +83,10 @@ export async function createImageEncoder(world: World, config: ImageEncoderConfi
const resize = (height?: number) => {
// The export's own scale and rounding, so a capture at a resolution is
// pixel-for-pixel the encoded frame at that resolution.
- const scale = Math.round((height ?? sceneHeight) * 1e6 / sceneHeight) / 1e6;
- canvas.width = Math.round(sceneWidth * scale / 2) * 2;
- canvas.height = Math.round(sceneHeight * scale / 2) * 2;
- world.set(RenderSurface, { resolution: scale });
+ const output = computeOutputSize(sceneWidth, sceneHeight, height);
+ canvas.width = output.width;
+ canvas.height = output.height;
+ world.set(RenderSurface, { resolution: output.scale });
};
resize(config.resolution);
diff --git a/packages/encoder/src/interfaces.ts b/packages/encoder/src/interfaces.ts
index 8cf34572..c862bc60 100644
--- a/packages/encoder/src/interfaces.ts
+++ b/packages/encoder/src/interfaces.ts
@@ -70,11 +70,13 @@ export interface VideoConfig {
fps?: number;
/**
- * Resolution of the composition
+ * Target output height in pixels. The scene is scaled uniformly until it
+ * is this tall, so the output width follows the scene's aspect ratio —
+ * a 1920×1080 scene at 2160 encodes as 3840×2160, a 3840×1620 scene at
+ * 2160 as 5120×2160. See `computeOutputSize` for the exact rounding.
* @example
- * 1080 for 1080p
- * 1440 for 1440p
- * 2160 for 4k
+ * 1080 for an output 1080px tall
+ * 2160 for an output 2160px tall
* @default 1080
*/
resolution?: number;
diff --git a/packages/encoder/src/utils.ts b/packages/encoder/src/utils.ts
index 99496a5a..addfa9ae 100644
--- a/packages/encoder/src/utils.ts
+++ b/packages/encoder/src/utils.ts
@@ -2,6 +2,33 @@
* 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/. */
+/** The exact pixel size an export produces, and the scale it draws at. */
+export interface OutputSize {
+ width: number;
+ height: number;
+ scale: number;
+}
+
+/**
+ * The output size an export at `resolution` produces for a scene: the
+ * resolution is a target height, the scene is scaled to reach it, and both
+ * sides are rounded to even for the encoders. The one formula the video
+ * encoder, the image encoder and the UI all share — what is shown to the
+ * user is what is encoded.
+ */
+export function computeOutputSize(
+ sceneWidth: number,
+ sceneHeight: number,
+ resolution?: number,
+): OutputSize {
+ const scale = Math.round((resolution ?? sceneHeight) * 1e6 / sceneHeight) / 1e6;
+ return {
+ scale,
+ width: Math.round(sceneWidth * scale / 2) * 2,
+ height: Math.round(sceneHeight * scale / 2) * 2,
+ };
+}
+
/**
* Helper for creating the render event detail
*/