Skip to content

🎥 Make videos programmatically with React

add working render still

Alex Fernandez8dd35945f14b
diff --git a/packages/lambda-python/build.mjs b/packages/lambda-python/build.mjs
index ac89f196e1..7ae1e0b4fa 100644
--- a/packages/lambda-python/build.mjs
+++ b/packages/lambda-python/build.mjs
@@ -19,7 +19,7 @@ if (!hasPython()) {
const commands = [
"python -m venv remotion-env-lint",
". ./remotion-env-lint/bin/activate",
- "pip install boto3 pylint",
+ "pip install boto3 pylint attr",
"pylint ./remotion_lambda",
"deactivate",
"rm -rf remotion-env-lint",
diff --git a/packages/lambda-python/remotion_lambda/models.py b/packages/lambda-python/remotion_lambda/models.py
index 8300dcef91..e1b958d777 100644
--- a/packages/lambda-python/remotion_lambda/models.py
+++ b/packages/lambda-python/remotion_lambda/models.py
@@ -7,6 +7,172 @@ from .version import VERSION
# pylint: disable=too-many-instance-attributes
+
+
+class ValidStillImageFormats(str, Enum):
+ """
+ Enumeration of valid image formats for still images.
+
+ Attributes:
+ PNG: Represents the PNG image format.
+ JPEG: Represents the JPEG image format.
+ PDF: Represents the PDF format for images.
+ WEBP: Represents the WEBP image format.
+ """
+ PNG: str = 'png'
+ JPEG: str = 'jpeg'
+ PDF: str = 'pdf'
+ WEBP: str = 'webp'
+
+
+class Privacy(str, Enum):
+ """
+ Enumeration of privacy settings.
+
+ Attributes:
+ PUBLIC: Indicates a public setting.
+ PRIVATE: Indicates a private setting.
+ """
+ PUBLIC: str = 'public'
+ PRIVATE: str = 'private'
+
+
+class LogLevel(str, Enum):
+ """
+ Enumeration for different levels of logging.
+
+ Attributes:
+ VERBOSE: Verbose logging level.
+ INFO: Informational logging level.
+ WARN: Warning logging level.
+ ERROR: Error logging level.
+ """
+ VERBOSE = 'verbose'
+ INFO = 'info'
+ WARN = 'warn'
+ ERROR = 'error'
+
+
+class OpenGlRenderer(str, Enum):
+ """
+ Enumeration of OpenGL renderer options.
+
+ Attributes:
+ SWANGLE: Represents the SWANGLE OpenGL renderer.
+ ANGLE: Represents the ANGLE OpenGL renderer.
+ EGL: Represents the EGL OpenGL renderer.
+ SWIFTSHADER: Represents the SWIFTSHADER OpenGL renderer.
+ VULKAN: Represents the VULKAN OpenGL renderer.
+ """
+ SWANGLE: str = 'swangle'
+ ANGLE: str = 'angle'
+ EGL: str = 'egl'
+ SWIFTSHADER: str = 'swiftshader'
+ VULKAN: str = 'vulkan'
+
+
+@dataclass
+class ChromiumOptions:
+ """
+ Options for configuring the Chromium browser.
+
+ Attributes:
+ ignore_certificate_errors (Optional[bool]): If True, ignores certificate errors.
+ disable_web_security (Optional[bool]): If True, disables web security.
+ gl (Optional[OpenGlRenderer]): Specifies the OpenGL renderer to use.
+ headless (Optional[bool]): If True, runs Chromium in headless mode.
+ user_agent (Optional[str]): Specifies a custom user agent.
+ enable_multi_process_on_linux (Optional[bool]):
+ If True, enables multi-process mode on Linux.
+ """
+ ignore_certificate_errors: Optional[bool] = None
+ disable_web_security: Optional[bool] = None
+ gl: Optional[OpenGlRenderer] = OpenGlRenderer.SWANGLE
+ headless: Optional[bool] = None
+ user_agent: Optional[str] = None
+ enable_multi_process_on_linux: Optional[bool] = None
+
+
+@dataclass
+class CustomCredentialsWithoutSensitiveData:
+ """
+ Represents credentials without sensitive data.
+
+ Attributes:
+ endpoint (str): The endpoint associated with the credentials.
+ """
+ endpoint: str
+
+
+@dataclass
+class CustomCredentials(CustomCredentialsWithoutSensitiveData):
+ """
+ Represents custom credentials, extending credentials without sensitive data.
+
+ Attributes:
+ access_key_id (Optional[str]): The access key ID.
+ secret_access_key (Optional[str]): The secret access key.
+ """
+ access_key_id: Optional[str] = None
+ secret_access_key: Optional[str] = None
+
+
+@dataclass
+class OutNameInputObject:
+ """
+ Defines output naming and storage options.
+
+ Attributes:
+ bucket_name (str): The name of the S3 bucket for output storage.
+ key (str): The key name within the S3 bucket.
+ s3_output_provider (Optional[CustomCredentials]):
+ Optional custom credentials for the S3 output provider.
+ """
+ bucket_name: str
+ key: str
+ s3_output_provider: Optional[CustomCredentials] = None
+
+
+@dataclass
+class PlayInBrowser:
+ """
+ Represents the behavior to play content directly in the browser.
+
+ Attributes:
+ type (str): The type of in-browser action (e.g., 'play-in-browser').
+ """
+ type: str
+
+
+@dataclass
+class Download:
+ """
+ Represents the behavior to download content.
+
+ Attributes:
+ type (str): The type of download action.
+ file_name (Optional[str]): The name of the file to be downloaded, if specified.
+ """
+ type: str
+ file_name: Optional[str]
+
+
+class DeleteAfter(Enum):
+ """
+ Enumeration for specifying the time period after which an item should be deleted.
+
+ Attributes:
+ ONE_DAY: Represents deletion after one day.
+ THREE_DAYS: Represents deletion after three days.
+ SEVEN_DAYS: Represents deletion after seven days.
+ THIRTY_DAYS: Represents deletion after thirty days.
+ """
+ ONE_DAY = '1-day'
+ THREE_DAYS = '3-days'
+ SEVEN_DAYS = '7-days'
+ THIRTY_DAYS = '30-days'
+
+
@dataclass
class RenderParams:
"""
@@ -194,196 +360,30 @@ class RenderStillOnLambdaOutput:
cloud_watch_logs: str
-class ValidStillImageFormats(Enum):
- """
- Enumeration of valid image formats for still images.
-
- Attributes:
- PNG: Represents the PNG image format.
- JPEG: Represents the JPEG image format.
- PDF: Represents the PDF format for images.
- WEBP: Represents the WEBP image format.
- """
- PNG = 'png'
- JPEG = 'jpeg'
- PDF = 'pdf'
- WEBP = 'webp'
-
-
-class Privacy(Enum):
- """
- Enumeration of privacy settings.
-
- Attributes:
- PUBLIC: Indicates a public setting.
- PRIVATE: Indicates a private setting.
- """
- PUBLIC = 'public'
- PRIVATE = 'private'
-
-
-class LogLevel(Enum):
- """
- Enumeration for different levels of logging.
-
- Attributes:
- VERBOSE: Verbose logging level.
- INFO: Informational logging level.
- WARN: Warning logging level.
- ERROR: Error logging level.
- """
- VERBOSE = 'verbose'
- INFO = 'info'
- WARN = 'warn'
- ERROR = 'error'
-
-
-class OpenGlRenderer(Enum):
- """
- Enumeration of OpenGL renderer options.
-
- Attributes:
- SWANGLE: Represents the SWANGLE OpenGL renderer.
- ANGLE: Represents the ANGLE OpenGL renderer.
- EGL: Represents the EGL OpenGL renderer.
- SWIFTSHADER: Represents the SWIFTSHADER OpenGL renderer.
- VULKAN: Represents the VULKAN OpenGL renderer.
- """
- SWANGLE = 'swangle'
- ANGLE = 'angle'
- EGL = 'egl'
- SWIFTSHADER = 'swiftshader'
- VULKAN = 'vulkan'
-
-
-@dataclass
-class ChromiumOptions:
- """
- Options for configuring the Chromium browser.
-
- Attributes:
- ignore_certificate_errors (Optional[bool]): If True, ignores certificate errors.
- disable_web_security (Optional[bool]): If True, disables web security.
- gl (Optional[OpenGlRenderer]): Specifies the OpenGL renderer to use.
- headless (Optional[bool]): If True, runs Chromium in headless mode.
- user_agent (Optional[str]): Specifies a custom user agent.
- enable_multi_process_on_linux (Optional[bool]):
- If True, enables multi-process mode on Linux.
- """
- ignore_certificate_errors: Optional[bool] = None
- disable_web_security: Optional[bool] = None
- gl: Optional[OpenGlRenderer] = None
- headless: Optional[bool] = None
- user_agent: Optional[str] = None
- enable_multi_process_on_linux: Optional[bool] = None
-
-
-@dataclass
-class CustomCredentialsWithoutSensitiveData:
- """
- Represents credentials without sensitive data.
-
- Attributes:
- endpoint (str): The endpoint associated with the credentials.
- """
- endpoint: str
-
-
-@dataclass
-class CustomCredentials(CustomCredentialsWithoutSensitiveData):
- """
- Represents custom credentials, extending credentials without sensitive data.
-
- Attributes:
- access_key_id (Optional[str]): The access key ID.
- secret_access_key (Optional[str]): The secret access key.
- """
- access_key_id: Optional[str] = None
- secret_access_key: Optional[str] = None
-
-
-@dataclass
-class OutNameInputObject:
- """
- Defines output naming and storage options.
-
- Attributes:
- bucket_name (str): The name of the S3 bucket for output storage.
- key (str): The key name within the S3 bucket.
- s3_output_provider (Optional[CustomCredentials]):
- Optional custom credentials for the S3 output provider.
- """
- bucket_name: str
- key: str
- s3_output_provider: Optional[CustomCredentials] = None
-
-
-@dataclass
-class PlayInBrowser:
- """
- Represents the behavior to play content directly in the browser.
-
- Attributes:
- type (str): The type of in-browser action (e.g., 'play-in-browser').
- """
- type: str
-
-
-@dataclass
-class Download:
- """
- Represents the behavior to download content.
-
- Attributes:
- type (str): The type of download action.
- file_name (Optional[str]): The name of the file to be downloaded, if specified.
- """
- type: str
- file_name: Optional[str]
-
-
-class DeleteAfter(Enum):
- """
- Enumeration for specifying the time period after which an item should be deleted.
-
- Attributes:
- ONE_DAY: Represents deletion after one day.
- THREE_DAYS: Represents deletion after three days.
- SEVEN_DAYS: Represents deletion after seven days.
- THIRTY_DAYS: Represents deletion after thirty days.
- """
- ONE_DAY = '1-day'
- THREE_DAYS = '3-days'
- SEVEN_DAYS = '7-days'
- THIRTY_DAYS = '30-days'
-
-
-@dataclass
-class BaseRenderParams:
- region: str
- function_name: str
- serve_url: str
-
-
@dataclass
class RenderStillParams:
"""
Parameters for video rendering.
"""
composition: str
+ serve_url: str = ""
data: Optional[List] = None
input_props: Optional[Dict[str, Any]] = None
+ private_serialized_input_props: Optional[Dict[str, Any]] = None
image_format: ValidStillImageFormats = ValidStillImageFormats.JPEG
privacy: Privacy = Privacy.PUBLIC
- max_retries: Optional[int] = None
+ max_retries: Optional[int] = 1
env_variables: Optional[Dict[str, str]] = None
- jpeg_quality: Optional[int] = None
- frame: Optional[int] = None
- log_level: Optional[LogLevel] = None
+ jpeg_quality: Optional[int] = 80
+ frame: Optional[int] = 0
+ attempt: Optional[int] = 1
+ log_level: Optional[LogLevel] = LogLevel.INFO
out_name: Optional[Union[str, OutNameInputObject]] = None
- timeout_in_milliseconds: Optional[int] = None
- chromium_options: Optional[ChromiumOptions] = None
- scale: Optional[float] = None
+ timeout_in_milliseconds: Optional[int] = 30000
+ chromium_options: Optional[ChromiumOptions] = field(
+ default_factory=lambda: ChromiumOptions()
+ )
+ scale: Optional[float] = 1
download_behavior: Dict = field(default_factory=lambda: {
'type': 'play-in-browser'})
force_width: Optional[int] = None
@@ -411,21 +411,20 @@ class RenderStillParams:
Dict: A dictionary containing all the serialized parameters of the object.
"""
parameters = {
-
'composition': self.composition,
- 'inputProps': self.input_props or {}, # Provide a default empty dict if None
+ 'inputProps': self.private_serialized_input_props or {},
'imageFormat': self.image_format,
'privacy': self.privacy,
- 'type': 'start'
- }
- optional_fields = {
+ 'serveUrl': self.serve_url,
+ 'version': VERSION,
+ 'type': 'still',
+ 'timeoutInMilliseconds': self.timeout_in_milliseconds,
'maxRetries': self.max_retries,
'envVariables': self.env_variables,
'jpegQuality': self.jpeg_quality,
'frame': self.frame,
'logLevel': self.log_level,
'outName': self.out_name,
- 'timeoutInMilliseconds': self.timeout_in_milliseconds,
'chromiumOptions': self.chromium_options,
'scale': self.scale,
'downloadBehavior': self.download_behavior or {'type': 'play-in-browser'},
@@ -434,13 +433,9 @@ class RenderStillParams:
'forceBucketName': self.force_bucket_name,
'dumpBrowserLogs': self.dump_browser_logs,
'deleteAfter': self.delete_after,
+ 'attempt': self.attempt
}
- # Add optional fields only if they are not None
- for key, value in optional_fields.items():
- if value is not None:
- parameters[key] = value
-
return parameters
diff --git a/packages/lambda-python/remotion_lambda/remotionclient.py b/packages/lambda-python/remotion_lambda/remotionclient.py
index 10aef61612..19964e3c60 100644
--- a/packages/lambda-python/remotion_lambda/remotionclient.py
+++ b/packages/lambda-python/remotion_lambda/remotionclient.py
@@ -1,11 +1,13 @@
# pylint: disable=too-few-public-methods, missing-module-docstring, broad-exception-caught
+from dataclasses import asdict, is_dataclass
import json
from math import ceil
+from typing import Union, Literal
+from enum import Enum
+import boto3
from .models import (RenderParams, RenderProgress,
RenderResponse, RenderProgressParams,
RenderStillParams)
-from typing import Union, Literal
-import boto3
class RemotionClient:
@@ -74,25 +76,65 @@ class RemotionClient:
return boto3.client('lambda', region_name=self.region)
+ def _find_json_objects(self, input_string):
+ """Finds and returns a list of complete JSON object strings."""
+ objects = []
+ depth = 0
+ start_index = 0
+
+ for i, char in enumerate(input_string):
+ if char == '{':
+ if depth == 0:
+ start_index = i
+ depth += 1
+ elif char == '}':
+ depth -= 1
+ if depth == 0:
+ objects.append(input_string[start_index:i + 1])
+
+ return objects
+
+ def _parse_stream(self, stream):
+ """Parses a stream of concatenated JSON objects."""
+ json_objects = self._find_json_objects(stream)
+ parsed_objects = [json.loads(obj) for obj in json_objects]
+ return parsed_objects
+
def _invoke_lambda(self, function_name, payload):
+
client = self._create_lambda_client()
response = client.invoke(
- FunctionName=function_name, Payload=payload)
-
+ FunctionName=function_name, Payload=payload, )
result = response['Payload'].read().decode('utf-8')
-
- decoded_result = json.loads(result)
+ print("\ndecoded_result")
+ decoded_result = (self._parse_stream(result)[-1])
+ print(decoded_result)
+ print("\n")
if 'errorMessage' in decoded_result:
raise ValueError(decoded_result['errorMessage'])
if 'type' in decoded_result and decoded_result['type'] == 'error':
raise ValueError(decoded_result['message'])
- if not 'type' in decoded_result or decoded_result['type'] != 'success':
+ if (not 'type' in decoded_result or decoded_result['type'] != 'success'):
raise ValueError(result)
return decoded_result
- def construct_render_request(self, render_params: RenderParams, render_type: Union[Literal["video-or-audio"], Literal["still"]]) -> str:
+ def _custom_serializer(self, obj):
+ """A custom JSON serializer that handles enums and objects."""
+
+ if isinstance(obj, Enum):
+ return obj.value if hasattr(obj, 'value') else obj.name
+ elif hasattr(obj, '__dict__'):
+ return obj.__dict__
+ elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)):
+ return list(obj)
+ else:
+ return asdict(obj)
+
+ def construct_render_request(self, render_params: Union[RenderParams, RenderStillParams],
+ render_type:
+ Union[Literal["video-or-audio"], Literal["still"]]) -> str:
"""
Construct a render request in JSON format.
@@ -105,11 +147,12 @@ class RemotionClient:
render_params.serve_url = self.serve_url
render_params.region = self.region
render_params.function_name = self.function_name
+ render_params.type = render_type
render_params.private_serialized_input_props = self._serialize_input_props(
input_props=render_params.data,
render_type=render_type
)
- return json.dumps(render_params.serialize_params(), default=lambda x: x.value)
+ return json.dumps(render_params.serialize_params(), default=self._custom_serializer)
def construct_render_progress_request(self, render_id: str, bucket_name: str) -> str:
"""