🎥 Make videos programmatically with React
import fs from 'node:fs';
import {join} from 'node:path';
import {
ServerlessRoutines,
type ServerlessPayload,
} from '@remotion/lambda-client';
import {
makeCancelSignal,
renderMedia,
RenderInternals,
} from '@remotion/renderer';
import type {
Bitrate,
ChromiumOptions,
ResolvedFrameRange,
} from '@remotion/renderer';
import type {VideoConfig} from 'remotion/no-react';
import type {OnArtifactFromRenderer} from './artifact-registry';
import {startCancellationPolling} from './cancellation-polling';
import type {LaunchedBrowser} from './get-browser-instance';
import {getChromiumPath} from './helpers/get-chromium-path';
import {getCurrentRegionInFunction} from './helpers/get-current-region';
import {onDownloadsHelper} from './on-downloads-helpers';
import type {OverallProgressHelper} from './overall-render-progress';
export const renderWithSingleFunction = async ({
params,
composition,
serializedInputPropsWithCustomSchema,
frameRange,
browserInstance,
chromiumOptions,
overallProgress,
onArtifact,
}: {
params: ServerlessPayload;
composition: VideoConfig;
serializedInputPropsWithCustomSchema: string;
frameRange: ResolvedFrameRange;
browserInstance: LaunchedBrowser['instance'];
chromiumOptions: ChromiumOptions;
overallProgress: OverallProgressHelper;
onArtifact: OnArtifactFromRenderer;
}): Promise<{
outputFile: string;
separateAudioFile: string | null;
cleanup: () => Promise<void>;
}> => {
if (params.type !== ServerlessRoutines.launch || params.codec === null) {
throw new Error('Expected launch type');
}
const outputDirectory = RenderInternals.tmpDir('remotion-direct-render-');
const separateAudioFilename =
typeof params.separateAudioTo === 'string'
? params.separateAudioTo
: (params.separateAudioTo?.key ?? null);
const separateAudioFile =
separateAudioFilename === null
? null
: join(
outputDirectory,
`audio.${RenderInternals.getExtensionOfFilename(separateAudioFilename)?.toLowerCase()}`,
);
const outputFile = join(
outputDirectory,
`output.${RenderInternals.getFileExtensionFromCodec(
params.codec,
separateAudioFile === null ? params.audioCodec : null,
)}`,
);
const cleanup = () =>
fs.promises.rm(outputDirectory, {recursive: true, force: true});
const frameCount = RenderInternals.getFramesToRender(
frameRange,
params.everyNthFrame,
).length;
const startedAt = Date.now();
const {cancel, cancelSignal} = makeCancelSignal();
const stopCancellationPolling = params.enableCancellation
? startCancellationPolling({
bucketName: params.bucketName,
renderId: params.renderId,
region: getCurrentRegionInFunction(),
forcePathStyle: params.forcePathStyle,
intervalInMilliseconds: 1000,
logLevel: params.logLevel,
onCancelled: cancel,
})
: () => undefined;
try {
await renderMedia({
composition,
serveUrl: params.serveUrl,
codec: params.codec,
outputLocation: outputFile,
separateAudioTo: separateAudioFile,
inputProps: JSON.parse(serializedInputPropsWithCustomSchema) as Record<
string,
unknown
>,
imageFormat: params.imageFormat,
frameRange,
concurrency: params.concurrencyPerFunction,
puppeteerInstance: browserInstance,
jpegQuality: params.jpegQuality ?? RenderInternals.DEFAULT_JPEG_QUALITY,
envVariables: params.envVariables ?? {},
logLevel: params.logLevel,
crf: params.crf,
pixelFormat: params.pixelFormat ?? undefined,
proResProfile: params.proResProfile ?? undefined,
x264Preset: params.x264Preset,
gopSize: params.gopSize ?? null,
onDownload: onDownloadsHelper(params.logLevel),
overwrite: true,
chromiumOptions,
scale: params.scale,
timeoutInMilliseconds: params.timeoutInMilliseconds,
everyNthFrame: params.everyNthFrame,
numberOfGifLoops: params.numberOfGifLoops,
muted: params.muted,
enforceAudioTrack: true,
audioBitrate: params.audioBitrate as Bitrate | null,
videoBitrate: params.videoBitrate as Bitrate | null,
encodingBufferSize: params.encodingBufferSize as Bitrate | null,
encodingMaxRate: params.encodingMaxRate as Bitrate | null,
audioCodec: params.audioCodec,
preferLossless: params.preferLossless,
browserExecutable: getChromiumPath(),
cancelSignal: params.enableCancellation ? cancelSignal : undefined,
disallowParallelEncoding: false,
disableSharedMemoryCapture: params.disableSharedMemoryCapture,
offthreadVideoCacheSizeInBytes: params.offthreadVideoCacheSizeInBytes,
colorSpace: params.colorSpace ?? undefined,
repro: false,
binariesDirectory: null,
onBrowserDownload: () => {
throw new Error('Should not download a browser in Lambda');
},
onArtifact: (artifact) => {
const registration = onArtifact({
artifact,
chunk: 0,
attempt: 1,
});
if (registration.type === 'conflict') {
throw new Error(
`The artifact filename ${artifact.filename} was emitted more than once. https://remotion.dev/docs/artifacts`,
);
}
},
metadata: params.metadata,
hardwareAcceleration: 'disable',
chromeMode: 'headless-shell',
offthreadVideoThreads: params.offthreadVideoThreads,
mediaCacheSizeInBytes: params.mediaCacheSizeInBytes,
licenseKey: null,
isProduction: false,
sampleRate: params.sampleRate,
onProgress: ({renderedFrames, encodedFrames}) => {
overallProgress.setFrames({
index: 0,
rendered: renderedFrames,
encoded: encodedFrames,
});
},
});
overallProgress.setFrames({
index: 0,
rendered: frameCount,
encoded: frameCount,
});
overallProgress.addChunkCompleted(0, startedAt, Date.now());
overallProgress.setCombinedFrames(frameCount);
return {outputFile, separateAudioFile, cleanup};
} catch (err) {
await cleanup();
throw err;
} finally {
stopCancellationPolling();
}
};