Blog · 22 November 2026
Reframing landscape video to 9:16 in the browser with WebCodecs (no upload)
Most "make my video vertical" tools work the same way: upload your file, wait, download the result. For a 2 GB podcast recording that means waiting on the upload before you can even start, and it means your footage now lives on someone else's server.
Browsers can do this locally now. WebCodecs gives JavaScript access to the device's hardware video decoder and encoder, so I built Vertical Video Maker, a 16:9 → 9:16 (or 4:5 / 1:1) converter where the file never leaves the device. Here's how it works and what bit me along the way.
The pipeline
File ─► demux (MP4/MOV/WebM) ─► decode
─► draw each frame on a canvas
─► encode H.264 ─► mux MP4 ─► Blob
WebCodecs only does the codec part (VideoDecoder / VideoEncoder). It doesn't read or write containers, so you need something to demux and mux. I use Mediabunny, a TypeScript library that handles containers and wraps WebCodecs. Its Conversion API lets you hook into every decoded frame:
const input = new Input({ source: new BlobSource(file), formats: ALL_FORMATS });
const output = new Output({
format: new Mp4OutputFormat({ fastStart: 'in-memory' }),
target: new BufferTarget(),
});
const canvas = new OffscreenCanvas(W, H); // e.g. 1080×1920
const ctx = canvas.getContext('2d', { alpha: false })!;
const conversion = await Conversion.init({
input,
output,
video: {
codec, // picked at runtime, see below
bitrate,
processedWidth: W,
processedHeight: H,
process: (sample) => {
renderFrame(ctx, W, H, sample, settings, timeOf(sample));
return new VideoSample(canvas, { timestamp: sample.timestamp, duration: sample.duration });
},
},
audio: { codec: 'aac' },
});
await conversion.execute();
BlobSource reads the file lazily, so even a multi-GB input isn't loaded into memory up front. Audio passes through to AAC and video is decoded, re-framed, and encoded on the hardware encoder when one is available.
The codec is picked with a capability check rather than assumed:
for (const codec of ['avc', 'vp9', 'av1']) {
if (await canEncodeVideo(codec, { width, height, bitrate })) return codec;
}
In practice that's H.264 almost everywhere, which is what Instagram, TikTok and YouTube want anyway. Bitrate is about 3.9 bits per pixel per frame (≈8 Mbps at 1080×1920), comfortably above what the platforms re-encode to.
One renderer for preview and export
The most important design decision: the live preview and the export call the same renderFrame function. The preview passes the <video> element and the export passes a decoded VideoSample. Both are wrapped in a tiny draw callback:
type DrawFn = (ctx, dx, dy, dw, dh) => void;
// preview
const draw: DrawFn = (c, dx, dy, dw, dh) => c.drawImage(video, dx, dy, dw, dh);
// export
const draw: DrawFn = (c, dx, dy, dw, dh) => sample.draw(c, dx, dy, dw, dh);
If the preview and export share code, "it looked right in the preview" means it is right in the file. With two separate implementations, small differences crept in and the exported framing didn't quite match what you'd set up.
Gotcha: don't use drawImage's source-rectangle form on video
The obvious way to crop is the 9-argument drawImage(src, sx, sy, sw, sh, dx, dy, dw, dh). With video sources, Safari and Chrome disagreed on the coordinate space for the source rectangle, so the exported framing drifted from the preview on one browser.
The fix: always draw the whole frame, scaled and offset so the region you want lands in the destination box, and clip:
function drawRegion(ctx, draw, srcW, srcH, sx, sy, sw, sh, dx, dy, dw, dh) {
const kx = dw / sw, ky = dh / sh;
ctx.save();
ctx.beginPath();
ctx.rect(dx, dy, dw, dh);
ctx.clip();
draw(ctx, dx - sx * kx, dy - sy * ky, srcW * kx, srcH * ky);
ctx.restore();
}
It's slightly more work for the GPU and it's identical on every browser.
Blur fill without a slow blur
"Blur fill" (the whole 16:9 frame in the middle, a blurred copy filling the top and bottom) is the most-used mode. A real Gaussian blur of a 1080×1920 canvas every frame is expensive, and ctx.filter isn't supported everywhere.
The cheap trick: draw a cover-cropped copy of the frame into a tiny canvas (about 1/14 of the output size), then stretch it back up with high-quality smoothing. Downscaling throws away the detail, and bilinear upscaling does the rest. Where ctx.filter exists I add a 1.5 px blur on the tiny canvas, which costs almost nothing at that size. The scratch canvas has a 4 px padding ring that gets cropped off, so the blur's faded edges never show:
const bw = Math.round(W / 14), bh = Math.round(H / 14), pad = 4;
scratch.width = bw + 2 * pad; scratch.height = bh + 2 * pad;
if (filterSupported) sctx.filter = 'blur(1.5px)';
drawRegion(sctx, draw, /* cover-crop source into the padded scratch */ ...);
ctx.drawImage(scratch, pad, pad, bw, bh, 0, 0, W, H); // stretch to full output
Then a semi-transparent black fill dims it so the sharp video stands out.
Following a moving subject: eased keyframes
A fixed crop loses anyone who walks across the frame. Users can save a focus point at any moment, and the crop pans between them. Linear interpolation looks robotic, so each segment uses smoothstep:
const smooth = (u: number) => u * u * (3 - 2 * u);
export function focusAt(s, t) {
// ...find keyframes a, b around t
const u = smooth((t - a.t) / (b.t - a.t));
return { x: a.x + (b.x - a.x) * u, y: a.y + (b.y - a.y) * u };
}
The crop rectangle is then clamped to the frame so it never shows past the edge.
Gotcha: timestamps that don't start at zero
This one was subtle. In the preview, video.currentTime is 0 on the first frame. But many files (iPhone footage, stock clips, anything that's been edited) have container timestamps that start at, say, 0.067 s or even several seconds in. Keyframes are saved in preview time, so the export applied each pan a little early or late.
The fix is to measure time from the first sample the export actually receives, and to shift trim points by the track's first timestamp:
const base = Math.max(0, await videoTrack.getFirstTimestamp());
// trim in raw container time
trim: { start: base + trimStart, end: base + trimEnd },
// map each sample back to preview time
process: (sample) => {
firstTs ??= sample.timestamp;
const t = trimStart + (sample.timestamp - firstTs);
...
}
Gotcha: video.duration lies sometimes
WebM files from MediaRecorder and some screen recordings report Infinity (or a short value) as their duration on loadedmetadata, and only correct it later. My timeline showed Infinity:NaN. Now the duration is read from the container itself (Mediabunny's computeDuration()) and also updated on durationchange, whichever arrives with a real number.
Android: share a video straight into the web app
The site is a PWA with a share_target in the manifest, so on Android it shows up in the Gallery's share sheet. The share arrives as a multipart POST, which a service worker catches, stashes in the Cache API, and redirects to the page:
// sw.js
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
if (event.request.method !== 'POST' || url.pathname !== '/share-target') return;
event.respondWith((async () => {
const form = await event.request.formData();
const file = form.get('video');
const cache = await caches.open('vvm-share');
await cache.put('/__shared-video', new Response(file, { headers: { 'Content-Type': file.type } }));
return Response.redirect('/?shared=1#app', 303);
})());
});
The service worker caches nothing else, so deploys are never stale.
Limits and trade-offs
- The output is built in memory (
BufferTarget). That's fine for Reels/Shorts-length clips, but a full one-hour export at 1080p would be several GB of RAM. Streaming to disk with the File System Access API is the obvious next step for long exports. - Speed depends on the device. With a hardware encoder, exports are quick; on devices without one, the browser falls back to a much slower software path.
- No WebCodecs, no export. Recent Chrome, Edge and Safari are fine. When it's missing, the page says so up front instead of failing halfway.
- Canvas 2D, not WebGL. For a crop, a scale and a cheap blur, 2D canvas is fast enough and much simpler. A WebGL path would help if I ever add real effects.
Try it
The tool is at verticalvideomaker.app. It's free with a small watermark at 720p, and a one-time $19.95 removes the watermark and unlocks 1080p. Because there's no server-side processing, running it costs almost nothing, which is why there's no subscription.
Happy to answer questions about WebCodecs, Mediabunny or the canvas tricks in the comments.