Lambda 並行處理
說明 Remotion Lambda 的高並行分散式渲染架構,包含如何透過 framesPerLambda 與 concurrency 參數控制渲染並行度,以及預設值與限制說明。
概覽
Remotion Lambda 是一個高度並行的分散式影片渲染系統,它會將影片渲染工作分配給多個 Lambda 函式平行執行。你可以自訂使用的函式數量,或讓 Remotion 自動決定最佳值。
並行度的定義
並行度(concurrency)定義為:
並行度 = 影格總數(frameCount)/ 每個 Lambda 的影格數(framesPerLambda)
因此,framesPerLambda 設定越高,並行度越低;反之,設定越低,並行度越高、使用更多 Lambda 函式平行渲染。
範例:
- 影片共 300 影格(frames)
framesPerLambda設為15- 並行度 = 300 / 15 = 20 個 Lambda 函式
設定並行度
方法一:使用 framesPerLambda
透過 CLI 設定:
npx remotion lambda render <serve-url> <composition> --frames-per-lambda=20透過 Node.js API 設定:
import { renderMediaOnLambda } from "@remotion/lambda/client";
await renderMediaOnLambda({
region: "us-east-1",
functionName: "remotion-render-xxxxx",
serveUrl: "https://remotionlambda-xxxxx.s3.amazonaws.com/sites/my-video/index.html",
composition: "MyComposition",
inputProps: {},
codec: "h264",
framesPerLambda: 20, // 每個 Lambda 渲染 20 影格
});方法二:使用 concurrency
若你不想計算 framesPerLambda 的值,可以直接指定 Lambda 函式的數量:
npx remotion lambda render <serve-url> <composition> --concurrency=50await renderMediaOnLambda({
region: "us-east-1",
functionName: "remotion-render-xxxxx",
serveUrl: "...",
composition: "MyComposition",
inputProps: {},
codec: "h264",
concurrency: 50, // 直接指定使用 50 個 Lambda 函式
});預設值
當未指定 framesPerLambda 或 concurrency 時,Remotion 會根據影片長度自動選擇 framesPerLambda 值(介於 20 到無限大之間)。影片越長,並行度越高。
最低保障: 無論影片多短,每個 Lambda 至少渲染 20 影格。
決定 framesPerLambda 的演算法如下:
import { interpolate } from "remotion";
const bestFramesPerLambdaParam = (frameCount: number) => {
// 在 0 到 10 分鐘(30fps)之間,將並行度從 75 插值到 150
const concurrency = interpolate(frameCount, [0, 18000], [75, 150], {
extrapolateRight: "clamp",
});
// framesPerLambda 最少為 20
const framesPerLambda = Math.max(frameCount / concurrency, 20);
// 均勻分配:21 影格分給 2 個 Lambda = 11 + 10
const lambdasNeeded = Math.ceil(frameCount / framesPerLambda);
return Math.ceil(frameCount / lambdasNeeded);
};並行度限制
設定值必須在以下範圍內,否則渲染會拋出錯誤:
| 限制項目 | 最小值 | 最大值 |
|---|---|---|
framesPerLambda | 5(Remotion 4.0.331 以前為 4) | 無限制 |
| 並行度(concurrency) | 1 | 200 |
Remotion 的預設值永遠不會超出這些範圍。
錯誤排解:「Too many functions」
如果你看到以下錯誤:
Too many functions: This render would cause [X] functions to spawn.
We limit this amount to 200 functions as more would result in diminishing returns.
這表示你設定的 framesPerLambda 太低,導致需要生成超過 200 個函式。
解決方法:
- 將
framesPerLambda設為null讓 Remotion 自動選擇合理的值:
await renderMediaOnLambda({
// ...
framesPerLambda: null, // 讓 Remotion 自動決定
});- 若要手動設定,確保
concurrency不超過 200:
// 若影片有 1000 影格,framesPerLambda 至少要設為 5
// 1000 / 5 = 200(剛好在限制內)
await renderMediaOnLambda({
// ...
framesPerLambda: 5,
});根據實際經驗,並行度超過 200 後渲染速度不會再有顯著提升。
並行度與 AWS 配額的關係
每次渲染可能使用多達 200 個並行 Lambda 函式。若你的 AWS 帳戶並行限制較低(新帳戶可能只有 10),你可能需要提前申請提高配額:
# 查看目前的並行限制
npx remotion lambda quotas