Skip to content

πŸŽ₯ Make videos programmatically with React

update

Jonny Burgera79ebbe3d856
diff --git a/packages/template-vercel/build-dir.mjs b/packages/template-vercel/build-dir.mjs
new file mode 100644
index 0000000000..03dc349261
--- /dev/null
+++ b/packages/template-vercel/build-dir.mjs
@@ -0,0 +1 @@
+export const BUILD_DIR = ".remotion";
diff --git a/packages/template-vercel/build.ts b/packages/template-vercel/build.ts
index af594d90c3..36c2d30f28 100644
--- a/packages/template-vercel/build.ts
+++ b/packages/template-vercel/build.ts
@@ -1,4 +1,5 @@
import { $ } from "bun";
+import { BUILD_DIR } from "./build-dir.mjs";
-await $`bunx remotion bundle --out-dir ./.remotion`;
+await $`bunx remotion bundle --out-dir ./${BUILD_DIR}`;
await $`bunx next build`;
diff --git a/packages/template-vercel/next.config.js b/packages/template-vercel/next.config.js
index 225ebd1b61..5ae92554dd 100644
--- a/packages/template-vercel/next.config.js
+++ b/packages/template-vercel/next.config.js
@@ -1,9 +1,11 @@
+const { BUILD_DIR } = require("./build-dir.mjs");
+
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
// Include the Remotion bundle in the API route
outputFileTracingIncludes: {
- "/api/render": ["./.remotion/**/*"],
+ "/api/render": ["./" + BUILD_DIR + "/**/*", "./render.mjs"],
},
};
diff --git a/packages/template-vercel/render.mjs b/packages/template-vercel/render.mjs
new file mode 100644
index 0000000000..c7947454c2
--- /dev/null
+++ b/packages/template-vercel/render.mjs
@@ -0,0 +1,31 @@
+import { renderMedia, selectComposition } from "@remotion/renderer";
+
+const config = JSON.parse(process.argv[2]);
+
+async function main() {
+ try {
+ const composition = await selectComposition({
+ serveUrl: config.serveUrl,
+ id: config.compositionId,
+ inputProps: config.inputProps,
+ });
+
+ await renderMedia({
+ composition,
+ serveUrl: config.serveUrl,
+ codec: "h264",
+ outputLocation: "/tmp/video.mp4",
+ inputProps: config.inputProps,
+ onProgress: ({ progress }) => {
+ console.log(JSON.stringify({ type: "progress", progress }));
+ },
+ });
+
+ console.log(JSON.stringify({ type: "done" }));
+ } catch (err) {
+ console.error(JSON.stringify({ type: "error", message: err.message }));
+ process.exit(1);
+ }
+}
+
+main();
diff --git a/packages/template-vercel/src/app/api/render/helpers.ts b/packages/template-vercel/src/app/api/render/helpers.ts
index 89e359ac2e..ae816c9b45 100644
--- a/packages/template-vercel/src/app/api/render/helpers.ts
+++ b/packages/template-vercel/src/app/api/render/helpers.ts
@@ -1,6 +1,7 @@
import { Sandbox } from "@vercel/sandbox";
import { readdir, readFile } from "fs/promises";
import path from "path";
+import { BUILD_DIR } from "../../../../build-dir.mjs";
export const createDisposableSandbox = async (
options: Parameters<typeof Sandbox.create>[0],
@@ -26,36 +27,9 @@ export const createDisposableWriter = (
export async function getRemotionBundleFiles(): Promise<
{ path: string; content: Buffer }[]
> {
- // Debug logging to find where .remotion actually is
- console.log("__dirname:", __dirname);
- console.log("process.cwd():", process.cwd());
+ const bundleDir = path.join(process.cwd(), BUILD_DIR);
- // Option 1: 2 levels up from __dirname (current)
- const option1 = path.join(__dirname, "../../.remotion");
- // Option 2: 4 levels up from __dirname
- const option2 = path.join(__dirname, "../../../../.remotion");
- // Option 3: from process.cwd()
- const option3 = path.join(process.cwd(), ".remotion");
-
- for (const candidate of [option1, option2, option3]) {
- try {
- const contents = await readdir(candidate);
- console.log(`βœ“ Found ${candidate}:`, contents.slice(0, 5), "...");
- } catch {
- console.log(`βœ— Not found: ${candidate}`);
- }
- }
-
- // Also list project root to see what's there
- try {
- const cwdContents = await readdir(process.cwd());
- console.log(`Contents of cwd (${process.cwd()}):`, cwdContents);
- } catch {
- console.log(`Could not read cwd`);
- }
-
- // Placeholder - will update once we know the right path
- const remotionDir = option1;
+ const remotionDir = bundleDir;
const files: { path: string; content: Buffer }[] = [];
@@ -88,40 +62,7 @@ export function formatSSE(message: SSEMessage): string {
return `data: ${JSON.stringify(message)}\n\n`;
}
-export function generateRenderScript(options: {
- serveUrl: string;
- compositionId: string;
- inputProps: Record<string, unknown>;
-}): string {
- return `
-const { renderMedia, selectComposition } = require("@remotion/renderer");
-
-async function main() {
- try {
- const composition = await selectComposition({
- serveUrl: ${JSON.stringify(options.serveUrl)},
- id: ${JSON.stringify(options.compositionId)},
- inputProps: ${JSON.stringify(options.inputProps)},
- });
-
- await renderMedia({
- composition,
- serveUrl: ${JSON.stringify(options.serveUrl)},
- codec: "h264",
- outputLocation: "/tmp/video.mp4",
- inputProps: ${JSON.stringify(options.inputProps)},
- onProgress: ({ progress }) => {
- console.log(JSON.stringify({ type: "progress", progress }));
- },
- });
-
- console.log(JSON.stringify({ type: "done" }));
- } catch (err) {
- console.error(JSON.stringify({ type: "error", message: err.message }));
- process.exit(1);
- }
-}
-
-main();
-`;
+export async function getRenderScript(): Promise<Buffer> {
+ const renderScriptPath = path.join(process.cwd(), "render.mjs");
+ 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 d2a16eaf19..284da73bbe 100644
--- a/packages/template-vercel/src/app/api/render/route.ts
+++ b/packages/template-vercel/src/app/api/render/route.ts
@@ -8,10 +8,11 @@ import {
createDisposableSandbox,
createDisposableWriter,
formatSSE,
- generateRenderScript,
getRemotionBundleFiles,
+ getRenderScript,
SSEMessage,
} from "./helpers";
+import { BUILD_DIR } from "../../../../build-dir.mjs";
export async function POST(req: Request) {
const encoder = new TextEncoder();
@@ -50,13 +51,13 @@ export async function POST(req: Request) {
}
for (const dir of Array.from(dirs).sort()) {
- await sandbox.mkDir(`.remotion/${dir}`);
+ await sandbox.mkDir(BUILD_DIR + "/" + dir);
}
// Write all files to the sandbox
await sandbox.writeFiles(
bundleFiles.map((file) => ({
- path: `.remotion/${file.path}`,
+ path: BUILD_DIR + "/" + file.path,
content: file.content,
})),
);
@@ -119,25 +120,26 @@ 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,
- inputProps: body.inputProps,
- });
+ const serveUrl = "/vercel/sandbox/" + BUILD_DIR + "/index.html";
+ const renderScript = await getRenderScript();
await sandbox.writeFiles([
{
- path: "render.cjs",
- content: Buffer.from(renderScript),
+ path: "render.mjs",
+ content: renderScript,
},
]);
+ const renderConfig = JSON.stringify({
+ serveUrl,
+ compositionId: COMP_NAME,
+ inputProps: body.inputProps,
+ });
+
// Run the render script
const renderCmd = await sandbox.runCommand({
cmd: "node",
- args: ["render.cjs"],
+ args: ["render.mjs", renderConfig],
detached: true,
});