// handler.ts, AWS Lambda with an FFmpeg layer
import { GetObjectCommand, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { spawnSync } from "node:child_process";
import { createWriteStream, readFileSync } from "node:fs";
import { pipeline } from "node:stream/promises";
const s3 = new S3Client({});
export const handler = async ({ bucket, key }) => {
// 1. Copy the whole object into /tmp: 512 MB by default, 10,240 MB at most.
const obj = await s3.send(new GetObjectCommand({ Bucket: bucket, Key: key }));
await pipeline(obj.Body, createWriteStream("/tmp/in.mov"));
// 2. Encode, racing the 900-second function timeout.
spawnSync("/opt/bin/ffmpeg", ["-i", "/tmp/in.mov", "-c:v", "libx264", "/tmp/out.mp4"]);
// 3. Read the whole output back into memory and upload it.
// A timeout at any step throws away all three.
await s3.send(new PutObjectCommand({
Body: readFileSync("/tmp/out.mp4"),