從 Python 使用 Lambda
如何使用 Python 搭配 remotion-lambda pip 套件,透過 Remotion Lambda 觸發影片與靜態圖片的渲染請求,包含 boto3 整合、HTTP API 呼叫與大型輸入參數的自動處理。
從 Python 使用 Lambda
此功能自 Remotion v4.0.15 起提供。
本頁說明如何從 Python 應用程式觸發 Remotion Lambda 渲染,包含套件安裝、boto3 整合與完整的程式碼範例。
安裝
透過 pip 安裝 remotion-lambda 套件:
pip install remotion-lambda請使用與 NPM 上 Remotion 版本相同的版本號以確保相容性:
pip install remotion-lambda==4.0.15可在 PyPI 上查看最新可用版本。
前置條件
在繼續之前,請先完成 Lambda 環境設定,確保已完成:
- 建立 IAM 角色與政策
- 部署 Lambda 函式
- 部署 Remotion 站台至 S3
- 設定 AWS 憑證(環境變數或
~/.aws/credentials)
大型輸入參數支援
| 版本 | 行為 |
|---|---|
| v4.0.315 之前 | 不支援超過 200KB 的輸入參數 |
| v4.0.315 起 | 超過限制時自動上傳至 S3(影片/音訊約 194KB,靜態圖片約 4.9MB) |
SDK 會自動尋找或建立符合 Remotion 命名規範的 S3 儲存桶,整個過程對使用者完全透明。
環境變數設定
請在 .env 檔案中設定以下環境變數:
| 變數名稱 | 說明 |
|---|---|
REMOTION_APP_REGION | 部署的 AWS 區域,例如 us-east-1 |
REMOTION_APP_FUNCTION_NAME | Lambda 函式名稱 |
REMOTION_APP_SERVE_URL | Webpack 打包的服務 URL |
AWS_ACCESS_KEY_ID | AWS 存取金鑰 ID |
AWS_SECRET_ACCESS_KEY | AWS 秘密存取金鑰 |
渲染影片範例
以下範例展示如何發起影片渲染請求並追蹤渲染進度:
# render_media.py
from remotion_lambda import RenderMediaParams, Privacy, ValidStillImageFormats
from remotion_lambda import RemotionClient
import os
from dotenv import load_dotenv
load_dotenv()
# 載入並驗證環境變數
REMOTION_APP_REGION = os.getenv('REMOTION_APP_REGION')
if not REMOTION_APP_REGION:
raise Exception("REMOTION_APP_REGION is not set")
REMOTION_APP_FUNCTION_NAME = os.getenv('REMOTION_APP_FUNCTION_NAME')
if not REMOTION_APP_FUNCTION_NAME:
raise Exception("REMOTION_APP_FUNCTION_NAME is not set")
REMOTION_APP_SERVE_URL = os.getenv('REMOTION_APP_SERVE_URL')
if not REMOTION_APP_SERVE_URL:
raise Exception("REMOTION_APP_SERVE_URL is not set")
# 建立客戶端
client = RemotionClient(
region=REMOTION_APP_REGION,
serve_url=REMOTION_APP_SERVE_URL,
function_name=REMOTION_APP_FUNCTION_NAME
)
# 設定渲染請求
render_params = RenderMediaParams(
composition="MyVideo",
privacy=Privacy.PUBLIC,
image_format=ValidStillImageFormats.JPEG,
input_props={
'title': '我的影片',
'subtitle': '由 Python 觸發的渲染',
},
)
render_response = client.render_media_on_lambda(render_params)
if render_response:
print("渲染 ID:", render_response.render_id)
print("儲存桶名稱:", render_response.bucket_name)
# 輪詢渲染進度
progress_response = client.get_render_progress(
render_id=render_response.render_id,
bucket_name=render_response.bucket_name
)
while (progress_response and
not progress_response.done and
not progress_response.fatalErrorEncountered):
if progress_response.fatalErrorEncountered:
print("發生嚴重錯誤")
print(progress_response.errors)
break
print("整體進度:" + str(progress_response.overallProgress * 100) + "%")
progress_response = client.get_render_progress(
render_id=render_response.render_id,
bucket_name=render_response.bucket_name
)
print("渲染完成!")
print("輸出檔案:", progress_response.outputFile)渲染靜態圖片範例
靜態圖片渲染是同步操作,不需要輪詢進度:
# render_still.py
from remotion_lambda import RenderStillParams, Privacy, ValidStillImageFormats
from remotion_lambda import RemotionClient
import os
from dotenv import load_dotenv
load_dotenv()
REMOTION_APP_REGION = os.getenv('REMOTION_APP_REGION')
REMOTION_APP_FUNCTION_NAME = os.getenv('REMOTION_APP_FUNCTION_NAME')
REMOTION_APP_SERVE_URL = os.getenv('REMOTION_APP_SERVE_URL')
client = RemotionClient(
region=REMOTION_APP_REGION,
serve_url=REMOTION_APP_SERVE_URL,
function_name=REMOTION_APP_FUNCTION_NAME
)
# 設定靜態圖片渲染請求
render_params = RenderStillParams(
composition="Thumbnail",
privacy=Privacy.PUBLIC,
image_format=ValidStillImageFormats.PNG,
input_props={
'title': '縮圖標題',
'background': '#ffffff',
},
)
render_response = client.render_still_on_lambda(render_params)
if render_response:
print("輸出 URL:", render_response.url)大型輸入參數範例
自 v4.0.315 起,超過限制的輸入參數會自動上傳至 S3 處理:
# large_props_example.py
# 大型輸入參數會自動處理,無需額外設定
large_props = {
'subtitles': [{'text': '字幕行 ' + str(i), 'time': i * 1000} for i in range(500)],
'description': '這筆資料超過 200KB,將自動上傳至 S3',
}
render_params = RenderMediaParams(
composition="SubtitleVideo",
input_props=large_props, # 自動處理大型參數
privacy=Privacy.PUBLIC,
)
response = client.render_media_on_lambda(render_params)
print("渲染 ID:", response.render_id)boto3 直接整合
若你的應用程式已使用 boto3,也可以直接呼叫 Lambda API,不透過 remotion-lambda SDK:
import boto3
import json
lambda_client = boto3.client('lambda', region_name='us-east-1')
payload = {
"type": "start",
"serveUrl": "https://remotionlambda-abc123.s3.amazonaws.com/sites/my-video/index.html",
"composition": "MyVideo",
"inputProps": {"title": "Hello"},
"codec": "h264",
"imageFormat": "jpeg",
"maxRetries": 1,
"framesPerLambda": 20,
"privacy": "public",
}
response = lambda_client.invoke(
FunctionName='remotion-render-4-0-0-mem2048mb-disk2048mb-120sec',
InvocationType='RequestResponse',
Payload=json.dumps(payload),
)
result = json.loads(response['Payload'].read())
print("渲染 ID:", result.get('renderId'))
print("儲存桶:", result.get('bucketName'))建議使用官方
remotion-lambdaSDK 而非直接呼叫 Lambda,以獲得更好的型別安全與版本相容性保障。