Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply best practices for creating programmatic videos with Remotion and React.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/calculate-metadata.md
1---2name: calculate-metadata3description: Dynamically set composition duration, dimensions, and props4metadata:5tags: calculateMetadata, duration, dimensions, props, dynamic6---78# Using calculateMetadata910Use `calculateMetadata` on a `<Composition>` to dynamically set duration, dimensions, and transform props before rendering.1112```tsx13<Composition14id="MyComp"15component={MyComponent}16durationInFrames={300}17fps={30}18width={1920}19height={1080}20defaultProps={{ videoSrc: "https://remotion.media/video.mp4" }}21calculateMetadata={calculateMetadata}22/>23```2425## Setting duration based on a video2627Use the [`getVideoDuration`](./get-video-duration.md) and [`getVideoDimensions`](./get-video-dimensions.md) skills to get the video duration and dimensions:2829```tsx30import { CalculateMetadataFunction } from "remotion";31import { getVideoDuration } from "./get-video-duration";3233const calculateMetadata: CalculateMetadataFunction<Props> = async ({34props,35}) => {36const durationInSeconds = await getVideoDuration(props.videoSrc);3738return {39durationInFrames: Math.ceil(durationInSeconds * 30),40};41};42```4344## Matching dimensions of a video4546Use the [`getVideoDimensions`](./get-video-dimensions.md) skill to get the video dimensions:4748```tsx49import { CalculateMetadataFunction } from "remotion";50import { getVideoDuration } from "./get-video-duration";51import { getVideoDimensions } from "./get-video-dimensions";5253const calculateMetadata: CalculateMetadataFunction<Props> = async ({54props,55}) => {56const dimensions = await getVideoDimensions(props.videoSrc);5758return {59width: dimensions.width,60height: dimensions.height,61};62};63```6465## Setting duration based on multiple videos6667```tsx68const calculateMetadata: CalculateMetadataFunction<Props> = async ({69props,70}) => {71const metadataPromises = props.videos.map((video) =>72getVideoDuration(video.src),73);74const allMetadata = await Promise.all(metadataPromises);7576const totalDuration = allMetadata.reduce(77(sum, durationInSeconds) => sum + durationInSeconds,780,79);8081return {82durationInFrames: Math.ceil(totalDuration * 30),83};84};85```8687## Setting a default outName8889Set the default output filename based on props:9091```tsx92const calculateMetadata: CalculateMetadataFunction<Props> = async ({93props,94}) => {95return {96defaultOutName: `video-${props.id}.mp4`,97};98};99```100101## Transforming props102103Fetch data or transform props before rendering:104105```tsx106const calculateMetadata: CalculateMetadataFunction<Props> = async ({107props,108abortSignal,109}) => {110const response = await fetch(props.dataUrl, { signal: abortSignal });111const data = await response.json();112113return {114props: {115...props,116fetchedData: data,117},118};119};120```121122The `abortSignal` cancels stale requests when props change in the Studio.123124## Return value125126All fields are optional. Returned values override the `<Composition>` props:127128- `durationInFrames`: Number of frames129- `width`: Composition width in pixels130- `height`: Composition height in pixels131- `fps`: Frames per second132- `props`: Transformed props passed to the component133- `defaultOutName`: Default output filename134- `defaultCodec`: Default codec for rendering135