Skip to main content

从 Python 触发渲染

从 v4.0.15 起可用

🌐 available from v4.0.15

要使用 Python 触发 Lambda 渲染,请使用 pip 安装 remotion-lambda 包,通过在你的 Python 项目中执行 pip install remotion-lambda。使用与你从 NPM 使用的 remotion 版本相同的版本,例如 pip install remotion-lambda==4.0.15(参见 最新版本)。

🌐 To trigger a Lambda render using Python, install the remotion-lambda package using pip, by executing pip install remotion-lambda from your python project. Use the same version as the remotion version you are using from NPM, e.g. pip install remotion-lambda==4.0.15 (see newest version).

你首先需要完成 Lambda 设置

🌐 You first need to complete the Lambda setup.

限制

🌐 Limitations

在 v4.0.315 之前:不支持发送大输入属性(>200KB)。

🌐 Before v4.0.315: Sending large input props (>200KB) was not supported.

从 v4.0.315 开始,Python SDK 会自动处理超大输入属性,当它们超过 AWS Lambda 负载限制时,会将其上传到 S3。对于视频渲染,属性大于 200KB,对于静态渲染,属性大于 5MB 时,会自动使用与 JavaScript SDK 相同的逻辑进行压缩。SDK 会根据 Remotion 的命名规范自动查找或创建合适的 S3 存储桶。

🌐 Starting from v4.0.315, the Python SDK automatically handles large input props by uploading them to S3 when they exceed AWS Lambda payload limits. Props larger than 200KB for video renders and 5MB for still renders are automatically compressed using the same logic as the JavaScript SDK. The SDK will automatically find or create an appropriate S3 bucket following Remotion's naming conventions.

渲染视频

🌐 Rendering a video

下面是一个片段,展示了如何发起渲染请求并获取其状态。

🌐 Below is a snippet showing how to initiate a render request and get its status.

render_media.py
from remotion_lambda import RenderMediaParams, Privacy, ValidStillImageFormats from remotion_lambda import RemotionClient import os from dotenv import load_dotenv load_dotenv() # Load env variables 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") # Construct client client = RemotionClient(region=REMOTION_APP_REGION, serve_url=REMOTION_APP_SERVE_URL, function_name=REMOTION_APP_FUNCTION_NAME) # Set render request render_params = RenderMediaParams( composition="react-svg", privacy=Privacy.PUBLIC, image_format=ValidStillImageFormats.JPEG, input_props={ 'hi': 'there' }, ) render_response = client.render_media_on_lambda(render_params) if render_response: # Execute render request print("Render ID:", render_response.render_id) print("Bucket name:", render_response.bucket_name) # Execute progress request 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("Fatal error encountered") print(progress_response.errors) break print("Overall progress") 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("Render done!", progress_response.outputFile)

大型输入属性 从 v4.0.315 起可用

🌐 Large input props available from v4.0.315

当输入属性超过 AWS Lambda 负载限制时,Python SDK 会自动通过将它们上传到 S3 来处理大型输入属性:

🌐 The Python SDK automatically handles large input props by uploading them to S3 when they exceed AWS Lambda payload limits:

  • 视频/音频渲染:属性大于约194KB
  • 仍然渲染:属性大于约4.9MB

当检测到大型属性时,它们会被自动上传到 S3,并由 Lambda 函数引用。SDK 将会找到现有的 Remotion 存储桶,或自动创建一个新的存储桶,逻辑与 JavaScript SDK 相同。此过程对用户是透明的。

🌐 When large props are detected, they are automatically uploaded to S3 and referenced by the Lambda function. The SDK will find an existing Remotion bucket or create a new one automatically, following the same logic as the JavaScript SDK. This process is transparent to the user.

large_props_example.py
# Large input props are handled automatically large_props = { 'bigData': ['x' * 1000] * 250, # ~250KB of data 'description': 'This will be automatically uploaded to S3' } render_params = RenderMediaParams( composition="my-composition", input_props=large_props, # Automatically compressed # ... other parameters ) # The render works the same way, compression is handled internally response = client.render_media_on_lambda(render_params)

渲染图片

🌐 Rendering an image

下面是一个片段,展示了如何启动静态图片渲染。请注意,它不需要监控渲染进度。

🌐 Below is a snippet showing how to initiate a still image render. Note that it does not require monitoring the render progress.

render_still.py
from remotion_lambda import RenderStillParams, Privacy, ValidStillImageFormats from remotion_lambda import RemotionClient import os from dotenv import load_dotenv load_dotenv() # Load env variables 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") # Construct client client = RemotionClient(region=REMOTION_APP_REGION, serve_url=REMOTION_APP_SERVE_URL, function_name=REMOTION_APP_FUNCTION_NAME, access_key=None, # deprecated, use session instead secret_key=None, # deprecated, use session instead force_path_style=False, # By default False config=None, # Custom botocore configuration object session=None, # Pre-configured boto3 Session object (recommended for authentication) ) # Set render still request render_params = RenderStillParams( composition="still-helloworld", privacy=Privacy.PUBLIC, image_format=ValidStillImageFormats.JPEG, input_props={ 'message': 'Hello from props!' }, ) render_response = client.render_still_on_lambda(render_params) if render_response: # Execute render request print("Render ID:", render_response.render_id) print("Bucket name:", render_response.bucket_name) print("Render done! File at ", render_response.url)

重大变更

🌐 Breaking changes

4.0.380

  • RemotionClient 构造函数中添加了 configsession 参数。
  • 减少错误的重新打包,将更多的 AWS 错误直接传递给用户。

4.0.82

  • data 字段现在是 input_props
  • RenderParams 已被重命名为 RenderMediaParams
  • RenderProgress 已被重命名为 RenderMediaProgress
  • RenderResponse 现在是 RenderMediaResponse,其字段已更新:renderId 现在是 render_id,并且 bucketName 已更改为 bucket_name
  • 已添加以下类型:CostsInfoPrivacyValidStillImageFormatsLogLevelOpenGlRendererChromiumOptionsCustomCredentialsWithoutSensitiveDataCustomCredentialsOutNameInputObjectPlayInBrowserDownloadDeleteAfter。这些可以用于RenderMediaParamsRenderStillParams上的字段。

另请参阅

🌐 See also