Skip to content

🎥 Make videos programmatically with React

6.6 KB
Raw Blame
import {createReadStream} from 'node:fs';
import {
	LambdaClientInternals,
	ServerlessRoutines,
	type OnStream,
	type S3RendererArtifactMetadata,
	type S3RendererStatus,
	type ServerlessPayload,
	type AwsRegion,
} from '@remotion/lambda-client';
import type {EmittedArtifact} from '@remotion/renderer';

export const makeS3RendererOutput = ({
	params,
	expectedBucketOwner,
	region,
}: {
	params: ServerlessPayload;
	expectedBucketOwner: string | null;
	region: AwsRegion;
}) => {
	if (params.type !== ServerlessRoutines.renderer) {
		throw new Error('Expected renderer payload');
	}

	const startedAt = Date.now();
	let lambdaInvoked = true;
	let renderedFrames = 0;
	let encodedFrames = 0;
	let uploadedFrames = 0;
	let uploadedSizeInBytes = 0;
	let writeQueue = Promise.resolve();
	const artifacts: Array<{
		key: string;
		metadata: S3RendererArtifactMetadata;
	}> = [];
	const artifactUploads: Promise<void>[] = [];

	const statusKey = LambdaClientInternals.rendererTransportStatusKey({
		renderId: params.renderId,
		chunk: params.chunk,
		attempt: params.attempt,
	});

	const writeStatus = (status: S3RendererStatus) => {
		writeQueue = writeQueue.then(() =>
			LambdaClientInternals.lambdaWriteFileImplementation({
				bucketName: params.bucketName,
				key: statusKey,
				body: JSON.stringify(status),
				region,
				privacy: 'private',
				expectedBucketOwner,
				downloadBehavior: null,
				customCredentials: null,
				forcePathStyle: params.forcePathStyle,
				storageClass: null,
				requestHandler: null,
			}),
		);
		return writeQueue;
	};

	const runningStatus = (): S3RendererStatus => ({
		schema: 1,
		state: 'running',
		chunk: params.chunk,
		attempt: params.attempt,
		lambdaInvoked,
		renderedFrames,
		encodedFrames,
		uploadedFrames,
		uploadedSizeInBytes,
		startedAt,
	});

	const initialize = () => writeStatus(runningStatus());

	const onStream: OnStream = async (message) => {
		if (message.type === 'lambda-invoked') {
			lambdaInvoked = true;
			await writeStatus(runningStatus());
			return;
		}

		if (message.type === 'frames-rendered') {
			renderedFrames = Math.max(renderedFrames, message.payload.rendered);
			encodedFrames = Math.max(encodedFrames, message.payload.encoded);
			await writeStatus(runningStatus());
			return;
		}

		if (message.type === 'frames-uploaded') {
			uploadedFrames = Math.max(uploadedFrames, message.payload.uploaded);
			uploadedSizeInBytes = Math.max(
				uploadedSizeInBytes,
				message.payload.sizeInBytes,
			);
			await writeStatus(runningStatus());
			return;
		}

		if (message.type === 'artifact-emitted') {
			const artifact = LambdaClientInternals.deserializeArtifact(
				message.payload.artifact,
			);
			const key = LambdaClientInternals.rendererTransportArtifactKey(
				{
					renderId: params.renderId,
					chunk: params.chunk,
					attempt: params.attempt,
				},
				artifacts.length,
			);
			const metadata: S3RendererArtifactMetadata = {
				filename: artifact.filename,
				frame: artifact.frame,
				binary: artifact.content instanceof Uint8Array,
				downloadBehavior: artifact.downloadBehavior,
			};
			artifacts.push({key, metadata});
			const upload = LambdaClientInternals.lambdaWriteFileImplementation({
				bucketName: params.bucketName,
				key,
				body: artifact.content,
				region,
				privacy: 'private',
				expectedBucketOwner,
				downloadBehavior: null,
				customCredentials: null,
				forcePathStyle: params.forcePathStyle,
				storageClass: null,
				requestHandler: null,
			});
			artifactUploads.push(upload);
			await upload;
			return;
		}

		if (message.type === 'error-occurred') {
			await writeStatus({
				schema: 1,
				state: 'failed',
				chunk: params.chunk,
				attempt: params.attempt,
				lambdaInvoked,
				renderedFrames,
				encodedFrames,
				uploadedFrames,
				uploadedSizeInBytes,
				startedAt,
				failedAt: Date.now(),
				errorInfo: message.payload.errorInfo,
				shouldRetry: message.payload.shouldRetry,
			});
			return;
		}

		if (message.type === 'chunk-complete') {
			if (params.output.type === 'sequence') {
				await Promise.all(artifactUploads);
				await writeStatus({
					...runningStatus(),
					state: 'completed',
					lambdaInvoked: true,
					completedAt: message.payload.rendered,
					videoKey: null,
					audioKey: null,
					artifacts,
				});
			}

			return;
		}

		throw new Error(
			`Unexpected ${message.type} event in S3 renderer transport`,
		);
	};

	const uploadMediaAndComplete = async ({
		videoOutputLocation,
		audioOutputLocation,
		isAudioOnly,
		completedAt,
	}: {
		videoOutputLocation: string;
		audioOutputLocation: string | null;
		isAudioOnly: boolean;
		completedAt: number;
	}) => {
		const videoKey = isAudioOnly
			? null
			: LambdaClientInternals.rendererTransportVideoKey({
					renderId: params.renderId,
					chunk: params.chunk,
					attempt: params.attempt,
				});
		const audioKey =
			audioOutputLocation || isAudioOnly
				? LambdaClientInternals.rendererTransportAudioKey({
						renderId: params.renderId,
						chunk: params.chunk,
						attempt: params.attempt,
					})
				: null;

		await Promise.all([
			...artifactUploads,
			videoKey
				? LambdaClientInternals.lambdaWriteFileImplementation({
						bucketName: params.bucketName,
						key: videoKey,
						body: createReadStream(videoOutputLocation),
						region,
						privacy: 'private',
						expectedBucketOwner,
						downloadBehavior: null,
						customCredentials: null,
						forcePathStyle: params.forcePathStyle,
						storageClass: null,
						requestHandler: null,
					})
				: null,
			audioKey
				? LambdaClientInternals.lambdaWriteFileImplementation({
						bucketName: params.bucketName,
						key: audioKey,
						body: createReadStream(audioOutputLocation ?? videoOutputLocation),
						region,
						privacy: 'private',
						expectedBucketOwner,
						downloadBehavior: null,
						customCredentials: null,
						forcePathStyle: params.forcePathStyle,
						storageClass: null,
						requestHandler: null,
					})
				: null,
		]);

		lambdaInvoked = true;
		await writeStatus({
			schema: 1,
			state: 'completed',
			chunk: params.chunk,
			attempt: params.attempt,
			lambdaInvoked: true,
			renderedFrames,
			encodedFrames,
			uploadedFrames,
			uploadedSizeInBytes,
			startedAt,
			completedAt,
			videoKey,
			audioKey,
			artifacts,
		});
	};

	return {initialize, onStream, uploadMediaAndComplete};
};

export const artifactFromS3 = ({
	metadata,
	content,
}: {
	metadata: S3RendererArtifactMetadata;
	content: Uint8Array;
}): EmittedArtifact => ({
	filename: metadata.filename,
	frame: metadata.frame,
	downloadBehavior: metadata.downloadBehavior,
	content: metadata.binary ? content : new TextDecoder().decode(content),
});