Skip to content

🎥 Make videos programmatically with React

Update dynamic-metadata.mdx

Jonny Burger94867e1d41e1
diff --git a/packages/docs/docs/dynamic-metadata.mdx b/packages/docs/docs/dynamic-metadata.mdx
index f50c6e5aa7..a403dab0d3 100644
--- a/packages/docs/docs/dynamic-metadata.mdx
+++ b/packages/docs/docs/dynamic-metadata.mdx
@@ -3,7 +3,7 @@ image: /generated/articles-docs-dynamic-metadata.png
id: dynamic-metadata
title: Variable duration and dimensions
sidebar_label: Variable duration
-crumb: "How To"
+crumb: How To
---
You may change the duration of the video based on some asynchronously determined data.
@@ -16,14 +16,14 @@ Consider a scenario where a video is dynamically specified as a background and t
Pass a [`calculateMetadata`](/docs/composition#calculatemetadata) callback function to a [`<Composition>`](/docs/composition). This function should take the [combined props](/docs/props-resolution) and calculate the metadata.
```tsx twoslash title="src/Root.tsx"
-import { getVideoMetadata } from "@remotion/media-utils";
-import { Composition, Video } from "remotion";
+import {getVideoMetadata} from '@remotion/media-utils';
+import {Composition, Video} from 'remotion';
type MyCompProps = {
src: string;
};
-const MyComp: React.FC<MyCompProps> = ({ src }) => {
+const MyComp: React.FC<MyCompProps> = ({src}) => {
return <Video src={src} />;
};
@@ -37,9 +37,9 @@ export const Root: React.FC = () => {
width={1920}
height={1080}
defaultProps={{
- src: "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
+ src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
}}
- calculateMetadata={async ({ props }) => {
+ calculateMetadata={async ({props}) => {
const data = await getVideoMetadata(props.src);
return {
@@ -65,11 +65,11 @@ In the following example, Remotion is instructed to wait for the [`getVideoMetad
By calling [`delayRender()`](/docs/delay-render), Remotion will be blocked from proceeding until [`continueRender()`](/docs/continue-render) is called.
```tsx twoslash title="src/Root.tsx"
-import { useEffect, useState } from "react";
-import { Composition, continueRender, delayRender } from "remotion";
+import {useEffect, useState} from 'react';
+import {Composition, continueRender, delayRender} from 'remotion';
const VideoTesting: React.FC = () => null;
// ---cut---
-import { getVideoMetadata } from "@remotion/media-utils";
+import {getVideoMetadata} from '@remotion/media-utils';
export const Index: React.FC = () => {
const [handle] = useState(() => delayRender());
@@ -77,9 +77,9 @@ export const Index: React.FC = () => {
useEffect(() => {
getVideoMetadata(
- "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
+ 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
)
- .then(({ durationInSeconds }) => {
+ .then(({durationInSeconds}) => {
setDuration(Math.round(durationInSeconds * 30));
continueRender(handle);
})
@@ -104,7 +104,7 @@ export const Index: React.FC = () => {
To dynamically pass a video asset, you [may pass input props](/docs/props-resolution) when rendering and retrieve them inside your React code using [`getInputProps()`](/docs/get-input-props).
```tsx twoslash title="src/Root.tsx"
-import { getInputProps } from "remotion";
+import {getInputProps} from 'remotion';
const inputProps = getInputProps();
const src = inputProps.src;
@@ -126,30 +126,31 @@ The [`--scale`](/docs/scaling) parameter has the highest priority and will be ap
If you designed your video with certain dimensions and then want to render a different resolution (e.g. 4K instead of Full HD), you can use [output scaling](/docs/scaling).
-If you designed your video with certain FPS and then want to change the frame rate, you can use the [`<FpsConverter>`](/docs/miscellaneous/snippets/fps-converter) snippet.
+If you designed your video with certain FPS and then want to change the frame rate, you should refactor the composition to ensure [the timing stays the same with changing frame rates](/docs/multiple-fps).
## With the `<Player>`
The [`<Player>`](/docs/player) will react if the metadata being passed to it changes. There are two viable ways to do dynamically set the metadata of the Player:
-<p><Step>1</Step> Perform data fetching using <code>useEffect()</code>:</p>
+<p>
+ <Step>1</Step> Perform data fetching using <code>useEffect()</code>:
+</p>
```tsx twoslash title="MyApp.tsx"
-
const VideoTesting: React.FC = () => null;
// ---cut---
-import { getVideoMetadata } from "@remotion/media-utils";
-import { useEffect, useState } from "react";
-import { Player } from "@remotion/player";
+import {getVideoMetadata} from '@remotion/media-utils';
+import {useEffect, useState} from 'react';
+import {Player} from '@remotion/player';
export const Index: React.FC = () => {
const [duration, setDuration] = useState(1);
useEffect(() => {
getVideoMetadata(
- "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
+ 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
)
- .then(({ durationInSeconds }) => {
+ .then(({durationInSeconds}) => {
setDuration(Math.round(durationInSeconds * 30));
})
.catch((err) => {
@@ -169,16 +170,19 @@ export const Index: React.FC = () => {
};
```
-<p><Step>2</Step> Call your own <code>calculateMetadata()</code> function reused from your Remotion project:</p>
+<p>
+ <Step>2</Step> Call your own <code>calculateMetadata()</code> function reused
+ from your Remotion project:
+</p>
```tsx twoslash title="MyApp.tsx"
-import { useEffect, useState } from "react";
-import { CalculateMetadataFunction } from "remotion";
+import {useEffect, useState} from 'react';
+import {CalculateMetadataFunction} from 'remotion';
const VideoTesting: React.FC = () => null;
// ---cut---
-import { Player } from "@remotion/player";
+import {Player} from '@remotion/player';
type Props = {};
@@ -209,10 +213,10 @@ export const Index: React.FC = () => {
defaultProps: {},
props: {},
abortSignal: new AbortController().signal,
- compositionId: "MyComp",
+ compositionId: 'MyComp',
}),
)
- .then(({ durationInFrames, props, width, height, fps }) => {
+ .then(({durationInFrames, props, width, height, fps}) => {
setMetadata({
durationInFrames: durationInFrames as number,
compositionWidth: width as number,