使用 SSR API 进行渲染
NPM 包 @remotion/renderer 为你提供了用于以编程方式渲染媒体的“服务器端渲染(Server-Side Rendering)”API。 这些函数可在 Node.js 和 Bun 中使用。
🌐 The NPM package @remotion/renderer provides you with "Server-Side Rendering" APIs for rendering media programmatically.
These functions can be used in Node.js and Bun.
渲染视频需要三个步骤:
🌐 Rendering a video takes three steps:
示例脚本
🌐 Example script
按照这个带注释的示例查看如何渲染视频:
🌐 Follow this commented example to see how to render a video:
render.mjsimport {bundle } from '@remotion/bundler'; import {renderMedia ,selectComposition } from '@remotion/renderer'; importpath from 'path'; // The composition you want to render constcompositionId = 'HelloWorld'; // You only have to create a bundle once, and you may reuse it // for multiple renders that you can parametrize using input props. constbundleLocation = awaitbundle ({entryPoint :path .resolve ('./src/index.ts'), // If you have a webpack override in remotion.config.ts, pass it here as well.webpackOverride : (config ) =>config , }); // Parametrize the video by passing props to your component. constinputProps = {foo : 'bar', }; // Get the composition you want to render. Pass `inputProps` if you // want to customize the duration or other metadata. constcomposition = awaitselectComposition ({serveUrl :bundleLocation ,id :compositionId ,inputProps , }); // Render the video. Pass the same `inputProps` again // if your video is parametrized with data. awaitrenderMedia ({composition ,serveUrl :bundleLocation ,codec : 'h264',outputLocation : `out/${compositionId }.mp4`,inputProps , });console .log ('Render done!');
此流程可自定义。点击任意一个 SSR API 以了解其选项:
🌐 This flow is customizable. Click on one of the SSR APIs to read about its options:
getCompositions()- 从 Remotion 项目获取可用合成的列表。selectComposition()- 从 Remotion 项目获取可用合成的列表。renderMedia()- 渲染视频或音频。renderFrames()- 渲染图片序列。renderStill()- 渲染静态图片。stitchFramesToVideo()- 基于图片序列编码视频openBrowser()- 在函数调用之间共享浏览器实例以提高性能。
Linux 依赖
🌐 Linux Dependencies
如果你使用的是 Linux,Chrome 无头模式 Shell 需要安装一些共享库。请参阅 Linux 依赖。
🌐 If you are on Linux, Chrome Headless Shell requires some shared libraries to be installed. See Linux Dependencies.
Next.js 中的 SSR API
🌐 SSR APIs in Next.js
如果你正在使用 Next.js,你将无法使用 @remotion/bundler,因为在 我可以在 Next.js 中渲染视频吗? 中解释的限制。请参阅该页面以获取可能的替代方案。
我们推荐在 Next.js 中使用 Lambda。
🌐 If you are using Next.js, you will not be able to use @remotion/bundler because of the limitations explained in Can I render videos in Next.js? Refer to the page for possible alternatives.
We recommend Lambda for use in Next.js.
另请参阅
🌐 See also