The Media Processing Engineer transforms raw user-uploaded media into web-optimised deliverables. A 4MB JPEG uploaded by a user should never be served as-is - it should become a set of WebP images at multiple sizes, served from a CDN with appropriate cache headers. Video should be transcoded to multiple bitrate levels for adaptive streaming. Processing always happens asynchronously - never block an HTTP response on FFmpeg.
Image Processing with Sharp
import sharp from 'sharp';
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
const s3 = new S3Client({ region: 'us-east-1' });
interface ProcessedImage {
key: string;
width: number;
height: number;
size: number;
}
export async function processUploadedImage(
inputBuffer: Buffer,
userId: string,
imageId: string
): Promise<ProcessedImage[]> {
const SIZES = [
{ name: 'sm', width: 400 },
{ name: 'md', width: 800 },
{ name: 'lg', width: 1200 },
{ name: 'xl', width: 2000 },
];
const results = await Promise.all(
SIZES.map(async ({ name, width }) => {
const { data, info } = await sharp(inputBuffer)
.resize(width, undefined, { withoutEnlargement: true })
.webp({ quality: 82, effort: 4 })
.toBuffer({ resolveWithObject: true });
const key = `media/${userId}/${imageId}/${name}.webp`;
await s3.send(new PutObjectCommand({
Bucket: process.env.S3_BUCKET!,
Key: key,
Body: data,
ContentType: 'image/webp',
CacheControl: 'public, max-age=31536000, immutable',
}));
return { key, width: info.width, height: info.height, size: info.size };
})
);
return results;
}
FFmpeg Video Transcoding (Async Pipeline)
// Never run FFmpeg in a web request - use a queue
// Architecture: upload → S3 → SQS/BullMQ → worker → S3 → CloudFront
import { execFile } from 'child_process';
import { promisify } from 'util';
const exec = promisify(execFile);
async function transcodeVideo(inputPath: string, outputDir: string) {
// Generate HLS adaptive bitrate streams
await exec('ffmpeg', [
'-i', inputPath,
// 360p
'-vf', 'scale=-2:360', '-c:v', 'libx264', '-crf', '28', '-preset', 'fast',
'-c:a', 'aac', '-b:a', '96k',
`${outputDir}/360p.m3u8`,
// 720p
'-vf', 'scale=-2:720', '-c:v', 'libx264', '-crf', '23', '-preset', 'fast',
'-c:a', 'aac', '-b:a', '128k',
`${outputDir}/720p.m3u8`,
// Master playlist
'-master_pl_name', 'master.m3u8',
// Generate thumbnail at 1s mark
'-ss', '1', '-vframes', '1', `${outputDir}/thumbnail.jpg`,
]);
}
// Worker reads from queue, processes, updates DB
async function processVideoJob(job: { videoId: string; s3Key: string }) {
// 1. Download from S3 to temp
// 2. Transcode to HLS
// 3. Upload all output files to S3
// 4. Update DB: status = 'ready', thumbnailUrl, masterPlaylistUrl
// 5. Notify user via webhook or push notification
}
Delivery Checklist
- Serve images from a CDN (CloudFront, Cloudflare) - never direct S3 URLs (no CDN edge caching, high latency)
- Set
Cache-Control: public, max-age=31536000, immutablefor content-addressed URLs (include hash in filename) - Use
<picture>element with WebP and JPEG fallback; usesrcsetto match screen density - Generate
blurhashor low-quality placeholder (LQIP) from Sharp's resize to 20×20 output during processing - use as placeholder while full image loads - For video: use hls.js for browser HLS playback; set poster to the generated thumbnail