Skip to main content

calculateMetadata()v4.0.0

calculateMetadata 是一个传递给 <Composition> 的属性,它接受一个回调函数,该函数可能会转换元数据。

如果你:

🌐 Use it if you:

需要根据一些数据使 durationInFrameswidthheightfps 动态化

想要转换传递给组合的 props,例如进行 数据获取

想要为每个组合添加默认的编解码器

想要为每个组合添加默认的视频图片格式、像素格式或 ProRes 配置文件

想要在实际渲染开始前运行一次代码。

calculateMetadata() 函数只被调用一次,与渲染的并发性无关。
它在一个独立的标签页中运行,作为渲染过程的一部分调用 selectComposition()

🌐 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().

使用 / API

🌐 Usage / API

定义一个函数,你可以使用 CalculateMetadataFunction 来编写它——泛型参数接受你组合组件的属性:

🌐 Define a function, you may type it using CalculateMetadataFunction - the generic argument takes the props of the component of your composition:

src/Root.tsx
import React from 'react'; import {CalculateMetadataFunction, Composition} from 'remotion'; import {MyComponent, MyComponentProps} from './MyComp'; const calculateMetadata: CalculateMetadataFunction<MyComponentProps> = ({props, defaultProps, abortSignal, isRendering}) => { return { // Change the metadata durationInFrames: props.duration, // or transform some props props, // or add per-composition default codec defaultCodec: 'h264', // or add per-composition default video image format defaultVideoImageFormat: 'png', // or add per-composition default pixel format defaultPixelFormat: 'yuv420p', // or add per-composition default ProRes profile defaultProResProfile: 'hq', }; }; export const Root: 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:只有默认属性,从 defaultProps 属性或 Remotion Studio 数据侧边栏获取。
  • props:考虑输入属性的已解析属性
  • abortSignal:一个 AbortSignal,如果在此期间 默认属性已被更改,你可以使用它来中止网络请求。
  • compositionId从 v4.0.98 可用):当前合成的 ID
  • isRendering从 v4.0.342 可用):一个布尔值,指示该函数是在渲染过程中(true)调用,还是在其他上下文中(如 Studio)(false)调用

该函数必须返回一个纯 JSON 可序列化对象,该对象可以包含以下属性:

🌐 The function must return an pure JSON-serializable object, which can contain the following properties:

  • props:组件接收到的最终 props。它必须具有与此函数接收到的 props 相同的形状。props 必须是普通对象,并且不得包含任何非 JSON 可序列化的值,除 DateMapSetstaticFile() 外。可选。
  • durationInFrames:作品的时长,以帧为单位。可选。
  • width:画面的宽度(以像素为单位)。可选。
  • height:画面的高度(像素)。可选。
  • fps:合成的每秒帧数。可选。
  • defaultCodec:用于合成的默认编解码器。可选。
  • defaultOutName:渲染时的默认输出名称(不含文件扩展名)。可选。 v4.0.268
  • defaultVideoImageFormat:默认的视频图片格式('png''jpeg''none')。可选。 v4.0.316
  • defaultPixelFormat:默认像素格式('yuv420p''yuva420p''yuv422p''yuv444p''yuv420p10le''yuv422p10le''yuv444p10le''yuva444p10le')。可选。 v4.0.316
  • defaultProResProfile:默认的 ProRes 配置文件('4444-xq''4444''hq''standard''light''proxy')。可选。 v4.0.367

如果你返回一个字段,它将优先于直接传递给组合的 props。返回的默认编解码器(defaultCodec)优先级将高于配置文件,但低于显式将选项传递给 renderMedia() 的优先级,即如果没有指定更高优先级的内容,组合将使用该编解码器进行渲染。

🌐 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.

该函数可能是 async

🌐 The function may be async.

该函数必须在超时内完成。

🌐 The function must resolve within the timeout.

每当 Remotion Studio 中的 props 发生变化时,该函数都会被执行。

🌐 The function will get executed every time the props in the Remotion Studio changes.

兼容性

🌐 Compatibility

BrowsersServersEnvironments
Chrome
Firefox
Safari
Node.js
Bun
Serverless Functions

另请参阅

🌐 See also