Skip to main content

自定义层

Lambda函数默认包含Chrome和基本字体

在一些高级用例中,您可能希望替换堆栈的某些部分:

  • 使用另一个Chrome版本(您可能需要自行构建
  • 替换默认字体或表情符号

在创建自定义堆栈之前,请随时与我们联系,以查看Remotion是否可以提供上游更改。

还要考虑到AWS Lambda层的累积提取量不得超过250MB,因此您需要牺牲相等数量的其他文件。

如果您只想添加字体,我们建议使用Web字体

默认层v4.0.205

在创建自定义堆栈之前,请考虑使用我们的层选项:

使用默认选项(--runtime-preference=cjk或无选项):

项目大小
chromium188 MB
emoji-google9.9 MB
字体1.9 MB
cjk16 MB
总计215.8 MB

使用选项--runtime-preference=apple-emojis

项目大小
chromium188 MB
emoji-apple45 MB
字体1.9 MB
总计235.9 MB
note

请注意,启用苹果表情符号后,为保持在250MB限制以下,将删除对CJK(中文、日文、韩文)字符的支持。
Google表情符号也将被删除。

确保Remotion版本

从v3.0.17开始,可以自定义Remotion Lambda层。
Lambda二进制文件可能会在较小的Remotion版本中更改,您有责任保持版本最新。

创建自定义二进制文件

转到remotion-dev/lambda-binaries存储库并克隆它。

文件夹 chromiumfonts 包含 ARM 版本的二进制文件。x64 版本已经停止支持。

将您想要的文件放入相应的文件夹 - 例如,将 Apple Emoji 字体 AppleColorEmoji.ttf 添加到 fonts/.fonts/NotoSans/ 文件夹中。

由于 AWS Lambda 层的提取文件大小不能超过 250MB,我们需要牺牲相同大小的其他文件 - 例如,通过移除 fonts/.fonts/NotoColorEmoji.ttffonts/.fonts/NotoSansCJKjp-Regular.otf

您可以通过运行以下命令查看文件夹的大小:

bash
sh size.sh
bash
sh size.sh

完成更改后,请运行:

bash
sh make.sh
bash
sh make.sh

这将压缩层并将其作为构件放入 out 目录中。

创建 Lambda 层

  • 转到 AWS 控制台,选择 Lambda 产品,然后选择“层”:
  • 选择“创建层”并填写名称。将从 out 文件夹中创建的层上传到表单中。字段“兼容架构”、“兼容运行时”和“许可证”是可选的。

  • 创建层后,您将获得一个版本 ARN(示例:arn:aws:lambda:us-east-1:123456789012:layer:apple-emoji:1)。复制它。

note

您需要为要在 Remotion Lambda 中使用的每个 AWS 区域执行此操作。

更新 Lambda 函数

在继续之前,请确保已部署了 Remotion Lambda 函数。

要更改已部署 Lambda 函数的层,您可以通过 AWS 控制台进行操作,也可以使用一个 Node.JS 脚本,在每次函数部署后运行。

在您可以通过 Node.JS APIs 更新函数之前,您需要为用户角色添加另一个规则

json
[
{
"Sid": "UpdateFunction",
"Effect": "Allow",
"Action": [
"lambda:GetFunctionConfiguration",
"lambda:UpdateFunctionConfiguration"
],
"Resource": ["arn:aws:lambda:*:*:function:remotion-render-*"]
},
{
"Sid": "GetOwnLayerVersion",
"Effect": "Allow",
"Action": ["lambda:GetLayerVersion"],
"Resource": ["*"]
}
]
json
[
{
"Sid": "UpdateFunction",
"Effect": "Allow",
"Action": [
"lambda:GetFunctionConfiguration",
"lambda:UpdateFunctionConfiguration"
],
"Resource": ["arn:aws:lambda:*:*:function:remotion-render-*"]
},
{
"Sid": "GetOwnLayerVersion",
"Effect": "Allow",
"Action": ["lambda:GetLayerVersion"],
"Resource": ["*"]
}
]

给定一个区域、函数名称、要移除的层和要添加的层,您可以使用以下代码片段来更新具有自定义层的函数

ts
import { AwsRegion, getAwsClient } from "@remotion/lambda";
// Customize these parameters
const REGION: AwsRegion = "us-east-1";
const FUNCTION_NAME = "remotion-render-2022-06-02-mem3000mb-disk2048mb-120sec";
const LAYER_TO_REMOVE = /fonts/;
const LAYER_TO_ADD = "arn:aws:lambda:us-east-1:1234567891:layer:apple-emoji:1";
const { client, sdk } = getAwsClient({
region: REGION,
service: "lambda",
});
const fnConfig = await client.send(
new sdk.GetFunctionConfigurationCommand({
FunctionName: FUNCTION_NAME,
}),
);
if (!fnConfig) {
throw new Error(`Function ${FUNCTION_NAME} not deployed`);
}
await client.send(
new sdk.UpdateFunctionConfigurationCommand({
FunctionName: FUNCTION_NAME,
Layers: [
...(fnConfig.Layers ?? [])
.filter((l) => !l.Arn?.match(LAYER_TO_REMOVE))
.map((l) => l.Arn as string),
LAYER_TO_ADD,
],
}),
);
ts
import { AwsRegion, getAwsClient } from "@remotion/lambda";
// Customize these parameters
const REGION: AwsRegion = "us-east-1";
const FUNCTION_NAME = "remotion-render-2022-06-02-mem3000mb-disk2048mb-120sec";
const LAYER_TO_REMOVE = /fonts/;
const LAYER_TO_ADD = "arn:aws:lambda:us-east-1:1234567891:layer:apple-emoji:1";
const { client, sdk } = getAwsClient({
region: REGION,
service: "lambda",
});
const fnConfig = await client.send(
new sdk.GetFunctionConfigurationCommand({
FunctionName: FUNCTION_NAME,
}),
);
if (!fnConfig) {
throw new Error(`Function ${FUNCTION_NAME} not deployed`);
}
await client.send(
new sdk.UpdateFunctionConfigurationCommand({
FunctionName: FUNCTION_NAME,
Layers: [
...(fnConfig.Layers ?? [])
.filter((l) => !l.Arn?.match(LAYER_TO_REMOVE))
.map((l) => l.Arn as string),
LAYER_TO_ADD,
],
}),
);

参见