We have a free ad maker that draws an ad on a canvas: a photo, a headline, a button. People wanted the same ad as a short video, with its parts arriving one after another, and they wanted it on their phone, from the page, with nothing uploaded.
So the video is made in the browser. The canvas is drawn frame by frame, each frame goes to WebCodecs' VideoEncoder as H.264, and a small MP4 writer of about 40 lines wraps the frames into a file. Where a browser has no VideoEncoder, MediaRecorder records the canvas instead.
Here is the whole path, with the parts that took us more than one try.
1. Load the fonts before the first frame
A canvas draws with whatever font is loaded at the moment it draws. If the web font is still on its way, frame 1 comes out in a fallback font and frame 20 in the right one.
await Promise.all([ document.fonts.load('80px "Funnel Display"'), document.fonts.load('600 32px Inter'), ]); 2. Make every frame a function of time
The encoder does not care about the clock. If render(ctx, t) draws the frame for time t and keeps no state between calls, you can draw frame 0, frame 1, frame 2 as fast as the machine goes. A six-second video takes as long as the machine needs to draw and encode it, with no waiting for the clock.
const FPS = 30, SECONDS = 6, W = 1080, H = 1920; const canvas = document.createElement('canvas'); canvas.width = W; canvas.height = H; const ctx = canvas.getContext('2d'); // render(ctx, t): the ad at t seconds; every part's position and opacity computed from t 3. Ask for a codec the browser can encode
H.264 comes in profiles and levels, written into the codec string. We try them in order and take the first the browser says it supports:
let config = null; for (const codec of ['avc1.42E028', 'avc1.42002A', 'avc1.4D4028', 'avc1.640028']) { const c = { codec, width: W, height: H, bitrate: 8e6, framerate: FPS, avc: { format: 'avc' } }; if ((await VideoEncoder.isConfigSupported(c)).supported) { config = c; break; } } -
42is the Baseline profile. It has no B-frames, so frames come out of the encoder in the order they are shown, and the MP4 needs no reordering table. -
28(hex for 40) is level 4.0, which is enough for 1080 by 1920 at 30 frames a second.2Ais level 4.2;4Dis Main and64is High, kept as fallbacks. -
avc: { format: 'avc' }gives length-prefixed frames plus anavcCrecord (the decoder setup), which is what MP4 wants. The other choice,annexb, is for raw streams.
4. Encode every frame, and wait when the queue fills
const chunks = []; let avcC = null, failed = null; const bytes = (d) => d instanceof ArrayBuffer ? new Uint8Array(d.slice(0)) : new Uint8Array(d.buffer.slice(d.byteOffset, d.byteOffset + d.byteLength)); const encoder = new VideoEncoder({ output: (chunk, meta) => { if (!avcC && meta?.decoderConfig?.description) avcC = bytes(meta.decoderConfig.description); const data = new Uint8Array(chunk.byteLength); chunk.copyTo(data); chunks.push({ data, key: chunk.type === 'key', ts: chunk.timestamp }); }, error: (e) => { failed = e; }, }); encoder.configure(config); const n = FPS * SECONDS; for (let i = 0; i < n && !failed; i++) { render(ctx, i / FPS); const frame = new VideoFrame(canvas, { timestamp: Math.round(i * 1e6 / FPS), // microseconds duration: Math.round(1e6 / FPS), }); encoder.encode(frame, { keyFrame: i % (FPS * 2) === 0 }); // a key frame every 2 seconds frame.close(); while (encoder.encodeQueueSize > 3 && !failed) await new Promise((r) => setTimeout(r, 4)); } await encoder.flush(); encoder.close(); Two things matter here:
- Close every
VideoFrame. Each one holds a full-size picture; 180 of them left open is a lot of memory on a phone. - Watch
encodeQueueSize. Without the wait, the loop hands over all 180 frames at once and the encoder falls behind. A short wait keeps it fed without piling up, and it gives the page a moment to update a progress label.
5. Write the MP4
An MP4 is a tree of boxes: four bytes of size, four letters of type, then the contents. One helper does almost all of it:
const ascii = (s) => Uint8Array.from(s, (c) => c.charCodeAt(0)); const u32 = (v) => Uint8Array.of(v >>> 24 & 255, v >>> 16 & 255, v >>> 8 & 255, v & 255); const concat = (parts) => { const out = new Uint8Array(parts.reduce((s, p) => s + p.length, 0)); let o = 0; for (const p of parts) { out.set(p, o); o += p.length; } return out; }; const box = (type, ...parts) => { const body = concat(parts); return concat([u32(body.length + 8), ascii(type), body]); }; // a "full box" is the same with a version byte and three flag bytes in front of its contents For one video track the tree is:
ftyp the file type moov the index mvhd the movie: its time scale and length trak tkhd the track: width and height mdia mdhd the track's time scale (we use fps x 1000, so each frame lasts 1000 ticks) hdlr 'vide' minf vmhd dinf > dref > url stbl the sample table stsd > avc1 > avcC the encoder's avcC record, copied as it came stts every frame the same duration stss which frames are key frames stsc all frames in one chunk stsz each frame's size in bytes stco where that chunk starts in the file mdat the frames, back to back We put moov before mdat, so a player can start before the whole file is read. The catch is stco: it holds the byte offset of the frames, and that offset depends on the size of moov itself. We build moov once with offset 0 to measure it, then again with the real offset:
const head = ftyp.length + moov(0).length + 8; // + the mdat box's own 8-byte header const file = new Blob([ftyp, moov(head), u32(8 + totalFrameBytes), ascii('mdat'), ...chunks.map((c) => c.data)], { type: 'video/mp4' }); The size never changes between the two builds, because stco is a fixed four bytes either way.
If you take a profile that reorders frames (Main or High with B-frames), you also need a ctts box with each frame's display offset, and an edit list so playback starts at the first frame shown. With Baseline you can skip both.
6. The fallback: record the canvas
Where VideoEncoder is missing, the canvas is played once and recorded as it plays:
const type = ['video/mp4;codecs=avc1.42E01E', 'video/mp4', 'video/webm;codecs=vp9', 'video/webm'] .find((m) => MediaRecorder.isTypeSupported(m)); const rec = new MediaRecorder(canvas.captureStream(FPS), { mimeType: type, videoBitsPerSecond: 8e6 }); This one runs in real time (six seconds for six seconds of video) and gives MP4 or WebM depending on the browser. It is the slower path, so it is the second one.
7. Saving it on an iPhone
On an iPhone the natural way to save a video is the share sheet (Save Video, or straight to Instagram), through navigator.share({ files: [file] }). The share sheet only opens on a fresh tap, and by the time the video is encoded, the tap that started it is a few seconds old.
So the button changes when the video is ready: "Download video" becomes "Save video", and the second tap opens the share sheet. Everywhere else, the first tap downloads the file through a link with a download attribute.
What it looks like
You can try it in the free hair salon ad maker: pick a template, type a name, and press the video button. The video is made on your own device; nothing is sent anywhere to make it.
If you build something similar, the three things to get right are the fonts before the first frame, the queue while encoding, and the stco offset in the MP4.

