π₯ Make videos programmatically with React
move
Jonny Burgerb9e86135eab0
diff --git a/packages/docs/docs/miscellaneous/cloudflare-containers-example.mdx b/packages/docs/docs/cloudflare-containers.mdxsimilarity index 57%rename from packages/docs/docs/miscellaneous/cloudflare-containers-example.mdxrename to packages/docs/docs/cloudflare-containers.mdxindex 787bbc20b9..91fa912c55 100644--- a/packages/docs/docs/miscellaneous/cloudflare-containers-example.mdx+++ b/packages/docs/docs/cloudflare-containers.mdx@@ -1,7 +1,8 @@---+image: /generated/articles-docs-cloudflare-containers.pngtitle: 'Deploying a Renderer to Cloudflare Containers'sidebar_label: 'Cloudflare Containers'-crumb: 'Cloudflare Containers'+crumb: 'Server-side rendering'---import Tabs from '@theme/Tabs';@@ -27,6 +28,7 @@ values={[{ label: 'npm', value: 'npm', },{ label: 'yarn', value: 'yarn', },{ label: 'pnpm', value: 'pnpm', },+{ label: 'bun', value: 'bun', },]}><TabItem value="npm">@@ -44,10 +46,11 @@ pnpm add express @aws-sdk/client-s3```</TabItem>-<TabItem value="yarn">++<TabItem value="bun">```bash-yarn add express @aws-sdk/client-s3+bun add express @aws-sdk/client-s3```</TabItem>@@ -58,33 +61,40 @@ yarn add express @aws-sdk/client-s3Create a Dockerfile in your project's root directory. This file defines the container image, installing all necessary system dependencies for Remotion's browser rendering.-```dockerfile title="Dockerfile"-# Use a Node.js base image. Bullseye has the required libraries.-FROM node:18-bullseye-slim--# Install system dependencies needed for Chromium-RUN apt-get update && apt-get install -y \- ca-certificates fonts-liberation libasound2 libatk-bridge2.0-0 libatk1.0-0 \- libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 \- libglib2.0-0 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 libpangocairo-1.0-0 \- libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 \- libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 lsb-release \- wget xdg-utils --no-install-recommends--# Set up the working directory-WORKDIR /app--# Copy dependency manifests-COPY package.json yarn.lock* ./--# Install project dependencies-RUN yarn install --frozen-lockfile--# Copy the rest of your project source code-COPY . .--# Tell Remotion where to find the system-installed Chromium.-ENV REMOTION_CHROME_BINARY=/usr/bin/google-chrome-stable+```docker title="Dockerfile"+FROM node:22-bookworm-slim++# Install Chrome dependencies+RUN apt-get update+RUN apt install -y \+ libnss3 \+ libdbus-1-3 \+ libatk1.0-0 \+ libgbm-dev \+ libasound2 \+ libxrandr2 \+ libxkbcommon-dev \+ libxfixes3 \+ libxcomposite1 \+ libxdamage1 \+ libatk-bridge2.0-0 \+ libpango-1.0-0 \+ libcairo2 \+ libcups2++# Copy everything from your project to the Docker image. Adjust if needed.+COPY package.json package*.json yarn.lock* pnpm-lock.yaml* bun.lockb* bun.lock* tsconfig.json* remotion.config.* ./+COPY src ./src++# If you have a public folder:+COPY public ./public+++# Install the right package manager and dependencies+RUN npm i++# Install Chrome+RUN npx remotion browser ensure# The command to start the serverCMD ["node", "server.js"]@@ -95,8 +105,8 @@ CMD ["node", "server.js"]Create a server.js file in your root. This Express server will handle render requests, orchestrate the render with Remotion, upload the result to R2, and return the public URL.```javascript title="server.js"-import { renderMedia, getCompositions } from '@remotion/renderer';-import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';+import {renderMedia, getCompositions} from '@remotion/renderer';+import {S3Client, PutObjectCommand} from '@aws-sdk/client-s3';import express from 'express';import fs from 'fs/promises';import path from 'path';@@ -112,63 +122,63 @@ const BUCKET_PUBLIC_URL = process.env.R2_PUBLIC_URL;const ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID;const s3 = new S3Client({- region: 'auto',- endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`,- // Credentials are not needed when running in the Cloudflare environment- // as they are automatically provided by the R2 binding.+ region: 'auto',+ endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`,+ // Credentials are not needed when running in the Cloudflare environment+ // as they are automatically provided by the R2 binding.});app.post('/render', async (req, res) => {- const { compositionId, inputProps } = req.body;- const outputLocation = path.join('/tmp', `out-${Date.now()}.mp4`);-- try {- if (!compositionId) {- return res.status(400).send({ message: '`compositionId` is required.' });- }- if (!BUCKET_NAME || !ACCOUNT_ID || !BUCKET_PUBLIC_URL) {- throw new Error('Server is missing R2 environment variables.');- }-- const compositions = await getCompositions({ entryPoint: './src/index.ts' });- const composition = compositions.find((c) => c.id === compositionId);- if (!composition) {- return res.status(404).send({ message: `Composition '${compositionId}' not found.` });- }-- console.log('Starting render...');- await renderMedia({ composition, inputProps, codec: 'h264', outputLocation });- console.log('Render finished.');-- const fileBuffer = await fs.readFile(outputLocation);- const objectKey = `renders/${path.basename(outputLocation)}`;-- console.log(`Uploading to R2 bucket: ${BUCKET_NAME}`);- await s3.send(- new PutObjectCommand({- Bucket: BUCKET_NAME,- Key: objectKey,- Body: fileBuffer,- ContentType: 'video/mp4',- })- );- console.log('Upload complete.');-- const publicUrl = `${BUCKET_PUBLIC_URL}/${objectKey}`;- res.status(200).send({ outputUrl: publicUrl });- } catch (err) {- console.error(err);- res.status(500).send({ message: 'Error rendering video.', error: err.message });- } finally {- // Clean up the temporary file from the container's filesystem- if (await fs.stat(outputLocation).catch(() => null)) {- await fs.unlink(outputLocation);- }+ const {compositionId, inputProps} = req.body;+ const outputLocation = path.join('/tmp', `out-${Date.now()}.mp4`);++ try {+ if (!compositionId) {+ return res.status(400).send({message: '`compositionId` is required.'});+ }+ if (!BUCKET_NAME || !ACCOUNT_ID || !BUCKET_PUBLIC_URL) {+ throw new Error('Server is missing R2 environment variables.');}++ const compositions = await getCompositions({entryPoint: './src/index.ts'});+ const composition = compositions.find((c) => c.id === compositionId);+ if (!composition) {+ return res.status(404).send({message: `Composition '${compositionId}' not found.`});+ }++ console.log('Starting render...');+ await renderMedia({composition, inputProps, codec: 'h264', outputLocation});+ console.log('Render finished.');++ const fileBuffer = await fs.readFile(outputLocation);+ const objectKey = `renders/${path.basename(outputLocation)}`;++ console.log(`Uploading to R2 bucket: ${BUCKET_NAME}`);+ await s3.send(+ new PutObjectCommand({+ Bucket: BUCKET_NAME,+ Key: objectKey,+ Body: fileBuffer,+ ContentType: 'video/mp4',+ }),+ );+ console.log('Upload complete.');++ const publicUrl = `${BUCKET_PUBLIC_URL}/${objectKey}`;+ res.status(200).send({outputUrl: publicUrl});+ } catch (err) {+ console.error(err);+ res.status(500).send({message: 'Error rendering video.', error: err.message});+ } finally {+ // Clean up the temporary file from the container's filesystem+ if (await fs.stat(outputLocation).catch(() => null)) {+ await fs.unlink(outputLocation);+ }+ }});app.listen(port, () => {- console.log(`Renderer server listening on port ${port}`);+ console.log(`Renderer server listening on port ${port}`);});```@@ -191,10 +201,6 @@ binding = "R2_BUCKET" # This name is conventional but not used directly in the sbucket_name = "your-r2-bucket-name"```-:::note-You must replace the placeholder values for R2_BUCKET_NAME, R2_PUBLIC_URL, and CLOUDFLARE_ACCOUNT_ID with your actual values.-:::-## Step 5: DeployWith all files in place, deploy the service from your terminal.@@ -240,4 +246,4 @@ That's it! You can now use this endpoint to programmatically render videos on de:::warningThe deployed endpoint is public by default. For production use, you should implement an authentication mechanism (e.g., a secret token in the request header) to secure your rendering API and prevent unauthorized use.-:::\ No newline at end of file+:::diff --git a/packages/docs/docs/miscellaneous/cloudflare-containers.mdx b/packages/docs/docs/miscellaneous/cloudflare-containers.mdxnew file mode 100644index 0000000000..0094e0b1f6--- /dev/null+++ b/packages/docs/docs/miscellaneous/cloudflare-containers.mdx@@ -0,0 +1,248 @@+---+title: 'Deploying a Renderer to Cloudflare Containers'+sidebar_label: 'Cloudflare Containers'+crumb: 'Server-side rendering'+---++import Tabs from '@theme/Tabs';+import TabItem from '@theme/TabItem';++This guide provides a complete walkthrough for deploying a Remotion rendering service on [Cloudflare Containers](https://developers.cloudflare.com/containers/). The final rendered videos will be uploaded to a [Cloudflare R2](https://developers.cloudflare.com/r2/) bucket for persistent storage.++## Prerequisites++- A working Remotion project (e.g., from `npm create video`).+- A [Cloudflare account](https://dash.cloudflare.com/sign-up).+- The [Wrangler CLI](https://developers.cloudflare.com/workers/wrangler/install-and-update/) installed (`npm i -g wrangler`) and authenticated (`wrangler login`).+- An R2 bucket created in your Cloudflare dashboard.+- Your R2 bucket's "Public access" URL enabled. In your bucket's settings, click "Allow Access" and copy the public URL (e.g., `https://pub-xxxxxxxx.r2.dev`).++## Step 1: Add Dependencies++Your server will need `express` to handle HTTP requests and the `@aws-sdk/client-s3` package to communicate with the S3-compatible R2 API.++<Tabs+defaultValue="npm"+values={[+{ label: 'npm', value: 'npm', },+{ label: 'yarn', value: 'yarn', },+{ label: 'pnpm', value: 'pnpm', },+{ label: 'bun', value: 'bun', },+]+}>+<TabItem value="npm">++```bash+npm install express @aws-sdk/client-s3+```++</TabItem>++<TabItem value="pnpm">++```bash+pnpm add express @aws-sdk/client-s3+```++</TabItem>++<TabItem value="bun">++```bash+bun add express @aws-sdk/client-s3+```++</TabItem>++</Tabs>++## Step 2: Create a Dockerfile++Create a Dockerfile in your project's root directory. This file defines the container image, installing all necessary system dependencies for Remotion's browser rendering.++```docker title="Dockerfile"+FROM node:22-bookworm-slim++# Install Chrome dependencies+RUN apt-get update+RUN apt install -y \+ libnss3 \+ libdbus-1-3 \+ libatk1.0-0 \+ libgbm-dev \+ libasound2 \+ libxrandr2 \+ libxkbcommon-dev \+ libxfixes3 \+ libxcomposite1 \+ libxdamage1 \+ libatk-bridge2.0-0 \+ libpango-1.0-0 \+ libcairo2 \+ libcups2++# Copy everything from your project to the Docker image. Adjust if needed.+COPY package.json package*.json yarn.lock* pnpm-lock.yaml* bun.lockb* bun.lock* tsconfig.json* remotion.config.* ./+COPY src ./src++# If you have a public folder:+COPY public ./public+++# Install the right package manager and dependencies+RUN npm i++# Install Chrome+RUN npx remotion browser ensure++# The command to start the server+CMD ["node", "server.js"]+```++## Step 3: Create the Render Server++Create a server.js file in your root. This Express server will handle render requests, orchestrate the render with Remotion, upload the result to R2, and return the public URL.++```javascript title="server.js"+import {renderMedia, getCompositions} from '@remotion/renderer';+import {S3Client, PutObjectCommand} from '@aws-sdk/client-s3';+import express from 'express';+import fs from 'fs/promises';+import path from 'path';++const app = express();+app.use(express.json());++const port = process.env.PORT || 8080;++// These environment variables will be set in wrangler.toml+const BUCKET_NAME = process.env.R2_BUCKET_NAME;+const BUCKET_PUBLIC_URL = process.env.R2_PUBLIC_URL;+const ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID;++const s3 = new S3Client({+ region: 'auto',+ endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`,+ // Credentials are not needed when running in the Cloudflare environment+ // as they are automatically provided by the R2 binding.+});++app.post('/render', async (req, res) => {+ const {compositionId, inputProps} = req.body;+ const outputLocation = path.join('/tmp', `out-${Date.now()}.mp4`);++ try {+ if (!compositionId) {+ return res.status(400).send({message: '`compositionId` is required.'});+ }+ if (!BUCKET_NAME || !ACCOUNT_ID || !BUCKET_PUBLIC_URL) {+ throw new Error('Server is missing R2 environment variables.');+ }++ const compositions = await getCompositions({entryPoint: './src/index.ts'});+ const composition = compositions.find((c) => c.id === compositionId);+ if (!composition) {+ return res.status(404).send({message: `Composition '${compositionId}' not found.`});+ }++ console.log('Starting render...');+ await renderMedia({composition, inputProps, codec: 'h264', outputLocation});+ console.log('Render finished.');++ const fileBuffer = await fs.readFile(outputLocation);+ const objectKey = `renders/${path.basename(outputLocation)}`;++ console.log(`Uploading to R2 bucket: ${BUCKET_NAME}`);+ await s3.send(+ new PutObjectCommand({+ Bucket: BUCKET_NAME,+ Key: objectKey,+ Body: fileBuffer,+ ContentType: 'video/mp4',+ }),+ );+ console.log('Upload complete.');++ const publicUrl = `${BUCKET_PUBLIC_URL}/${objectKey}`;+ res.status(200).send({outputUrl: publicUrl});+ } catch (err) {+ console.error(err);+ res.status(500).send({message: 'Error rendering video.', error: err.message});+ } finally {+ // Clean up the temporary file from the container's filesystem+ if (await fs.stat(outputLocation).catch(() => null)) {+ await fs.unlink(outputLocation);+ }+ }+});++app.listen(port, () => {+ console.log(`Renderer server listening on port ${port}`);+});+```++## Step 4: Configure wrangler.toml++The wrangler.toml file configures your Cloudflare service. Create it in your root. We will define environment variables and bind our R2 bucket, which securely grants the container access.++```toml title="wrangler.toml"+name = "remotion-renderer-service"++# Define environment variables for the container+[vars]+R2_BUCKET_NAME = "your-r2-bucket-name" # The name of your R2 bucket+R2_PUBLIC_URL = "https://pub-xxxxxxxx.r2.dev" # The public URL for your bucket+CLOUDFLARE_ACCOUNT_ID = "your-account-id" # Find this in your Cloudflare dashboard++# Add an R2 binding to grant access+[[r2_buckets]]+binding = "R2_BUCKET" # This name is conventional but not used directly in the server.js+bucket_name = "your-r2-bucket-name"+```++## Step 5: Deploy++With all files in place, deploy the service from your terminal.++```bash+wrangler deploy+```++Wrangler will build the Docker image, push it to Cloudflare, and deploy your service. It will then output your service's public URL.++```bash title="Deployment Output (example)"+β Successfully deployed my-remotion-renderer (9.34s)+ π https://remotion-renderer-service.your-subdomain.workers.dev+```++## Step 6: Trigger a Render++Send a POST request to your service's /render endpoint. Replace YOUR_SERVICE_URL with the URL from the deployment output.++```bash+curl -X POST https://YOUR_SERVICE_URL/render \+-H "Content-Type: application/json" \+-d '{+ "compositionId": "HelloWorld",+ "inputProps": {+ "titleText": "Hello from Cloudflare Containers!",+ "titleColor": "#0B5394"+ }+}'+```++### Response++If successful, the API will respond with a JSON object containing the public URL of your video, hosted on R2.++```json title="Example Response"+{+ "outputUrl": "https://pub-xxxxxxxx.r2.dev/renders/out-1678886400000.mp4"+}+```++That's it! You can now use this endpoint to programmatically render videos on demand.++:::warning+The deployed endpoint is public by default. For production use, you should implement an authentication mechanism (e.g., a secret token in the request header) to secure your rendering API and prevent unauthorized use.+:::diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.tsindex d6f6148367..15ac0ab78a 100644--- a/packages/docs/sidebars.ts+++ b/packages/docs/sidebars.ts@@ -903,6 +903,7 @@ const sidebars: SidebarsConfig = {'miscellaneous/cloud-gpu-docker','compare-ssr','distributed-rendering',+ 'cloudflare-containers',],},{@@ -1261,7 +1262,6 @@ const sidebars: SidebarsConfig = {'bun','deno','standalone',- 'miscellaneous/cloudflare-containers-example','miscellaneous/emojis','miscellaneous/cross-origin-isolation','media-fragments',diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex b20ae6828b..7f4edc6f5b 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -665,6 +665,15 @@ export const articles = [noAi: false,slug: 'clipper',},+ {+ id: 'cloudflare-containers',+ title: 'Deploying a Renderer to Cloudflare Containers',+ relativePath: 'docs/cloudflare-containers.mdx',+ compId: 'articles-docs-cloudflare-containers',+ crumb: 'Server-side rendering',+ noAi: false,+ slug: 'cloudflare-containers',+ },{id: 'cloudrun',title: '@remotion/cloudrun',@@ -3122,6 +3131,15 @@ export const articles = [noAi: false,slug: 'miscellaneous/cloud-gpu',},+ {+ id: 'miscellaneous/cloudflare-containers',+ title: 'Deploying a Renderer to Cloudflare Containers',+ relativePath: 'docs/miscellaneous/cloudflare-containers.mdx',+ compId: 'articles-docs-miscellaneous-cloudflare-containers',+ crumb: 'Server-side rendering',+ noAi: false,+ slug: 'miscellaneous/cloudflare-containers',+ },{id: 'cross-origin-isolation',title: 'Cross-Origin Isolation',diff --git a/packages/docs/static/generated/articles-docs-cloudflare-containers.png b/packages/docs/static/generated/articles-docs-cloudflare-containers.pngnew file mode 100644index 0000000000..e6d0907ea4Binary files /dev/null and b/packages/docs/static/generated/articles-docs-cloudflare-containers.png differdiff --git a/packages/docs/static/generated/articles-docs-miscellaneous-cloudflare-containers.png b/packages/docs/static/generated/articles-docs-miscellaneous-cloudflare-containers.pngnew file mode 100644index 0000000000..e6d0907ea4Binary files /dev/null and b/packages/docs/static/generated/articles-docs-miscellaneous-cloudflare-containers.png differ