Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor export resolution handling and update utility functions

konstantin-paulus814caddf1d43
diff --git a/apps/web/src/components/sidebar-right/inspector/export.tsx b/apps/web/src/components/sidebar-right/inspector/export.tsx
index b505a6b8..76dcb346 100644
--- a/apps/web/src/components/sidebar-right/inspector/export.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/export.tsx
@@ -123,10 +123,13 @@ export function ExportPanel(props: ExportPanelProps) {
const resolutionOptions = createMemo<ResolutionOption[]>(() => {
const w = sceneWidth();
const h = sceneHeight();
- return [...new Set([...RESOLUTION_OPTIONS, h])].sort((a, b) => a - b).map((resolution) => ({
+ // A resolution names the output's shorter side (the "p" number — for a
+ // vertical scene, its width), so a portrait scene still offers 720p–4K.
+ const shortSide = Math.min(w, h);
+ return [...new Set([...RESOLUTION_OPTIONS, shortSide])].sort((a, b) => a - b).map((resolution) => ({
resolution,
...computeOutputSize(w, h, resolution),
- native: resolution === h,
+ native: resolution === shortSide,
}));
});
@@ -136,7 +139,7 @@ export function ExportPanel(props: ExportPanelProps) {
if (match) return match;
const w = sceneWidth();
const h = sceneHeight();
- return { resolution, ...computeOutputSize(w, h, resolution), native: resolution === h };
+ return { resolution, ...computeOutputSize(w, h, resolution), native: resolution === Math.min(w, h) };
});
const [encodable] = createResource(
diff --git a/packages/encoder/src/image-encoder.ts b/packages/encoder/src/image-encoder.ts
index 0a22f632..c0bf0f48 100644
--- a/packages/encoder/src/image-encoder.ts
+++ b/packages/encoder/src/image-encoder.ts
@@ -10,7 +10,7 @@ import {
} from '@diffusionstudio/runtime';
import { captureScene, normalizeSceneTransform, resolverSystem, warmupAssets } from './encoder';
-import { computeOutputSize } from './utils';
+import { scaleSize } from './utils';
import type { World } from 'koota';
import type { ImageEncoderConfig } from './interfaces';
@@ -81,9 +81,8 @@ export async function createImageEncoder(world: World, config: ImageEncoderConfi
/** Re-target the output height; callers that lay frames out do this once sized. */
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 output = computeOutputSize(sceneWidth, sceneHeight, height);
+ const scale = Math.round((height ?? sceneHeight) * 1e6 / sceneHeight) / 1e6;
+ const output = scaleSize(sceneWidth, sceneHeight, scale);
canvas.width = output.width;
canvas.height = output.height;
world.set(RenderSurface, { resolution: output.scale });
diff --git a/packages/encoder/src/interfaces.ts b/packages/encoder/src/interfaces.ts
index c862bc60..0038343f 100644
--- a/packages/encoder/src/interfaces.ts
+++ b/packages/encoder/src/interfaces.ts
@@ -70,13 +70,14 @@ export interface VideoConfig {
fps?: number;
/**
- * 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.
+ * Target size of the output's shorter side in pixels — the "p" number,
+ * which for a vertical video is by convention its width. The scene is
+ * scaled uniformly until its shorter side reaches it: at 2160, a
+ * 1920×1080 scene encodes as 3840×2160, a portrait 1080×1920 one as
+ * 2160×3840. See `computeOutputSize` for the exact rounding.
* @example
- * 1080 for an output 1080px tall
- * 2160 for an output 2160px tall
+ * 1080 for 1080p
+ * 2160 for 4K
* @default 1080
*/
resolution?: number;
diff --git a/packages/encoder/src/utils.ts b/packages/encoder/src/utils.ts
index addfa9ae..54f10283 100644
--- a/packages/encoder/src/utils.ts
+++ b/packages/encoder/src/utils.ts
@@ -10,18 +10,11 @@ export interface OutputSize {
}
/**
- * 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.
+ * A scene size scaled and rounded to even the way the encoders take it —
+ * the one rounding the video encoder, the image encoder and the UI share,
+ * so 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;
+export function scaleSize(sceneWidth: number, sceneHeight: number, scale: number): OutputSize {
return {
scale,
width: Math.round(sceneWidth * scale / 2) * 2,
@@ -29,6 +22,24 @@ export function computeOutputSize(
};
}
+/**
+ * The output size an export at `resolution` produces for a scene. The
+ * resolution names the output's shorter side — the "p" number, which for
+ * a vertical video is by convention its width: at 1080, a 1920×1080 scene
+ * exports as it is, a portrait 1080×1920 one does too, and a 3840×1620
+ * ultrawide is scaled down to 2560×1080. Without a resolution the scene
+ * exports at its own size.
+ */
+export function computeOutputSize(
+ sceneWidth: number,
+ sceneHeight: number,
+ resolution?: number,
+): OutputSize {
+ const shortSide = Math.min(sceneWidth, sceneHeight);
+ const scale = Math.round((resolution ?? shortSide) * 1e6 / shortSide) / 1e6;
+ return scaleSize(sceneWidth, sceneHeight, scale);
+}
+
/**
* Helper for creating the render event detail
*/