Skip to content

🎥 Make videos programmatically with React

try to pass bundle locally

Jonny Burgere15d40a9f1f6
diff --git a/bun.lock b/bun.lock
index 634d0709b6..0c08775028 100644
--- a/bun.lock
+++ b/bun.lock
@@ -2129,6 +2129,7 @@
"@remotion/player": "workspace:*",
"@remotion/shapes": "workspace:*",
"@remotion/tailwind-v4": "workspace:*",
+ "@types/bun": "1.3.3",
"@vercel/blob": "2.0.1",
"@vercel/sandbox": "1.3.1",
"clsx": "2.1.1",
diff --git a/packages/template-vercel/.gitignore b/packages/template-vercel/.gitignore
index acc7d66936..03c0fed8b5 100644
--- a/packages/template-vercel/.gitignore
+++ b/packages/template-vercel/.gitignore
@@ -38,7 +38,4 @@ out/
.env
build
-# remotion bundle (generated at build time)
-/public/remotion-bundle
-
-bundle
\ No newline at end of file
+public/.remotion
\ No newline at end of file
diff --git a/packages/template-vercel/build.ts b/packages/template-vercel/build.ts
new file mode 100644
index 0000000000..b64684b884
--- /dev/null
+++ b/packages/template-vercel/build.ts
@@ -0,0 +1,4 @@
+import { $ } from "bun";
+
+await $`bunx remotion bundle --out-dir ./remotion`;
+await $`bunx next build`;
diff --git a/packages/template-vercel/next.config.js b/packages/template-vercel/next.config.js
index c93e03460a..ff16cc2349 100644
--- a/packages/template-vercel/next.config.js
+++ b/packages/template-vercel/next.config.js
@@ -1,9 +1,13 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
- reactStrictMode: true,
- typescript: {
- ignoreBuildErrors: true,
- },
+ reactStrictMode: true,
+ typescript: {
+ ignoreBuildErrors: true,
+ },
+ // Include the Remotion bundle in the API route
+ outputFileTracingIncludes: {
+ "/api/render": "./remotion",
+ },
};
module.exports = nextConfig;
diff --git a/packages/template-vercel/package.json b/packages/template-vercel/package.json
index 0c9cce8624..75b9d4ce83 100644
--- a/packages/template-vercel/package.json
+++ b/packages/template-vercel/package.json
@@ -17,6 +17,7 @@
"@remotion/player": "workspace:*",
"@remotion/shapes": "workspace:*",
"@remotion/tailwind-v4": "workspace:*",
+ "@types/bun": "1.3.3",
"@vercel/blob": "2.0.1",
"@vercel/sandbox": "1.3.1",
"clsx": "2.1.1",
diff --git a/packages/template-vercel/src/app/api/render/route.ts b/packages/template-vercel/src/app/api/render/route.ts
index 3a257bc67f..ceb1eb46ad 100644
--- a/packages/template-vercel/src/app/api/render/route.ts
+++ b/packages/template-vercel/src/app/api/render/route.ts
@@ -8,6 +8,33 @@ import {
VIDEO_HEIGHT,
VIDEO_WIDTH,
} from "../../../../types/constants";
+import { VERSION } from "remotion/version";
+import { readdir, readFile } from "fs/promises";
+import path from "path";
+
+async function getRemotionBundleFiles(): Promise<
+ { path: string; content: Buffer }[]
+> {
+ const remotionDir = path.join(process.cwd(), "remotion");
+ const files: { path: string; content: Buffer }[] = [];
+
+ async function readDirRecursive(dir: string, basePath: string = "") {
+ const entries = await readdir(dir, { withFileTypes: true });
+ for (const entry of entries) {
+ const fullPath = path.join(dir, entry.name);
+ const relativePath = path.join(basePath, entry.name);
+ if (entry.isDirectory()) {
+ await readDirRecursive(fullPath, relativePath);
+ } else {
+ const content = await readFile(fullPath);
+ files.push({ path: relativePath, content });
+ }
+ }
+ }
+
+ await readDirRecursive(remotionDir);
+ return files;
+}
type SSEMessage =
| { type: "log"; stream: "stdout" | "stderr"; data: string }
@@ -69,16 +96,6 @@ main();
}
export async function POST(req: Request) {
- const serveUrl = process.env.REMOTION_SERVE_URL;
- if (!serveUrl) {
- return new Response(
- JSON.stringify({
- error: "REMOTION_SERVE_URL environment variable is not set",
- }),
- { status: 500, headers: { "Content-Type": "application/json" } },
- );
- }
-
let sandbox: Sandbox | null = null;
const encoder = new TextEncoder();
@@ -101,6 +118,31 @@ export async function POST(req: Request) {
timeout: 5 * 60 * 1000,
});
+ await send({ type: "phase", phase: "Copying Remotion bundle..." });
+
+ const bundleFiles = await getRemotionBundleFiles();
+
+ // Create the directories first
+ const dirs = new Set<string>();
+ for (const file of bundleFiles) {
+ const dir = path.dirname(file.path);
+ if (dir && dir !== ".") {
+ dirs.add(dir);
+ }
+ }
+
+ for (const dir of Array.from(dirs).sort()) {
+ await sandbox.mkDir(`remotion/${dir}`);
+ }
+
+ // Write all files to the sandbox
+ await sandbox.writeFiles(
+ bundleFiles.map((file) => ({
+ path: `remotion/${file.path}`,
+ content: file.content,
+ })),
+ );
+
await send({
type: "phase",
phase: "Installing system dependencies...",
@@ -143,7 +185,7 @@ export async function POST(req: Request) {
const installCmd = await sandbox.runCommand({
cmd: "pnpm",
- args: ["install", "@remotion/renderer@latest"],
+ args: ["install", "@remotion/renderer@" + VERSION],
detached: true,
});
@@ -158,6 +200,9 @@ export async function POST(req: Request) {
await send({ type: "phase", phase: "Rendering video..." });
+ // Use the local bundle copied to the sandbox
+ const serveUrl = "/vercel/sandbox/remotion/index.html";
+
const renderScript = generateRenderScript({
serveUrl,
compositionId: COMP_NAME,