Skip to content

🎥 Make videos programmatically with React

make lambda types more strict

Jonny Burger2bce6ed2df3f
diff --git a/packages/cli/src/get-cli-options.ts b/packages/cli/src/get-cli-options.ts
index 04eac2d6c6..cd0cffa504 100644
--- a/packages/cli/src/get-cli-options.ts
+++ b/packages/cli/src/get-cli-options.ts
@@ -1,15 +1,9 @@
import type {ChromiumOptions} from '@remotion/renderer';
-import { RenderInternals} from '@remotion/renderer';
+import {RenderInternals} from '@remotion/renderer';
import fs from 'fs';
import path from 'path';
-import type {
- BrowserExecutable,
- Codec,
- FrameRange,
- PixelFormat} from 'remotion';
-import {
- Internals
-} from 'remotion';
+import type {BrowserExecutable, Codec, FrameRange, PixelFormat} from 'remotion';
+import {Internals} from 'remotion';
import {getEnvironmentVariables} from './get-env';
import {getOutputFilename} from './get-filename';
import {getInputProps} from './get-input-props';
@@ -39,7 +33,6 @@ const getFinalCodec = async (options: {isLambda: boolean}) => {
? null
: RenderInternals.getExtensionOfFilename(getUserPassedOutputLocation()),
emitWarning: true,
- isLambda: options.isLambda,
});
const ffmpegExecutable = Internals.getCustomFfmpegExecutable();
if (
diff --git a/packages/core/src/config/codec.ts b/packages/core/src/config/codec.ts
index cf6b572496..7be3e0c3c6 100644
--- a/packages/core/src/config/codec.ts
+++ b/packages/core/src/config/codec.ts
@@ -22,18 +22,15 @@ export const getOutputCodecOrUndefined = (): CodecOrUndefined => {
};
export const DEFAULT_CODEC: Codec = 'h264';
-const DEFAULT_LAMBDA_CODEC: Codec = 'h264-mkv';
export const getFinalOutputCodec = ({
codec: inputCodec,
fileExtension,
emitWarning,
- isLambda,
}: {
codec: CodecOrUndefined;
fileExtension: string | null;
emitWarning: boolean;
- isLambda: boolean;
}): Codec => {
if (inputCodec === undefined && fileExtension === 'webm') {
if (emitWarning) {
@@ -115,7 +112,7 @@ export const getFinalOutputCodec = ({
return 'h264-mkv';
}
- return inputCodec ?? (isLambda ? DEFAULT_LAMBDA_CODEC : DEFAULT_CODEC);
+ return inputCodec ?? DEFAULT_CODEC;
};
export const setOutputFormat = (newLegacyFormat: LegacyFormat) => {
diff --git a/packages/docs/docs/encoding.md b/packages/docs/docs/encoding.md
index 77552d0e6f..38ed5714c9 100644
--- a/packages/docs/docs/encoding.md
+++ b/packages/docs/docs/encoding.md
@@ -7,7 +7,7 @@ Backed by [FFMPEG](https://ffmpeg.org/), Remotion allows you to configure a vari
## Choosing a codec
-Remotion supports 5 video codecs: `h264` (_default_), `h265`, `vp8`, `vp9`, `prores`. The first two produce an output that will be an MP4, the last two are for generating WebM videos. While H264 will work well in most cases, sometimes it's worth going for a different codec. Refer to the table below to see the advantages and drawbacks of each codec.
+Remotion supports 5 video codecs: `h264` (_default_), `h265`, `vp8`, `vp9` and `prores`. While H264 will work well in most cases, sometimes it's worth going for a different codec. Refer to the table below to see the advantages and drawbacks of each codec.
<table>
<tr>
diff --git a/packages/docs/docs/lambda/cli/render.md b/packages/docs/docs/lambda/cli/render.md
index 11a32d557b..43a17bebb8 100644
--- a/packages/docs/docs/lambda/cli/render.md
+++ b/packages/docs/docs/lambda/cli/render.md
@@ -102,7 +102,7 @@ The `framesPerLambda` parameter cannot result in more than 200 functions being s
### `--codec`
-[`h264` or `h265` or `png` or `vp8` or `vp9` or `mp3` or `aac` or `wav` or `prores` or `h264-mkv`](/docs/config#setcodec). If you don't supply `--codec`, it will use `h264-mkv`.
+[`h264` or `h265` or `png` or `vp8` or `mp3` or `aac` or `wav` or `prores`](/docs/config#setcodec). If you don't supply `--codec`, it will use `h264`.
### `--prores-profile`
diff --git a/packages/docs/docs/lambda/rendermediaonlambda.md b/packages/docs/docs/lambda/rendermediaonlambda.md
index dbfe3f99e6..530f79dfb8 100644
--- a/packages/docs/docs/lambda/rendermediaonlambda.md
+++ b/packages/docs/docs/lambda/rendermediaonlambda.md
@@ -23,7 +23,7 @@ const { bucketName, renderId } = await renderMediaOnLambda({
serveUrl:
"https://remotionlambda-qg35eyp1s1.s3.eu-central-1.amazonaws.com/sites/bf2jrbfkw",
inputProps: {},
- codec: "h264-mkv",
+ codec: "h264",
imageFormat: "jpeg",
maxRetries: 1,
privacy: "public",
@@ -72,7 +72,9 @@ React props that are passed to your composition. You define the shape of the pro
### `codec`
-Which codec should be used to encode the video. At the moment the only supported video codec is `h264-mkv`, which will return a MP4 encoded using H.264, but where the chunks are encoded in the Matruska container. The reason Lambda has a special value is to allow audio to be stitched together seamlessly.
+Which codec should be used to encode the video.
+
+Video codecs `h264` and `vp8` are supported.
Audio codecs `mp3`, `aac` and `wav` are also supported.
diff --git a/packages/docs/docs/lambda/setup.md b/packages/docs/docs/lambda/setup.md
index 0d4b8d3d51..0fe9748d88 100644
--- a/packages/docs/docs/lambda/setup.md
+++ b/packages/docs/docs/lambda/setup.md
@@ -287,7 +287,7 @@ const { renderId, bucketName } = await renderMediaOnLambda({
serveUrl: url,
composition: "HelloWorld",
inputProps: {},
- codec: "h264-mkv",
+ codec: "h264",
imageFormat: "jpeg",
maxRetries: 1,
framesPerLambda: 20,
@@ -320,7 +320,7 @@ const { renderId, bucketName } = await renderMediaOnLambda({
serveUrl: url,
composition: "HelloWorld",
inputProps: {},
- codec: "h264-mkv",
+ codec: "h264",
imageFormat: "jpeg",
maxRetries: 1,
framesPerLambda: 20,
diff --git a/packages/lambda/src/api/render-media-on-lambda.ts b/packages/lambda/src/api/render-media-on-lambda.ts
index d5cb55d96a..7efef9b2d4 100644
--- a/packages/lambda/src/api/render-media-on-lambda.ts
+++ b/packages/lambda/src/api/render-media-on-lambda.ts
@@ -4,16 +4,17 @@ import type {
ImageFormat,
LogLevel,
PixelFormat,
- ProResProfile} from 'remotion';
-import {
- Internals
+ ProResProfile,
} from 'remotion';
+import {Internals} from 'remotion';
import type {AwsRegion} from '../pricing/aws-regions';
import {callLambda} from '../shared/call-lambda';
-import type { Privacy} from '../shared/constants';
+import type {Privacy} from '../shared/constants';
import {LambdaRoutines} from '../shared/constants';
import {convertToServeUrl} from '../shared/convert-to-serve-url';
import {validateFramesPerLambda} from '../shared/validate-frames-per-lambda';
+import type {LambdaCodec} from '../shared/validate-lambda-codec';
+import {validateLambdaCodec} from '../shared/validate-lambda-codec';
import {validateServeUrl} from '../shared/validate-serveurl';
export type RenderMediaOnLambdaInput = {
@@ -22,7 +23,7 @@ export type RenderMediaOnLambdaInput = {
serveUrl: string;
composition: string;
inputProps: unknown;
- codec: 'h264-mkv' | 'mp3' | 'aac' | 'wav';
+ codec: LambdaCodec;
imageFormat: ImageFormat;
crf?: number | undefined;
envVariables?: Record<string, string>;
@@ -87,6 +88,7 @@ export const renderMediaOnLambda = async ({
chromiumOptions,
scale,
}: RenderMediaOnLambdaInput): Promise<RenderMediaOnLambdaOutput> => {
+ const actualCodec = validateLambdaCodec(codec);
validateServeUrl(serveUrl);
validateFramesPerLambda(framesPerLambda ?? null);
const realServeUrl = await convertToServeUrl(serveUrl, region);
@@ -98,7 +100,7 @@ export const renderMediaOnLambda = async ({
composition,
serveUrl: realServeUrl,
inputProps,
- codec,
+ codec: actualCodec,
imageFormat,
crf,
envVariables,
diff --git a/packages/lambda/src/cli/commands/render/render.ts b/packages/lambda/src/cli/commands/render/render.ts
index 211c74fa5e..572cd425c6 100644
--- a/packages/lambda/src/cli/commands/render/render.ts
+++ b/packages/lambda/src/cli/commands/render/render.ts
@@ -11,6 +11,7 @@ import {
} from '../../../shared/constants';
import {sleep} from '../../../shared/sleep';
import {validateFramesPerLambda} from '../../../shared/validate-frames-per-lambda';
+import type {LambdaCodec} from '../../../shared/validate-lambda-codec';
import {validatePrivacy} from '../../../shared/validate-privacy';
import {validateMaxRetries} from '../../../shared/validate-retries';
import {parsedLambdaCli} from '../../args';
@@ -85,7 +86,7 @@ export const renderCommand = async (args: string[]) => {
functionName,
serveUrl,
inputProps,
- codec: codec as 'h264-mkv' | 'mp3' | 'aac' | 'wav',
+ codec: codec as LambdaCodec,
imageFormat,
crf: crf ?? undefined,
envVariables,
diff --git a/packages/lambda/src/functions/helpers/concat-videos.ts b/packages/lambda/src/functions/helpers/concat-videos.ts
index a0751b088d..dda9d9157c 100644
--- a/packages/lambda/src/functions/helpers/concat-videos.ts
+++ b/packages/lambda/src/functions/helpers/concat-videos.ts
@@ -16,6 +16,7 @@ import {
REMOTION_CONCATED_TOKEN,
REMOTION_FILELIST_TOKEN,
} from '../../shared/constants';
+import type {LambdaCodec} from '../../shared/validate-lambda-codec';
import {lambdaLs, lambdaReadFile} from './io';
import {timer} from './timer';
@@ -170,7 +171,7 @@ export const concatVideosS3 = async ({
numberOfFrames: number;
renderId: string;
region: AwsRegion;
- codec: Codec;
+ codec: LambdaCodec;
expectedBucketOwner: string;
fps: number;
}) => {
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index 3860974df6..db8fc0c56e 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -6,7 +6,8 @@ import {getLambdaClient} from '../shared/aws-clients';
import type {
EncodingProgress,
LambdaPayload,
- RenderMetadata} from '../shared/constants';
+ RenderMetadata,
+} from '../shared/constants';
import {
CURRENT_VERSION,
encodingProgressKey,
@@ -154,7 +155,7 @@ const innerLaunchHandler = async (params: LambdaPayload, options: Options) => {
inputProps: params.inputProps,
renderId: params.renderId,
imageFormat: params.imageFormat,
- codec: params.codec,
+ codec: params.codec === 'h264' ? 'h264-mkv' : params.codec,
crf: params.crf,
envVariables: params.envVariables,
pixelFormat: params.pixelFormat,
diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.ts
index a6e9ad0b01..6085efc6f5 100644
--- a/packages/lambda/src/shared/constants.ts
+++ b/packages/lambda/src/shared/constants.ts
@@ -13,6 +13,7 @@ import type {EnhancedErrorInfo} from '../functions/helpers/write-lambda-error';
import type {AwsRegion} from '../pricing/aws-regions';
import type {ExpensiveChunk} from './get-most-expensive-chunks';
import type {LambdaArchitecture} from './validate-architecture';
+import type {LambdaCodec} from './validate-lambda-codec';
export const MIN_MEMORY = 512;
export const MAX_MEMORY = 10240;
@@ -179,7 +180,7 @@ export type LambdaPayloads = {
composition: string;
framesPerLambda: number | null;
inputProps: unknown;
- codec: Codec;
+ codec: LambdaCodec;
imageFormat: ImageFormat;
crf: number | undefined;
envVariables: Record<string, string> | undefined;
@@ -204,7 +205,7 @@ export type LambdaPayloads = {
inputProps: unknown;
renderId: string;
imageFormat: ImageFormat;
- codec: Codec;
+ codec: LambdaCodec;
crf: number | undefined;
envVariables: Record<string, string> | undefined;
pixelFormat: PixelFormat | undefined;
@@ -239,7 +240,7 @@ export type LambdaPayloads = {
inputProps: unknown;
renderId: string;
imageFormat: ImageFormat;
- codec: Codec;
+ codec: Exclude<Codec, 'h264'>;
crf: number | undefined;
proResProfile: ProResProfile | undefined;
pixelFormat: PixelFormat | undefined;
diff --git a/packages/lambda/src/shared/validate-lambda-codec.ts b/packages/lambda/src/shared/validate-lambda-codec.ts
new file mode 100644
index 0000000000..dac715b53a
--- /dev/null
+++ b/packages/lambda/src/shared/validate-lambda-codec.ts
@@ -0,0 +1,28 @@
+const lambdaCodecs = ['h264-mkv', 'h264', 'vp8', 'mp3', 'aac', 'wav'] as const;
+
+export type LambdaCodec = typeof lambdaCodecs[number];
+
+export const validateLambdaCodec = (codec: unknown): LambdaCodec => {
+ if (typeof codec !== 'string') {
+ throw new TypeError('"codec" must be a string ');
+ }
+
+ if (!(lambdaCodecs as readonly string[]).includes(codec)) {
+ throw new TypeError(
+ "'" +
+ codec +
+ "' is not a valid codec for Lambda. The following values are supported: " +
+ lambdaCodecs.join(', ')
+ );
+ }
+
+ if ((codec as LambdaCodec) === 'h264-mkv') {
+ console.warn(
+ 'The "h264-mkv" codec for renderMediaOnLambda() is deprecated - it\'s now just "h264".'
+ );
+
+ return 'h264';
+ }
+
+ return codec as LambdaCodec;
+};
diff --git a/packages/lambda/src/test/integration/renders/old-version.test.ts b/packages/lambda/src/test/integration/renders/old-version.test.ts
index 846feb7b49..f85e39edd9 100644
--- a/packages/lambda/src/test/integration/renders/old-version.test.ts
+++ b/packages/lambda/src/test/integration/renders/old-version.test.ts
@@ -31,7 +31,7 @@ test('Should be able to render to another bucket', async () => {
type: LambdaRoutines.start,
serveUrl: 'https://competent-mccarthy-56f7c9.netlify.app/',
chromiumOptions: {},
- codec: 'h264-mkv',
+ codec: 'h264',
composition: 'react-svg',
crf: 9,
envVariables: {},
diff --git a/packages/lambda/src/test/integration/renders/other-bucket.test.ts b/packages/lambda/src/test/integration/renders/other-bucket.test.ts
index 37c7efbe90..eb39f9c5f2 100644
--- a/packages/lambda/src/test/integration/renders/other-bucket.test.ts
+++ b/packages/lambda/src/test/integration/renders/other-bucket.test.ts
@@ -33,7 +33,7 @@ test('Should be able to render to another bucket', async () => {
serveUrl:
'https://6297949544e290044cecb257--cute-kitsune-214ea5.netlify.app/',
chromiumOptions: {},
- codec: 'h264-mkv',
+ codec: 'h264',
composition: 'react-svg',
crf: 9,
envVariables: {},
diff --git a/packages/lambda/src/test/integration/renders/silent-audio.test.ts b/packages/lambda/src/test/integration/renders/silent-audio.test.ts
index 856fd937c1..73e0388aea 100644
--- a/packages/lambda/src/test/integration/renders/silent-audio.test.ts
+++ b/packages/lambda/src/test/integration/renders/silent-audio.test.ts
@@ -33,7 +33,7 @@ test('Should add silent audio if there is no audio', async () => {
serveUrl:
'https://6297949544e290044cecb257--cute-kitsune-214ea5.netlify.app/',
chromiumOptions: {},
- codec: 'h264-mkv',
+ codec: 'h264',
composition: 'react-svg',
crf: 9,
envVariables: {},