设置
1. 安装 @remotion/lambda
🌐 1. Install @remotion/lambda
- Remotion CLI
- npm
- bun
- pnpm
- yarn
npx remotion add @remotion/lambda
This assumes you are currently using v4.0.431 of Remotion.npm i --save-exact @remotion/lambda@4.0.431
Also update
remotion and all `@remotion/*` packages to the same version.Remove all
^ character in front of the version numbers of it as it can lead to a version conflict.This assumes you are currently using v4.0.431 of Remotion.pnpm i @remotion/lambda@4.0.431
Also update
remotion and all `@remotion/*` packages to the same version.Remove all
^ character in front of the version numbers of it as it can lead to a version conflict.This assumes you are currently using v4.0.431 of Remotion.bun i @remotion/lambda@4.0.431
Also update
remotion and all `@remotion/*` packages to the same version.Remove all
^ character in front of the version numbers of it as it can lead to a version conflict.This assumes you are currently using v4.0.431 of Remotion.yarn --exact add @remotion/lambda@4.0.431
Also update
remotion and all `@remotion/*` packages to the same version.Remove all
^ character in front of the version numbers of it as it can lead to a version conflict.2. 创建角色策略
🌐 2. Create role policy
- 前往 AWS 账户 IAM 策略部分
- 点击“创建策略”
- 点击 JSON
- 在你的项目中,在命令行中输入
npx remotion lambda policies role并将其复制到 AWS 的“JSON”字段中。 - 点击下一步。将策略完全命名为
remotion-lambda-policy。其他字段可以保持不变。 - 在标签页面上,你不需要填写任何内容。再次点击下一步。
3. 创建一个角色
🌐 3. Create a role
- 前往 AWS 账户 IAM 角色部分
- 点击“创建角色”。
- 在“用例”下,选择“Lambda”。点击下一步。
- 在“权限策略”下,筛选
remotion-lambda-policy并勾选复选框以分配此策略。点击下一步。 - 在最后一步,准确地命名角色
remotion-lambda-role。其他字段可以保持原样。 - 点击“创建角色”以确认。
4. 创建用户
🌐 4. Create a user
- 前往 AWS 账户 IAM 用户部分
- 点击
Add users - 输入任意用户名,例如
remotion-user。 - 不要勾选“启用控制台访问”选项。你不需要它。
- 点击“下一步”。
- 再次点击“下一步”,无需更改任何设置。你现在应该在“查看并创建”步骤。
- 点击“创建用户”。
5. 为用户创建访问密钥
🌐 5. Create an access key for the user
- 前往 AWS 账户 IAM 用户部分
- 点击在第4步中创建的用户的名称。
- 导航到“安全凭证”选项卡,然后向下滚动到“访问密钥”部分。
- 点击“创建访问密钥”按钮。
- 选择“在 AWS 计算服务上运行的应用”
- 忽略可能出现的警告,并勾选“我理解这些建议……”复选框。
- 点击“下一步”。
- 点击“创建访问密钥”。
- 在项目根目录添加一个
.env文件,并以以下格式添加你刚刚复制的凭证:
.envREMOTION_AWS_ACCESS_KEY_ID=<Access key ID> REMOTION_AWS_SECRET_ACCESS_KEY=<Secret access key>
6. 为你的用户添加权限
🌐 6. Add permissions to your user
- 前往 AWS 账户 IAM 用户部分
- 选择你刚创建的用户。
- 在“权限策略”面板中的“添加权限”下拉菜单下点击“添加内联策略”。
- 点击“JSON”标签。
- 在终端中输入:
npx remotion lambda policies user并将打印出的内容复制到 AWS 文本字段中。 - 点击“查看政策”。
- 给政策起一个名字。例如
remotion-user-policy,但可以是任何名称。 - 点击“创建策略”以确认。
7. 可选:验证权限设置
🌐 7. Optional: Validate the permission setup
检查所有用户权限,并通过执行以下命令将其与 AWS 策略模拟器进行验证:
🌐 Check all user permissions and validate them against the AWS Policy simulator by executing the following command:
npx remotion lambda policies validate对于以下步骤,你可以在命令行接口上执行它们,或者使用 Node.JS API 以编程方式执行。
🌐 For the following steps, you may execute them on the CLI, or programmatically using the Node.JS APIs.
8. 部署一个函数
🌐 8. Deploy a function
- CLI
- Node.JS
通过执行以下命令,在你的 AWS 账户中部署一个可以渲染视频的函数:
🌐 Deploy a function that can render videos into your AWS account by executing the following command:
npx remotion lambda functions deploy你可以使用 deployFunction() 在你的 AWS 账户中部署一个可以渲染视频的函数。
🌐 You can deploy a function that can render videos into your AWS account using deployFunction().
const {functionName } = await deployFunction ({
region : 'us-east-1',
timeoutInSeconds : 120,
memorySizeInMb : 2048,
createCloudWatchLogGroup : true,
});返回了函数名称,你将需要它来进行渲染。
🌐 The function name is returned which you'll need for rendering.
该函数由必要的二进制文件和 JavaScript 代码组成,可以获取一个 服务 URL 并从中进行渲染。函数与 Remotion 版本绑定,如果你升级 Remotion,你 需要部署一个新函数。函数不包括你的 Remotion 代码,这部分将在下一步中部署。
🌐 The function consists of necessary binaries and JavaScript code that can take a serve URL and make renders from it. A function is bound to the Remotion version, if you upgrade Remotion, you need to deploy a new function. A function does not include your Remotion code, it will be deployed in the next step instead.
9. 部署一个网站
🌐 9. Deploy a site
- CLI
- Node.JS
运行以下命令将你的 Remotion 项目部署到 S3 存储桶。将项目的 入口点 作为最后一个参数传递。
🌐 Run the following command to deploy your Remotion project to an S3 bucket. Pass as the last argument the entry point of the project.
npx remotion lambda sites create src/index.ts --site-name=my-video将打印一个指向已部署项目的 Serve URL。
🌐 A Serve URL will be printed pointing to the deployed project.
当你将来更新你的 Remotion 代码时,请重新部署你的网站。传递相同的 --site-name 以覆盖之前的部署。如果你不传递 --site-name,每次部署都会生成一个唯一的 URL。
🌐 When you update your Remotion code in the future, redeploy your site. Pass the same --site-name to overwrite the previous deploy. If you don't pass --site-name, a unique URL will be generated on every deploy.
首先,你需要在首选区域创建一个 S3 存储桶。如果已存在一个,则将使用现有的存储桶:
🌐 First, you need to create an S3 bucket in your preferred region. If one already exists, it will be used instead:
import path from 'path';
import {deploySite , getOrCreateBucket } from '@remotion/lambda';
const {bucketName } = await getOrCreateBucket ({
region : 'us-east-1',
});接下来,将你的 Remotion 项目上传到 S3 存储桶。指定你的 Remotion 项目的入口点,这个文件是调用 registerRoot() 的地方。
🌐 Next, upload your Remotion project to an S3 bucket. Specify the entry point of your Remotion project, this is the file where registerRoot() is called.
const {serveUrl } = await deploySite ({
bucketName ,
entryPoint : path .resolve (process .cwd (), 'src/index.ts'),
region : 'us-east-1',
siteName : 'my-video',
});当你将来更新你的 Remotion 代码时,请重新部署你的网站。传入相同的 siteName 以覆盖之前的部署。如果你不传入 siteName,每次部署都会生成一个唯一的 URL。
🌐 When you update your Remotion code in the future, redeploy your site. Pass the same siteName to overwrite the previous deploy. If you don't pass siteName, a unique URL will be generated on every deploy.
10. 检查 AWS 并发限制
🌐 10. Check AWS concurrency limit
检查 AWS 分配给你账户的并发限制:
🌐 Check the concurrency limit that AWS has given to your account:
npx remotion lambda quotas默认情况下,每个区域有 1000 个并发调用。但是,新账户可能有一个 低至 10 的限制。每次 Remotion 渲染可能会同时使用多达 200 个函数,因此如果你的分配限制非常低, 你可能想立即申请增加。
🌐 By default, it is 1000 concurrent invocations per region. However, new accounts might have a limit as low as 10. Each Remotion render may use as much as 200 functions per render concurrently, so if your assigned limit is very low, you might want to request an increase right away.
11. 渲染视频
🌐 11. Render a video
- CLI
- Node.JS
使用你在第9步收到的URL——你的“服务URL”——并运行以下命令。同时传入你想要渲染的作品ID。
🌐 Take the URL you received from the step 9 - your "serve URL" - and run the following command. Also pass in the ID of the composition you'd like to render.
npx remotion lambda render <serve-url> <composition-id>进度将在视频渲染完成前打印。恭喜!你已经使用 Remotion Lambda 渲染了你的第一个视频 🚀
🌐 Progress will be printed until the video finished rendering. Congrats! You rendered your first video using Remotion Lambda 🚀
你已经在前面的步骤中获得了函数名称。但由于你只需要部署一次函数,所以在渲染视频之前,如果你的 Node.JS 程序重新启动,按程序获取已部署函数的名称是很有用的。我们可以使用 compatibleOnly 标志调用 getFunctions() 来仅获取具有匹配版本的函数。
🌐 You already have the function name from a previous step. But since you only need to deploy a function once, it's useful to retrieve the name of your deployed function programmatically before rendering a video in case your Node.JS program restarts. We can call getFunctions() with the compatibleOnly flag to get only functions with a matching version.
import {getFunctions , renderMediaOnLambda , getRenderProgress } from '@remotion/lambda/client';
const functions = await getFunctions ({
region : 'us-east-1',
compatibleOnly : true,
});
const functionName = functions [0].functionName ;我们现在可以使用 renderMediaOnLambda() 函数触发渲染。
🌐 We can now trigger a render using the renderMediaOnLambda() function.
const {renderId , bucketName } = await renderMediaOnLambda ({
region : 'us-east-1',
functionName ,
serveUrl : url ,
composition : 'HelloWorld',
inputProps : {},
codec : 'h264',
imageFormat : 'jpeg',
maxRetries : 1,
framesPerLambda : 20,
privacy : 'public',
});渲染现在将运行,一段时间后视频将可在你的 S3 存储桶中使用。你可以随时通过调用 getRenderProgress() 获取视频渲染的状态。
🌐 The render will now run and after a while the video will be available in your S3 bucket. You can at any time get the status of the video render by calling getRenderProgress().
while (true) {
await new Promise ((resolve ) => setTimeout (resolve , 1000));
const progress = await getRenderProgress ({
renderId ,
bucketName ,
functionName ,
region : 'us-east-1',
});
if (progress .done ) {
console .log ('Render finished!', progress .outputFile );
process .exit (0);
}
if (progress .fatalErrorEncountered ) {
console .error ('Error enountered', progress .errors );
process .exit (1);
}
}此代码将每秒轮询一次以检查视频的进度,如果渲染完成则退出脚本。恭喜!检查你的 S3 存储桶 - 你刚刚使用 Remotion Lambda 渲染了你的第一个视频 🚀
🌐 This code will poll every second to check the progress of the video and exit the script if the render is done. Congrats! Check your S3 Bucket - you just rendered your first video using Remotion Lambda 🚀
下一步
🌐 Next steps
- 选择你想在其中运行 Remotion Lambda 的区域。
- 熟悉命令行接口(CLI)和 Node.JS 的 API(在侧边栏列出)。
- 了解如何升级 Remotion Lambda。
- 在上线之前,查看生产检查清单。
- 如果你有任何问题,请查看常见问题或在我们的Discord通道中提问
