Skip to content

🎥 Make videos programmatically with React

Revamp dataset render article for Remotion 4.0

Jonny Burgerafd5697c875a
diff --git a/packages/docs/components/Dataset/MyComp.tsx b/packages/docs/components/Dataset/MyComp.tsx
index dd73862ea8..b56ea887f0 100644
--- a/packages/docs/components/Dataset/MyComp.tsx
+++ b/packages/docs/components/Dataset/MyComp.tsx
@@ -63,6 +63,7 @@ export const MyComp: React.FC<Props> = ({ name, repo, logo }) => {
style={{
display: "flex",
flexDirection: "column",
+ color: "black",
}}
>
<div
diff --git a/packages/docs/docs/dataset-render.md b/packages/docs/docs/dataset-render.mdx
similarity index 57%
rename from packages/docs/docs/dataset-render.md
rename to packages/docs/docs/dataset-render.mdx
index eb71c45596..beb5582da2 100644
--- a/packages/docs/docs/dataset-render.md
+++ b/packages/docs/docs/dataset-render.mdx
@@ -7,14 +7,14 @@ crumb: "Tutorials"
---
import { Player } from "@remotion/player";
-import { DatasetDemo} from "../components/Dataset/DatasetDemo";
+import { DatasetDemo } from "../components/Dataset/DatasetDemo";
You can use Remotion to do a batch render to create many videos based on a dataset. In the following example, we are going to turn a JSON dataset into a series of videos.
We'll start by creating a blank Remotion project:
-import Tabs from '@theme/Tabs';
-import TabItem from '@theme/TabItem';
+import Tabs from "@theme/Tabs";
+import TabItem from "@theme/TabItem";
<Tabs
defaultValue="npm"
@@ -39,14 +39,14 @@ pnpm create video --blank
```
</TabItem>
- <TabItem value="yarn">
+ <TabItem value="bun">
```bash
bun create video --blank
```
</TabItem>
- <TabItem value="bun">
+ <TabItem value="yarn">
```bash
yarn create video --blank
@@ -197,38 +197,13 @@ const bundleLocation = await bundle({
});
```
-## Getting the composition
-
-We can use [`getCompositions()`](/docs/renderer/get-compositions) to extract all the defined compositions. Select the composition by searching for the composition ID that is defined in `src/Root.tsx` - by default `MyComp`:
-
-```tsx twoslash
-// @module: esnext
-// @target: es2022
-
-const bundleLocation = "xxx";
-// ---cut---
-
-import { getCompositions } from "@remotion/renderer";
-
-const compositionId = "MyComp";
-const allCompositions = await getCompositions(bundleLocation);
-
-const composition = allCompositions.find((c) => c.id === compositionId);
-
-if (!composition) {
- throw new Error(`No composition with the ID ${compositionId} found.`);
-}
-```
-
-By throwing an error if the composition does not exist, we tell TypeScript that we are sure that `composition` is not `undefined`.
+## Rendering videos
-:::note
-If the duration or dimensions of your video are dependent on your data, pass the data to `getCompositions()` as well. [See here for an example](#change-duration-width-or-height-dynamically).
-:::
+Import the dataset and loop over each entry.
-## Rendering videos
+Fetch the composition metadata for each render using [`selectComposition()`](/docs/renderer/select-composition). You have the opportunity to parametrize the duration, width and height of the composition based on the data entry with the [`calculateMetadata()`](http://localhost:3000/docs/calculate-metadata) function.
-Import the dataset and loop over each entry. Trigger a render using [`renderMedia()`](/docs/renderer/render-media) and pass the data entry as [`inputProps`](/docs/renderer/render-media#inputprops). This will pass the object as React props to the component above.
+Trigger a render using [`renderMedia()`](/docs/renderer/render-media) and pass the data entry as [`inputProps`](/docs/renderer/render-media#inputprops). This will pass the object as React props to the component above.
```ts twoslash
// @module: esnext
@@ -247,25 +222,20 @@ export const data = [
},
];
-const bundleLocation = "xxx";
-
// @filename: render.ts
-const composition = {
- width: 1000,
- height: 1000,
- fps: 30,
- durationInFrames: 30,
- id: "hi",
- defaultProps: {},
- props: {},
- defaultCodec: null,
-};
+const compositionId = "MyComp";
const bundleLocation = "xxx";
// ---cut---
-import { renderMedia } from "@remotion/renderer";
+import { renderMedia, selectComposition } from "@remotion/renderer";
import { data } from "./dataset";
for (const entry of data) {
+ const composition = await selectComposition({
+ serveUrl: bundleLocation,
+ id: compositionId,
+ inputProps: entry,
+ });
+
await renderMedia({
composition,
serveUrl: bundleLocation,
@@ -282,9 +252,7 @@ It is not recommended to render more than one video at once.
## Full script
-Currently, top level `await` is not enable by default in Node, so all asynchronous functions were wrapped in an async function and which is immediately called.
-
-```ts twoslash title="render.ts"
+```ts twoslash title="render.mjs"
// @filename: dataset.ts
export const data = [
{
@@ -305,156 +273,88 @@ export const webpackOverride: WebpackOverrideFn = (f) => f;
// @filename: render.ts
// ---cut---
-import { getCompositions, renderMedia } from "@remotion/renderer";
+// @module: ESNext
+// @target: ESNext
+import { selectComposition, renderMedia } from "@remotion/renderer";
import { webpackOverride } from "./webpack-override";
import { bundle } from "@remotion/bundler";
import { data } from "./dataset";
const compositionId = "MyComp";
-const start = async () => {
- const bundleLocation = await bundle({
- entryPoint: "./src/index.ts",
- // If you have a webpack override, don't forget to add it
- webpackOverride: webpackOverride,
- });
-
- const allCompositions = await getCompositions(bundleLocation);
-
- const composition = allCompositions.find((c) => c.id === compositionId);
-
- if (!composition) {
- throw new Error(`No composition with the ID ${compositionId} found.`);
- }
+const bundleLocation = await bundle({
+ entryPoint: "./src/index.ts",
+ // If you have a webpack override, don't forget to add it
+ webpackOverride: webpackOverride,
+});
- for (const entry of data) {
- await renderMedia({
- composition,
- serveUrl: bundleLocation,
- codec: "h264",
- outputLocation: `out/${entry.name}.mp4`,
- inputProps: entry,
- });
- }
-};
+for (const entry of data) {
+ const composition = await selectComposition({
+ serveUrl: bundleLocation,
+ id: compositionId,
+ inputProps: entry,
+ });
-start()
- .then(() => {
- console.log("Rendered all videos");
- })
- .catch((err) => {
- console.log("Error occurred:", err);
+ await renderMedia({
+ composition,
+ serveUrl: bundleLocation,
+ codec: "h264",
+ outputLocation: `out/${entry.name}.mp4`,
+ inputProps: entry,
});
+}
```
## Running the script
-To help us in running the render, we need to install `ts-node` from npm.
-
<Tabs
-defaultValue="npm"
+defaultValue="Node"
values={[
-{ label: 'npm', value: 'npm', },
-{ label: 'yarn', value: 'yarn', },
-{ label: 'pnpm', value: 'pnpm', },
+{ label: 'Node', value: 'Node', },
+{ label: 'Bun', value: 'Bun', },
]
}>
-<TabItem value="npm">
+<TabItem value="Node">
```bash
-npm i ts-node
+node render.mjs
```
- </TabItem>
- <TabItem value="pnpm">
-
-```bash
-pnpm i ts-node
-```
+To use TypeScript, rename the file to `render.ts`, install `ts-node` from npm and run `ts-node render.ts`. If you get errors, wrap the asynchronous code in an async function and call it.
</TabItem>
-
- <TabItem value="yarn">
+ <TabItem value="Bun">
```bash
-yarn add ts-node
+bun render.mjs
```
+TypeScript should work out of the box if you rename your file to `render.ts`.
+
</TabItem>
</Tabs>
-You can then run the script using
-
-```bash
-npx ts-node render.ts
-```
-
## Rendering videos from a CSV dataset
Use a package like [`csv2json`](https://www.npmjs.com/package/csv2json) to convert your dataset into a JSON.
-## Change duration, width or height dynamically
-
-To make the duration or the dimensions dependent on your data, call [`getCompositions()`](/docs/renderer/get-compositions) with the [`inputProps`](/docs/renderer/get-compositions#inputprops) option. For this you need to move `getCompositions()` into the `for` loop like this:
-
-```ts twoslash title="render.ts"
-// @filename: dataset.ts
-export const data = [
- {
- name: "React",
- repo: "facebook/react",
- logo: "https://upload.wikimedia.org/wikipedia/commons/a/a7/React-icon.svg",
- },
- {
- name: "Remotion",
- repo: "remotion-dev/remotion",
- logo: "https://github.com/remotion-dev/logo/raw/main/withouttitle/element-0.png",
- },
-];
-
-// @filename: webpack-override.ts
-import type { WebpackOverrideFn } from "@remotion/bundler";
-export const webpackOverride: WebpackOverrideFn = (f) => f;
-
-// @filename: render.ts
-// @target: es2022
-const bundleLocation = "/tmp/bundle.js";
-import { getCompositions, renderMedia } from "@remotion/renderer";
-import { webpackOverride } from "./webpack-override";
-import { bundle } from "@remotion/bundler";
-import { data } from "./dataset";
-
-const compositionId = "MyComp";
-
-// ---cut---
-for (const entry of data) {
- const allCompositions = await getCompositions(bundleLocation, {
- // Add input props to getCompositions() as well.
- inputProps: entry,
- });
-
- const composition = allCompositions.find((c) => c.id === compositionId);
-
- if (!composition) {
- throw new Error(`No composition with the ID ${compositionId} found.`);
- }
-
- await renderMedia({
- composition,
- serveUrl: bundleLocation,
- codec: "h264",
- outputLocation: `out/${entry.name}.mp4`,
- inputProps: entry,
- });
-}
-```
-
-Learn how to [change the metadata based on the input props here](/docs/dynamic-metadata).
-
-## Credits
-
-Authored by [Alex Fernandez](https://github.com/alexfernandez803) and [ThePerfectSystem](https://github.com/ThePerfectSystem), edited by Jonny Burger.
-
## See also
- [Example repository using a dataset](https://github.com/alexfernandez803/remotion-dataset)
+
+<Credits
+ contributors={[
+ {
+ username: "alexfernandez803",
+ contribution: "Author",
+ },
+ {
+ username: "ThePerfectSystem",
+ contribution: "Author",
+ },
+ {
+ username: "JonnyBurger",
+ contribution: "Editor",
+ },
+ ]}
+/>