Skip to content

🎥 Make videos programmatically with React

lots of refinement

Jonny Burgerd00e635f7673
diff --git a/packages/template-vercel/.env.example b/packages/template-vercel/.env.example
index 73fedbeb84..2006f194a2 100644
--- a/packages/template-vercel/.env.example
+++ b/packages/template-vercel/.env.example
@@ -1,8 +1,3 @@
-# Vercel Sandbox OIDC Token
-# In development, if not set, the SDK will initiate a device authorization flow.
-# In production on Vercel, this is automatically available.
-# VERCEL_OIDC_TOKEN=
-
-# Vercel Blob Storage (for storing rendered videos)
+# Vercel Blob Storage (for storing rendered videos and caching sandboxes)
# Create a Blob store at: Vercel Dashboard -> Storage -> Create -> Blob
# BLOB_READ_WRITE_TOKEN=
diff --git a/packages/template-vercel/src/app/api/render/helpers.ts b/packages/template-vercel/src/app/api/render/helpers.ts
index bcf8d79549..d1eb72152f 100644
--- a/packages/template-vercel/src/app/api/render/helpers.ts
+++ b/packages/template-vercel/src/app/api/render/helpers.ts
@@ -1,4 +1,6 @@
import { Sandbox } from "@vercel/sandbox";
+import { execSync } from "child_process";
+import { existsSync } from "fs";
import { readdir, readFile } from "fs/promises";
import path from "path";
import { BUILD_DIR } from "../../../../build-dir.mjs";
@@ -52,8 +54,7 @@ export async function getRemotionBundleFiles(): Promise<
}
export type SSEMessage =
- | { type: "progress"; progress: number }
- | { type: "phase"; phase: string }
+ | { type: "phase"; phase: string; progress: number; subtitle?: string }
| { type: "done"; url: string; size: number }
| { type: "error"; message: string };
@@ -61,6 +62,25 @@ export function formatSSE(message: SSEMessage): string {
return `data: ${JSON.stringify(message)}\n\n`;
}
+export async function ensureLocalBundle(): Promise<void> {
+ if (process.env.VERCEL) {
+ return;
+ }
+
+ const bundleDir = path.join(process.cwd(), BUILD_DIR);
+ if (!existsSync(bundleDir)) {
+ try {
+ execSync(`node_modules/.bin/remotion bundle --out-dir ./${BUILD_DIR}`, {
+ cwd: process.cwd(),
+ stdio: "pipe",
+ });
+ } catch (e) {
+ const stderr = (e as { stderr?: Buffer }).stderr?.toString() ?? "";
+ throw new Error(`Remotion bundle failed: ${stderr}`);
+ }
+ }
+}
+
export async function getRenderScript(): Promise<Buffer> {
const renderScriptPath = path.join(process.cwd(), "render.ts");
return readFile(renderScriptPath);
diff --git a/packages/template-vercel/src/app/api/render/route.ts b/packages/template-vercel/src/app/api/render/route.ts
index 9b7a76dad2..5ee67b66f3 100644
--- a/packages/template-vercel/src/app/api/render/route.ts
+++ b/packages/template-vercel/src/app/api/render/route.ts
@@ -7,6 +7,7 @@ import { VERSION } from "remotion/version";
import {
createDisposableSandbox,
createDisposableWriter,
+ ensureLocalBundle,
formatSSE,
getEnsureBrowserScript,
getRemotionBundleFiles,
@@ -33,14 +34,20 @@ export async function POST(req: Request) {
const payload = await req.json();
const body = RenderRequest.parse(payload);
- await send({ type: "phase", phase: "Creating sandbox..." });
+ await send({ type: "phase", phase: "Bundling video...", progress: 0 });
+ await ensureLocalBundle();
+
+ await send({ type: "phase", phase: "Creating sandbox...", progress: 0 });
await using sandbox = await createDisposableSandbox({
runtime: "node24",
timeout: 5 * 60 * 1000,
});
- await send({ type: "phase", phase: "Preparing machine..." });
+ const preparingPhase = "Preparing machine...";
+ const preparingSubtitle = process.env.VERCEL
+ ? "This only needs to be done once."
+ : "This is only needed during development.";
// Preparation has 3 stages with weights:
// - System dependencies: 60%
@@ -50,6 +57,8 @@ export async function POST(req: Request) {
const WEIGHT_BUNDLE = 0.2;
const WEIGHT_BROWSER = 0.2;
+ await send({ type: "phase", phase: preparingPhase, subtitle: preparingSubtitle, progress: 0 });
+
// Stage 1: Install system dependencies (60%)
const sysInstallCmd = await sandbox.runCommand({
cmd: "sudo",
@@ -84,7 +93,9 @@ export async function POST(req: Request) {
1,
);
await send({
- type: "progress",
+ type: "phase",
+ phase: preparingPhase,
+ subtitle: preparingSubtitle,
progress: stageProgress * WEIGHT_SYS_DEPS,
});
}
@@ -121,7 +132,9 @@ export async function POST(req: Request) {
);
await send({
- type: "progress",
+ type: "phase",
+ phase: preparingPhase,
+ subtitle: preparingSubtitle,
progress: WEIGHT_SYS_DEPS + WEIGHT_BUNDLE,
});
@@ -165,7 +178,8 @@ export async function POST(req: Request) {
if (message.type === "browser-progress") {
const browserProgress = message.percent ?? 0;
await send({
- type: "progress",
+ type: "phase",
+ phase: preparingPhase,
progress:
WEIGHT_SYS_DEPS +
WEIGHT_BUNDLE +
@@ -187,7 +201,8 @@ export async function POST(req: Request) {
);
}
- await send({ type: "phase", phase: "Rendering video..." });
+ const renderingPhase = "Rendering video...";
+ await send({ type: "phase", phase: renderingPhase, progress: 0 });
// Use the local bundle copied to the sandbox
const serveUrl = `/vercel/sandbox/${BUILD_DIR}`;
@@ -218,7 +233,11 @@ export async function POST(req: Request) {
try {
const message = JSON.parse(log.data);
if (message.type === "progress") {
- await send({ type: "progress", progress: message.progress });
+ await send({
+ type: "phase",
+ phase: renderingPhase,
+ progress: message.progress,
+ });
}
} catch {
// Not JSON, ignore
@@ -233,7 +252,7 @@ export async function POST(req: Request) {
throw new Error(`Render failed: ${stderr} ${stdout}`);
}
- await send({ type: "phase", phase: "Uploading video..." });
+ await send({ type: "phase", phase: "Uploading video...", progress: 1 });
const videoBuffer = await sandbox.readFileToBuffer({
path: "/tmp/video.mp4",
@@ -250,7 +269,7 @@ export async function POST(req: Request) {
await send({
type: "done",
- url: blob.url,
+ url: blob.downloadUrl,
size: videoBuffer.length,
});
} catch (err) {
diff --git a/packages/template-vercel/src/components/ProgressBar.tsx b/packages/template-vercel/src/components/ProgressBar.tsx
index d3f929e9ae..b5003b317e 100644
--- a/packages/template-vercel/src/components/ProgressBar.tsx
+++ b/packages/template-vercel/src/components/ProgressBar.tsx
@@ -11,9 +11,9 @@ export const ProgressBar: React.FC<{
return (
<div>
- <div className="w-full h-2.5 rounded-md appearance-none bg-unfocused-border-color mt-2.5 mb-6">
+ <div className="w-full h-2.5 rounded-md overflow-hidden appearance-none bg-unfocused-border-color mt-2.5 mb-6">
<div
- className="bg-foreground h-2.5 rounded-md transition-all ease-in-out duration-100"
+ className="bg-foreground h-2.5"
style={fill}
></div>
</div>
diff --git a/packages/template-vercel/src/components/RenderControls.tsx b/packages/template-vercel/src/components/RenderControls.tsx
index 7b6da9c78a..858800a56d 100644
--- a/packages/template-vercel/src/components/RenderControls.tsx
+++ b/packages/template-vercel/src/components/RenderControls.tsx
@@ -41,11 +41,16 @@ export const RenderControls: React.FC<{
{state.status === "invoking" ? (
<>
<Spacing></Spacing>
- <div style={{ fontSize: 14, color: "#666", marginBottom: 8 }}>
- {state.phase}
- {state.progress > 0
- ? ` (${Math.round(state.progress * 100)}%)`
- : null}
+ <div style={{ fontSize: 14, lineHeight: 1.5, minHeight: "2.5em", marginBottom: 8 }}>
+ <div style={{ color: "#666" }}>
+ {state.phase}
+ {state.progress > 0 && state.progress < 1
+ ? ` ${Math.round(state.progress * 100)}%`
+ : null}
+ </div>
+ <div style={{ color: "#999", fontSize: 12, visibility: state.subtitle ? "visible" : "hidden" }}>
+ {state.subtitle ?? "\u00A0"}
+ </div>
</div>
<ProgressBar progress={state.progress} />
</>
diff --git a/packages/template-vercel/src/helpers/use-rendering.ts b/packages/template-vercel/src/helpers/use-rendering.ts
index 8e020b686d..92be868281 100644
--- a/packages/template-vercel/src/helpers/use-rendering.ts
+++ b/packages/template-vercel/src/helpers/use-rendering.ts
@@ -11,6 +11,7 @@ export type State =
status: "invoking";
phase: string;
progress: number;
+ subtitle: string | null;
}
| {
status: "error";
@@ -35,6 +36,7 @@ export const useRendering = (
status: "invoking",
phase: "Starting...",
progress: 0,
+ subtitle: null,
});
try {
@@ -66,20 +68,14 @@ export const useRendering = (
const json = line.slice(6);
const message = JSON.parse(json) as SSEMessage;
- if (message.type === "progress") {
- setState((prev) => {
- if (prev.status !== "invoking") return prev;
- return {
- ...prev,
- progress: message.progress,
- };
- });
- } else if (message.type === "phase") {
+ if (message.type === "phase") {
setState((prev) => {
if (prev.status !== "invoking") return prev;
return {
...prev,
phase: message.phase,
+ progress: message.progress,
+ subtitle: message.subtitle ?? null,
};
});
} else if (message.type === "done") {
diff --git a/packages/template-vercel/types/constants.ts b/packages/template-vercel/types/constants.ts
index 4767c5e80f..a0f8d14c3d 100644
--- a/packages/template-vercel/types/constants.ts
+++ b/packages/template-vercel/types/constants.ts
@@ -6,7 +6,7 @@ export const CompositionProps = z.object({
});
export const defaultMyCompProps: z.infer<typeof CompositionProps> = {
- title: "Next.js and Remotion",
+ title: "Vercel and Remotion",
};
export const DURATION_IN_FRAMES = 200;
diff --git a/packages/template-vercel/types/schema.ts b/packages/template-vercel/types/schema.ts
index c04cf015a7..66d77866c9 100644
--- a/packages/template-vercel/types/schema.ts
+++ b/packages/template-vercel/types/schema.ts
@@ -18,7 +18,6 @@ export type RenderResponse =
};
export type SSEMessage =
- | { type: "progress"; progress: number }
- | { type: "phase"; phase: string }
+ | { type: "phase"; phase: string; progress: number; subtitle?: string }
| { type: "done"; url: string; size: number }
| { type: "error"; message: string };