Skip to content

🎥 Make videos programmatically with React

document calculateMetadata

Jonny Burgerc2ce6978a2bb
diff --git a/packages/docs/docs/calculate-metadata.md b/packages/docs/docs/calculate-metadata.md
new file mode 100644
index 0000000000..27a3843d5a
--- /dev/null
+++ b/packages/docs/docs/calculate-metadata.md
@@ -0,0 +1,78 @@
+---
+image: /generated/articles-docs-calculate-metadata.png
+id: calculate-metadata
+title: calculateMetadata()
+crumb: "API"
+---
+
+# calculateMetadata()<AvailableFrom v="4.0.0" />
+
+`calculateMetadata` is a prop that gets passed to [`<Composition>`](/docs/composition) and takes a callback function which may transform metadata.
+
+Use it if you:
+
+<Step>1</Step> need to make the duration, width, height, or fps <a href="/docs/dynamic-metadata">dynamic based on some data</a><br/>
+<Step>2</Step> want to transform the props passed to the composition, for example to do <a href="/docs/data-fetching">data fetching</a>
+
+## Usage / API
+
+Define a function, you may type it using `CalculateMetadataFunction` - the generic argument takes the props of the component of your composition:
+
+```tsx twoslash title="src/Root.tsx"
+// @filename: MyComp.tsx
+import React from "react";
+export type MyComponentProps = {
+ text: string;
+ duration: number;
+};
+
+export const MyComponent: React.FC<MyComponentProps> = ({ text }) => {
+ return <div>{text}</div>;
+};
+
+// @filename: Vid.tsx
+// ---cut---
+import { CalculateMetadataFunction } from "remotion";
+import { MyComponent, MyComponentProps } from "./MyComp";
+
+const calculateMetadata: CalculateMetadataFunction<MyComponentProps> = ({
+ props,
+ defaultProps,
+ abortSignal,
+}) => {
+ return {
+ // Change the metadata
+ durationInFrames: props.duration,
+ // or transform some props
+ props,
+ };
+};
+```
+
+As argument, you get an object with the following properties:
+
+- `defaultProps`: Only the default props, taken from the [`defaultProps`](/docs/composition#defaultprops) prop or the Remotion Studio Data sidebar.
+- `props`: The [resolved props](/docs/props-resolution), taking input props into account.
+- `abortSignal`: An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) which you can use to abort network requests if [the default props have been changed](/docs/data-fetching#aborting-stale-requests) in the meanwhile.
+
+The function must return an object, which can contain the following properties:
+
+- `props` _optional_: The [final props](/docs/props-resolution) the component receives. It must have the [same shape](/docs/data-fetching#usage) as the `props` received by this function.
+- `durationInFrames` _optional_: The duration of the composition in frames
+- `width` _optional_: The width of the composition in pixels
+- `height` _optional_: The height of the composition in pixels
+- `fps` _optional_: The frames per second of the composition
+
+If you return a field, it will take precendence over the props directly passed to the composition.
+
+The function may be `async`.
+
+The function must resolve within the [timeout](/docs/delay-render#timeout).
+
+The function will get executed every time the props in the [Remotion Studio](/docs/visual-editing) changes.
+
+## See also
+
+- [`<Composition>`](/docs/composition)
+- [Data fetching](/docs/data-fetching)
+- [Dynamic metadata](/docs/dynamic-metadata)
diff --git a/packages/docs/docs/composition.md b/packages/docs/docs/composition.md
index 80427de054..c7a484d3c2 100644
--- a/packages/docs/docs/composition.md
+++ b/packages/docs/docs/composition.md
@@ -83,7 +83,7 @@ Passing huge objects to `defaultProps` can be slow. [Learn how to avoid it.](/do
### `calculateMetadata`
-TODO
+See: [`calculateMetadata()`](/docs/calculate-metadata)
### `schema`
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 05d53ded14..857b28fd3c 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -59,6 +59,7 @@ module.exports = {
"use-current-frame",
"use-video-config",
"composition",
+ "calculate-metadata",
"interpolate",
"continue-render",
"delay-render",
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 4b619d0231..b8407ae1a6 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -111,6 +111,13 @@ export const articles = [
compId: "articles-docs-bundler",
crumb: null,
},
+ {
+ id: "calculate-metadata",
+ title: "calculateMetadata()",
+ relativePath: "docs/calculate-metadata.md",
+ compId: "articles-docs-calculate-metadata",
+ crumb: "API",
+ },
{
id: "cancel-render",
title: "cancelRender()",
diff --git a/packages/docs/static/generated/articles-docs-calculate-metadata.png b/packages/docs/static/generated/articles-docs-calculate-metadata.png
new file mode 100644
index 0000000000..a04eb8ea71
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-calculate-metadata.png differ
diff --git a/packages/docs/static/generated/template-google-tts.png b/packages/docs/static/generated/template-google-tts.png
new file mode 100644
index 0000000000..761a91eb76
Binary files /dev/null and b/packages/docs/static/generated/template-google-tts.png differ