calculateMetadata()v4.0.0
calculateMetadata
is a prop that gets passed to <Composition>
and takes a callback function which may transform metadata.
Use it if you:
need to make the duration, width, height, or fps dynamic based on some datawant to transform the props passed to the composition, for example to do data fetching
want to add a per-composition default codec
want to run code once, before the actual render starts.
The calculateMetadata()
function is called a single time, independently from the concurrency of the render.
It runs in a separate tab, as part of the render process calling selectComposition()
.
Usage / API
Define a function, you may type it using CalculateMetadataFunction
- the generic argument takes the props of the component of your composition:
src/Root.tsxtsx
importReact from "react";import {CalculateMetadataFunction ,Composition } from "remotion";import {MyComponent ,MyComponentProps } from "./MyComp";constcalculateMetadata :CalculateMetadataFunction <MyComponentProps > = ({props ,defaultProps ,abortSignal ,}) => {return {// Change the metadatadurationInFrames :props .duration ,// or transform some propsprops ,// or add per-composition default codecdefaultCodec : "h264",};};export constRoot :React .FC = () => {return (<Composition id ="MyComp"component ={MyComponent }durationInFrames ={300}fps ={30}width ={1920}height ={1080}defaultProps ={{text : "Hello World",duration : 1,}}calculateMetadata ={calculateMetadata }/>);};
src/Root.tsxtsx
importReact from "react";import {CalculateMetadataFunction ,Composition } from "remotion";import {MyComponent ,MyComponentProps } from "./MyComp";constcalculateMetadata :CalculateMetadataFunction <MyComponentProps > = ({props ,defaultProps ,abortSignal ,}) => {return {// Change the metadatadurationInFrames :props .duration ,// or transform some propsprops ,// or add per-composition default codecdefaultCodec : "h264",};};export constRoot :React .FC = () => {return (<Composition id ="MyComp"component ={MyComponent }durationInFrames ={300}fps ={30}width ={1920}height ={1080}defaultProps ={{text : "Hello World",duration : 1,}}calculateMetadata ={calculateMetadata }/>);};
As argument, you get an object with the following properties:
defaultProps
: Only the default props, taken from thedefaultProps
prop or the Remotion Studio Data sidebar.props
: The resolved props, taking input props into account.abortSignal
: AnAbortSignal
which you can use to abort network requests if the default props have been changed in the meanwhile.compositionId
(available from v4.0.98): The ID of the current composition
The function must return an pure JSON-serializable object, which can contain the following properties:
props
optional: The final props the component receives. It must have the same shape as theprops
received by this function. The props must be a plain object and must not contain any non-JSON-serializable values, exceptDate
,Map
,Set
andstaticFile()
.durationInFrames
optional: The duration of the composition in frameswidth
optional: The width of the composition in pixelsheight
optional: The height of the composition in pixelsfps
optional: The frames per second of the compositiondefaultCodec
optional: The default codec to use for the composition.
If you return a field, it will take precendence over the props directly passed to the composition. The defaultCodec returned will have a higher priority than the config file, but less priority than explicitly passing the option to renderMedia() i.e. the composition will be rendered with this codec if nothing with higher priority was specified.
The function may be async
.
The function must resolve within the timeout.
The function will get executed every time the props in the Remotion Studio changes.