Remotion LabRemotion Lab
Cloud RunCloud Run 設定

Cloud Run 設定

在 Google Cloud Run 上設定 Remotion 渲染環境的完整指南,涵蓋 GCP 專案建立、服務部署及基本設定。

Cloud Run 設定

Google Cloud Run 讓你能以無伺服器方式在雲端執行 Remotion 渲染任務,無需自行管理底層基礎設施。本文說明如何從零開始完成整套設定。

實驗性功能:Cloud Run 目前處於 Alpha 狀態,並未積極開發中。

前置需求

步驟一:安裝套件

npm install @remotion/cloudrun

步驟二:建立 GCP 專案

前往 Google Cloud Console 管理資源頁面,點擊「建立專案」。

接著在 Cloud Shell 或本機終端機登入並設定專案:

gcloud auth login
gcloud config set project YOUR_PROJECT_ID

啟用所需的 GCP API:

gcloud services enable run.googleapis.com
gcloud services enable artifactregistry.googleapis.com
gcloud services enable storage.googleapis.com
gcloud services enable cloudbuild.googleapis.com

步驟三:設定權限與服務帳戶

使用 Remotion 提供的安裝腳本,一鍵完成 IAM 設定:

curl -L https://github.com/remotion-dev/remotion/raw/main/packages/cloudrun/src/gcpInstaller/gcpInstaller.tar \
  | tar -x -C . && node install.mjs

依提示選擇操作:

  • 1:為此 GCP 專案設定 Remotion Cloud Run
  • 2:產生或管理 .env 服務帳戶金鑰

完成後將產生的環境變數複製到本機 .env 檔案:

# 查看產生的環境變數
cat .env

本機 .env 範例內容:

REMOTION_GCP_PROJECT_ID=my-remotion-project
REMOTION_GCP_CLIENT_EMAIL=remotion-sa@my-remotion-project.iam.gserviceaccount.com
REMOTION_GCP_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n..."

步驟四:驗證權限

確認 GCP 權限設定正確:

npx remotion cloudrun permissions

若設定正確,所有項目應顯示為通過狀態。

步驟五:部署 Cloud Run 服務

npx remotion cloudrun services deploy

指定地區與資源配置:

npx remotion cloudrun services deploy \
  --region=asia-east1 \
  --cpu=4 \
  --memory=8Gi \
  --timeout=1800

也可使用 Node.js API:

import { deployService } from "@remotion/cloudrun";
 
const deployResult = await deployService({
  projectID: "my-remotion-project",
  region: "asia-east1",
});
 
console.log("服務名稱:", deployResult.fullName);

步驟六:部署 Remotion 站台

將 Remotion 專案打包並上傳至 Cloud Storage:

npx remotion cloudrun sites create src/index.ts --site-name=my-site

成功後會回傳 serveUrl,之後渲染時需要使用:

Site: my-site
Serve URL: https://storage.googleapis.com/remotion-my-project/sites/my-site/index.html

步驟七:渲染第一支影片

npx remotion cloudrun render \
  https://storage.googleapis.com/my-bucket/sites/my-site/index.html \
  MyComposition

或使用 Node.js API:

import { renderMediaOnCloudrun } from "@remotion/cloudrun";
 
const result = await renderMediaOnCloudrun({
  region: "asia-east1",
  serviceName: "remotion-render-4-0-0",
  serveUrl: "https://storage.googleapis.com/...",
  composition: "MyComposition",
  inputProps: { title: "我的影片" },
  codec: "h264",
});
 
if (result.type === "success") {
  console.log("輸出網址:", result.publicUrl);
}

服務版本管理

Cloud Run 服務版本必須與本機 @remotion/cloudrun 套件版本一致。升級後需重新部署:

# 列出現有服務
npx remotion cloudrun services ls
 
# 刪除舊版本
npx remotion cloudrun services rm remotion-render-OLD_VERSION --region=asia-east1
 
# 部署新版本
npx remotion cloudrun services deploy --region=asia-east1

常用資源設定對照表

影片類型建議 CPU建議記憶體建議逾時
短片(< 30 秒)24Gi300 秒
中片(30–120 秒)48Gi900 秒
長片(> 120 秒)816Gi3600 秒

小結

  • 安裝 @remotion/cloudrun 並使用安裝腳本設定 GCP 權限
  • 使用 npx remotion cloudrun services deploy 部署渲染服務
  • 使用 npx remotion cloudrun sites create 上傳 Remotion 專案
  • 服務版本需與本機套件版本一致,升級時需重新部署

另請參閱