--- name: calculate-metadata description: Dynamically set composition duration, dimensions, and props metadata: tags: calculateMetadata, duration, dimensions, props, dynamic --- # Using calculateMetadata Use `calculateMetadata` on a `` to dynamically set duration, dimensions, and transform props before rendering. ```tsx ``` ## Setting duration based on a video Create this local helper as `get-media-metadata.ts` (requires installed Mediabunny) before using the examples: ```ts import {Input, ALL_FORMATS, UrlSource} from "mediabunny"; export const getMediaMetadata = async (src: string) => { using input = new Input({formats: ALL_FORMATS, source: new UrlSource(src)}); const durationInSeconds = await input.computeDuration(); const track = await input.getPrimaryVideoTrack(); return {durationInSeconds, dimensions: track ? {width: track.displayWidth, height: track.displayHeight} : undefined}; }; ``` Define `Props` for your component and use the same fps as its Composition: ```tsx import {CalculateMetadataFunction} from 'remotion'; import {getMediaMetadata} from '../get-media-metadata'; const calculateMetadata: CalculateMetadataFunction = async ({props}) => { const {durationInSeconds} = await getMediaMetadata(props.videoSrc); return { durationInFrames: Math.ceil(durationInSeconds * 30), }; }; ``` ## Matching dimensions of a video ```tsx const calculateMetadata: CalculateMetadataFunction = async ({props}) => { const {durationInSeconds, dimensions} = await getMediaMetadata(props.videoSrc); return { durationInFrames: Math.ceil(durationInSeconds * 30), width: dimensions?.width ?? 1920, height: dimensions?.height ?? 1080, }; }; ``` ## Setting duration based on multiple videos ```tsx const calculateMetadata: CalculateMetadataFunction = async ({props}) => { const metadataPromises = props.videos.map((video) => getMediaMetadata(video.src)); const allMetadata = await Promise.all(metadataPromises); const totalDuration = allMetadata.reduce((sum, meta) => sum + meta.durationInSeconds, 0); return { durationInFrames: Math.ceil(totalDuration * 30), }; }; ``` ## Setting a default outName Set the default output filename based on props: ```tsx const calculateMetadata: CalculateMetadataFunction = async ({props}) => { return { defaultOutName: `video-${props.id}.mp4`, }; }; ``` ## Transforming props Fetch data or transform props before rendering: ```tsx const calculateMetadata: CalculateMetadataFunction = async ({props, abortSignal}) => { const response = await fetch(props.dataUrl, {signal: abortSignal}); const data = await response.json(); return { props: { ...props, fetchedData: data, }, }; }; ``` The `abortSignal` cancels stale requests when props change in the Studio. ## Return value All fields are optional. Returned values override the `` props: - `durationInFrames`: Number of frames - `width`: Composition width in pixels - `height`: Composition height in pixels - `fps`: Frames per second - `props`: Transformed props passed to the component - `defaultOutName`: Default output filename - `defaultCodec`: Default codec for rendering