Skip to content

🎥 Make videos programmatically with React

serialized props

Jonny Burger1257e281c40c
diff --git a/packages/lambda/src/api/get-compositions-on-lambda.ts b/packages/lambda/src/api/get-compositions-on-lambda.ts
index b32452c4b5..f2a2162417 100644
--- a/packages/lambda/src/api/get-compositions-on-lambda.ts
+++ b/packages/lambda/src/api/get-compositions-on-lambda.ts
@@ -4,7 +4,11 @@ import {VERSION} from 'remotion/version';
import type {AwsRegion} from '../client';
import {LambdaRoutines} from '../defaults';
import {callLambda} from '../shared/call-lambda';
-import {serializeInputProps} from '../shared/serialize-props';
+import {
+ getNeedsToUpload,
+ serializeInputProps,
+ serializeOrThrow,
+} from '../shared/serialize-props';
export type GetCompositionsOnLambdaInput = {
chromiumOptions?: ChromiumOptions;
@@ -49,12 +53,14 @@ export const getCompositionsOnLambda = async ({
forceBucketName: bucketName,
dumpBrowserLogs,
}: GetCompositionsOnLambdaInput): Promise<GetCompositionsOnLambdaOutput> => {
+ const stringifiedInputProps = serializeOrThrow(inputProps, 'input-props');
+
const serializedInputProps = await serializeInputProps({
- inputProps,
+ stringifiedInputProps,
region,
- type: 'still',
userSpecifiedBucketName: bucketName ?? null,
propsType: 'input-props',
+ needsToUpload: getNeedsToUpload('video-or-audio', stringifiedInputProps),
});
try {
diff --git a/packages/lambda/src/api/make-lambda-payload.ts b/packages/lambda/src/api/make-lambda-payload.ts
index 74b4a6a411..791a1c81f6 100644
--- a/packages/lambda/src/api/make-lambda-payload.ts
+++ b/packages/lambda/src/api/make-lambda-payload.ts
@@ -1,7 +1,11 @@
import {VERSION} from 'remotion/version';
import type {LambdaStartPayload, LambdaStatusPayload} from '../defaults';
import {LambdaRoutines} from '../defaults';
-import {serializeInputProps} from '../shared/serialize-props';
+import {
+ getNeedsToUpload,
+ serializeInputProps,
+ serializeOrThrow,
+} from '../shared/serialize-props';
import {validateDownloadBehavior} from '../shared/validate-download-behavior';
import {validateFramesPerLambda} from '../shared/validate-frames-per-lambda';
import {validateLambdaCodec} from '../shared/validate-lambda-codec';
@@ -61,10 +65,15 @@ export const makeLambdaRenderMediaPayload = async ({
});
validateDownloadBehavior(downloadBehavior);
- const serializedInputProps = await serializeInputProps({
- inputProps: inputProps ?? {},
+ const stringifiedInputProps = serializeOrThrow(
+ inputProps ?? {},
+ 'input-props'
+ );
+
+ const serialized = await serializeInputProps({
+ stringifiedInputProps,
region,
- type: 'video-or-audio',
+ needsToUpload: getNeedsToUpload('video-or-audio', stringifiedInputProps),
userSpecifiedBucketName: bucketName ?? null,
propsType: 'input-props',
});
@@ -73,7 +82,7 @@ export const makeLambdaRenderMediaPayload = async ({
framesPerLambda: framesPerLambda ?? null,
composition,
serveUrl,
- inputProps: serializedInputProps,
+ inputProps: serialized,
codec: actualCodec,
imageFormat: imageFormat ?? 'jpeg',
crf,
diff --git a/packages/lambda/src/api/render-still-on-lambda.ts b/packages/lambda/src/api/render-still-on-lambda.ts
index 2bcf76942c..fbb0c0578d 100644
--- a/packages/lambda/src/api/render-still-on-lambda.ts
+++ b/packages/lambda/src/api/render-still-on-lambda.ts
@@ -10,7 +10,11 @@ import type {CostsInfo, OutNameInput, Privacy} from '../shared/constants';
import {DEFAULT_MAX_RETRIES, LambdaRoutines} from '../shared/constants';
import type {DownloadBehavior} from '../shared/content-disposition-header';
import {getCloudwatchStreamUrl} from '../shared/get-aws-urls';
-import {serializeInputProps} from '../shared/serialize-props';
+import {
+ getNeedsToUpload,
+ serializeInputProps,
+ serializeOrThrow,
+} from '../shared/serialize-props';
export type RenderStillOnLambdaInput = {
region: AwsRegion;
@@ -99,10 +103,12 @@ export const renderStillOnLambda = async ({
);
}
+ const stringifiedInputProps = serializeOrThrow(inputProps, 'input-props');
+
const serializedInputProps = await serializeInputProps({
- inputProps,
+ stringifiedInputProps,
region,
- type: 'still',
+ needsToUpload: getNeedsToUpload('still', stringifiedInputProps),
userSpecifiedBucketName: forceBucketName ?? null,
propsType: 'input-props',
});
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index 49cdfbc23d..f864562e96 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -46,7 +46,9 @@ import {
import {writePostRenderData} from './helpers/write-post-render-data';
import {
deserializeInputProps,
+ getNeedsToUpload,
serializeInputProps,
+ serializeOrThrow,
} from '../shared/serialize-props';
type Options = {
@@ -247,20 +249,31 @@ const innerLaunchHandler = async (params: LambdaPayload, options: Options) => {
const reqSend = timer('sending off requests');
+ const serializedResolved = serializeOrThrow(comp.props, 'resolved-props');
+ const serializedDefault = serializeOrThrow(
+ comp.defaultProps,
+ 'default-props'
+ );
+
+ const needsToUpload = getNeedsToUpload(
+ 'video-or-audio',
+ serializedResolved + serializedDefault
+ );
+
const [serializedResolvedProps, serializedDefaultProps] = await Promise.all([
serializeInputProps({
propsType: 'resolved-props',
region: getCurrentRegionInFunction(),
- inputProps: comp.props,
- type: 'video-or-audio',
+ stringifiedInputProps: serializedResolved,
userSpecifiedBucketName: params.bucketName,
+ needsToUpload,
}),
serializeInputProps({
propsType: 'default-props',
region: getCurrentRegionInFunction(),
- inputProps: comp.defaultProps,
- type: 'video-or-audio',
+ stringifiedInputProps: serializedDefault,
userSpecifiedBucketName: params.bucketName,
+ needsToUpload,
}),
]);
diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.ts
index 5b4ea7923b..be7f7136da 100644
--- a/packages/lambda/src/shared/constants.ts
+++ b/packages/lambda/src/shared/constants.ts
@@ -218,7 +218,7 @@ export type SerializedInputProps =
}
| {
type: 'payload';
- payload: unknown;
+ payload: string;
};
export type LambdaStartPayload = {
diff --git a/packages/lambda/src/shared/serialize-props.ts b/packages/lambda/src/shared/serialize-props.ts
index a1ac557e0b..32d498ea8b 100644
--- a/packages/lambda/src/shared/serialize-props.ts
+++ b/packages/lambda/src/shared/serialize-props.ts
@@ -1,74 +1,91 @@
import {getOrCreateBucket} from '../api/get-or-create-bucket';
import type {AwsRegion} from '../client';
import {lambdaReadFile, lambdaWriteFile} from '../functions/helpers/io';
-import {defaultPropsKey, inputPropsKey, resolvedPropsKey, type SerializedInputProps} from './constants';
+import type {SerializedInputProps} from './constants';
+import {defaultPropsKey, inputPropsKey, resolvedPropsKey} from './constants';
import {randomHash} from './random-hash';
import {streamToString} from './stream-to-string';
type PropsType = 'input-props' | 'resolved-props' | 'default-props';
+export const serializeOrThrow = (
+ inputProps: Record<string, unknown>,
+ propsType: PropsType
+) => {
+ try {
+ const payload = JSON.stringify(inputProps);
+ return payload;
+ } catch (err) {
+ throw new Error(
+ `Error serializing ${propsType}. Check it has no circular references or reduce the size if the object is big.`
+ );
+ }
+};
+
+export const getNeedsToUpload = (
+ type: 'still' | 'video-or-audio',
+ stringifiedInputProps: string
+) => {
+ const MAX_INLINE_PAYLOAD_SIZE = type === 'still' ? 5000000 : 200000;
+ if (stringifiedInputProps.length > MAX_INLINE_PAYLOAD_SIZE) {
+ console.warn(
+ `Warning: The props are over ${Math.round(
+ MAX_INLINE_PAYLOAD_SIZE / 1000
+ )}KB (${Math.ceil(
+ stringifiedInputProps.length / 1024
+ )}KB) in size. Uploading them to S3 to circumvent AWS Lambda payload size, which may lead to slowdown.`
+ );
+ return true;
+ }
+
+ return false;
+};
+
export const serializeInputProps = async ({
- inputProps,
+ stringifiedInputProps,
region,
- type,
userSpecifiedBucketName,
- propsType
+ propsType,
+ needsToUpload,
}: {
- inputProps: Record<string, unknown>;
+ stringifiedInputProps: string;
region: AwsRegion;
- type: 'still' | 'video-or-audio';
userSpecifiedBucketName: string | null;
propsType: PropsType;
+ needsToUpload: boolean;
}): Promise<SerializedInputProps> => {
- try {
- const payload = JSON.stringify(inputProps);
- const hash = randomHash();
-
- const MAX_INLINE_PAYLOAD_SIZE = type === 'still' ? 5000000 : 200000;
-
- // TODO: It should take the COMBINED size of default props and resolved props
- if (payload.length > MAX_INLINE_PAYLOAD_SIZE) {
- console.warn(
- `Warning: inputProps are over ${Math.round(
- MAX_INLINE_PAYLOAD_SIZE / 1000
- )}KB (${Math.ceil(
- payload.length / 1024
- )}KB) in size. Uploading them to S3 to circumvent AWS Lambda payload size.`
- );
- const bucketName =
- userSpecifiedBucketName ??
- (
- await getOrCreateBucket({
- region,
- })
- ).bucketName;
-
- await lambdaWriteFile({
- body: payload,
- bucketName,
- region,
- customCredentials: null,
- downloadBehavior: null,
- expectedBucketOwner: null,
- key: makeKey(propsType, hash),
- privacy: 'public',
- });
-
- return {
- type: 'bucket-url',
- hash,
- };
- }
+ const hash = randomHash();
+
+ if (needsToUpload) {
+ const bucketName =
+ userSpecifiedBucketName ??
+ (
+ await getOrCreateBucket({
+ region,
+ })
+ ).bucketName;
+
+ await lambdaWriteFile({
+ body: stringifiedInputProps,
+ bucketName,
+ region,
+ customCredentials: null,
+ downloadBehavior: null,
+ expectedBucketOwner: null,
+ key: makeKey(propsType, hash),
+ privacy: 'public',
+ });
return {
- type: 'payload',
- payload,
+ type: 'bucket-url',
+ hash,
};
- } catch (err) {
- throw new Error(
- `Error serializing ${propsType}. Check it has no circular references or reduce the size if the object is big.`
- );
}
+
+ return {
+ type: 'payload',
+ payload: stringifiedInputProps,
+ };
};
export const deserializeInputProps = async ({
@@ -76,7 +93,7 @@ export const deserializeInputProps = async ({
region,
bucketName,
expectedBucketOwner,
- propsType
+ propsType,
}: {
serialized: SerializedInputProps;
region: AwsRegion;
@@ -85,7 +102,7 @@ export const deserializeInputProps = async ({
propsType: PropsType;
}): Promise<Record<string, unknown>> => {
if (serialized.type === 'payload') {
- return JSON.parse(serialized.payload as string);
+ return JSON.parse(serialized.payload);
}
try {
@@ -119,5 +136,4 @@ const makeKey = (type: PropsType, hash: string): string => {
}
return resolvedPropsKey(hash);
-
-}
+};