Skip to content

The VS Code of video editing, built for agents.

TypeScript

Enhance rendering and capture functionality with project directory support

konstantin-paulus6e51eb878ebb
diff --git a/apps/web/src/context/dapi/api.tsx b/apps/web/src/context/dapi/api.tsx
index 3c0a83e8..52cb2b83 100644
--- a/apps/web/src/context/dapi/api.tsx
+++ b/apps/web/src/context/dapi/api.tsx
@@ -86,7 +86,7 @@ function createAppRouter({ world, project, getUser, requireAuth }: AppRouterDeps
return t.router({
whoami: t.procedure.query(() => getUser()),
context: q0(handleContextGet(world, project)),
- capture: q(handleCapture(world)),
+ capture: q(handleCapture(world, project)),
models: q(handleModels()),
logs: q(handleLogs()),
screenshot: q0(handleWindowScreenshot()),
diff --git a/apps/web/src/context/dapi/capture.ts b/apps/web/src/context/dapi/capture.ts
index e6cad4c2..d3fb2e4e 100644
--- a/apps/web/src/context/dapi/capture.ts
+++ b/apps/web/src/context/dapi/capture.ts
@@ -16,6 +16,7 @@ import { createCapture } from "@/engine/capture";
import type { CaptureRequest, CaptureResult, TimecodedImage } from "@diffusionstudio/cli/channels";
import type { Entity, World } from "koota";
+import type { ProjectContextValue } from "@/context/project";
// Scenes are nodes too, so capture accepts them alongside geometry, groups,
// and adjustment layers.
@@ -41,7 +42,7 @@ function resolveNode(world: World, id: number): Entity {
// Ceiling on the height a sheet cell renders a node at.
const SHEET_CAPTURE_HEIGHT = 1080;
-export function handleCapture(world: World) {
+export function handleCapture(world: World, project: ProjectContextValue) {
return async ({ id, frames, combine = true, perSheet }: CaptureRequest): Promise<CaptureResult> => {
const node = resolveNode(world, id);
@@ -53,7 +54,7 @@ export function handleCapture(world: World) {
// The project re-rendered into a world of its own, reduced to this node:
// the same arrangement an export runs against, and the encoder's to draw.
- const capture = await createCapture(world, node);
+ const capture = await createCapture(world, node, { dir: project.dir() });
try {
const encoder = await createImageEncoder(capture.world, {
diff --git a/apps/web/src/context/export.tsx b/apps/web/src/context/export.tsx
index 38c2fc12..deb0eb90 100644
--- a/apps/web/src/context/export.tsx
+++ b/apps/web/src/context/export.tsx
@@ -78,7 +78,7 @@ export function ExportProvider(props: { children: JSX.Element }) {
const startedAt = performance.now();
try {
- const result = await renderScene(engine, { scene, target, config });
+ const result = await renderScene(engine, { scene, target, config, dir: project.dir() });
if (result.type === "error") {
console.error("Export failed:", result.error);
diff --git a/apps/web/src/context/render.ts b/apps/web/src/context/render.ts
index 68ee13ca..9def1a69 100644
--- a/apps/web/src/context/render.ts
+++ b/apps/web/src/context/render.ts
@@ -47,11 +47,13 @@ export type RenderSceneOptions = {
target: NonNullable<EncoderConfig["target"]>;
/** Encoder settings (resolution, codecs, format, ...). */
config?: Partial<EncoderConfig>;
+ /** The project's folder, so the encode compiles the sources as they are now. */
+ dir?: string;
};
export async function renderScene(
engine: Engine,
- { scene, target, config }: RenderSceneOptions,
+ { scene, target, config, dir }: RenderSceneOptions,
): Promise<ExportResult> {
const world = engine.world;
@@ -69,6 +71,7 @@ export async function renderScene(
let capture: Capture | undefined;
try {
capture = await createCapture(world, scene, {
+ dir,
frameRate: config?.video?.fps,
mode: config?.video?.enabled === false || config?.format === "ogg" ? "offline-audio" : "offline-video",
});
diff --git a/apps/web/src/engine/capture.ts b/apps/web/src/engine/capture.ts
index f3215425..fa6bc2d3 100644
--- a/apps/web/src/engine/capture.ts
+++ b/apps/web/src/engine/capture.ts
@@ -28,6 +28,7 @@ import {
} from '@diffusionstudio/runtime';
import { loadProjectBundle } from '@/lib/db';
+import { compileProject, isDesktop } from '@/projects';
import type { RuntimeMode } from '@diffusionstudio/runtime';
import type { Entity, World } from 'koota';
@@ -37,6 +38,27 @@ export interface CaptureOptions {
frameRate?: number;
/** What is being encoded. Audio-only skips nothing, it just draws nothing. */
mode?: RuntimeMode;
+ /** The project's folder, to compile it fresh; the remembered bundle otherwise. */
+ dir?: string;
+}
+
+/**
+ * The code an encode mounts. Compiled fresh when the project's folder is
+ * known: an element the editor inserted reaches the file without a recompile
+ * (its own writes are kept from the watcher), so the bundle the last mount
+ * remembered may predate it — and an encode of it would come up empty. The
+ * remembered bundle answers off the desktop, where there is no compiler.
+ */
+async function bundleFor(source: World, dir?: string): Promise<string> {
+ if (dir && isDesktop()) {
+ const result = await compileProject(dir);
+ if (!result.ok) throw new Error(result.error);
+ return result.code;
+ }
+
+ const code = await loadProjectBundle(source.get(Project)?.id ?? '');
+ if (!code) throw new Error('There is no project to render');
+ return code;
}
export interface Capture {
@@ -51,13 +73,13 @@ export interface Capture {
/**
* Builds the capture world for an encode of `node`, an entity of `source` —
* the editor's world. A scene, for a render of the composition; any node for
- * an image capture of it alone. Throws when there is no project to render,
- * when the node has no source stamp yet (an element the writer has not put in
- * the file), or when rendering the project produces nothing for that stamp.
+ * an image capture of it alone. Throws when there is no project to render (or
+ * it no longer compiles), when the node has no source stamp yet (an element
+ * the writer has not put in the file), or when rendering the project produces
+ * nothing for that stamp.
*/
export async function createCapture(source: World, node: Entity, options: CaptureOptions = {}): Promise<Capture> {
- const code = await loadProjectBundle(source.get(Project)?.id ?? '');
- if (!code) throw new Error('There is no project to render');
+ const code = await bundleFor(source, options.dir);
// The stamp, not the entity: the two worlds hand out ids of their own, and
// the stamp is the same element in both.