TL;DR
-pix_fmt yuv420pon a screen capture gives you (1) untagged color that Chromium decoded up to 19 points off on saturated buttons, and (2) red text that drops to ~27 dB PSNR because 4:2:0 shares one color sample per 2×2 block. Fix (1) for free with BT.709 conversion + tags. Fix (2) with 4:4:4 (38 dB) or a 2× nearest-neighbour upscale that stays 4:2:0 (42 dB), at a size cost.
Let's build a small lab that encodes a UI screenshot several ways, decodes each in a real browser, reads the pixels back, and scores the text. Versions: FFmpeg 6.1.1 (x264), Node 22.23.5, Playwright 1.63.0 with Chromium 158.
1. Make a test "screen" 🖥️
Any screenshot with colored text works. I rendered a fake dashboard in HTML (navy sidebar #1E3A8A, red button #DC2626, green #16A34A, a 13 px red error line, syntax-highlighted code) and captured it:
// shoot.js
const { chromium } = require('playwright');
(async () => {
const b = await chromium.launch();
const p = await b.newPage({ viewport: { width: 1920, height: 1080 } });
await p.goto('file://' + process.cwd() + '/ui.html');
await p.screenshot({ path: 'screen.png' });
await b.close();
})();
2. Encode four ways 🛠️
#!/usr/bin/env bash
# encode.sh
E="-y -loop 1 -framerate 30 -i screen.png -t 5 -c:v libx264 -preset medium -crf 18 -movflags +faststart"
TAGS="-colorspace bt709 -color_primaries bt709 -color_trc bt709 -color_range tv"
# A: what most pipelines do
ffmpeg $E -pix_fmt yuv420p naive.mp4
# B: convert with BT.709 explicitly AND write the tags
ffmpeg $E -vf "scale=out_color_matrix=bt709:out_range=tv,format=yuv420p" $TAGS tagged.mp4
# C: B plus the "high quality" scaler flags everyone suggests
ffmpeg $E -vf "scale=out_color_matrix=bt709:out_range=tv:flags=lanczos+accurate_rnd+full_chroma_int,format=yuv420p" $TAGS tagged_hq.mp4
# D: keep full color resolution
ffmpeg $E -vf "scale=out_color_matrix=bt709:out_range=tv,format=yuv444p" $TAGS yuv444.mp4
Check what got written:
$ for f in naive tagged yuv444; do ffprobe -v error -select_streams v \
-show_entries stream=profile,pix_fmt,color_space,color_primaries,color_transfer,color_range \
-of csv=p=0 $f.mp4; done
High,yuv420p,unknown,unknown,unknown,unknown
High,yuv420p,tv,bt709,bt709,bt709
High 4:4:4 Predictive,yuv444p,tv,bt709,bt709,bt709
naive.mp4 carries no color metadata at all. The player has to guess.
3. Read pixels back in the browser 🔬
ffprobe tells you what's in the file. This tells you what viewers see:
// probe.js
const { chromium } = require('playwright');
const http = require('http'), fs = require('fs');
const srv = http.createServer((q, r) => {
const f = '.' + q.url; if (!fs.existsSync(f)) { r.writeHead(404); return r.end(); }
r.writeHead(200, { 'Content-Type': f.endsWith('.mp4') ? 'video/mp4' : 'text/html' });
fs.createReadStream(f).pipe(r);
}).listen(8090);
const points = { redButton: [360, 60], green: [500, 60], sidebar: [150, 700], blueBox: [500, 400] };
const expected = { redButton: [220, 38, 38], green: [22, 163, 74], sidebar: [30, 58, 138], blueBox: [37, 99, 235] };
(async () => {
const b = await chromium.launch(); const p = await b.newPage();
await p.goto('http://localhost:8090/ui.html'); // same origin, so the canvas isn't tainted
for (const f of ['naive', 'tagged', 'tagged_hq', 'yuv444']) {
const px = await p.evaluate(async ({ f, points }) => {
const v = document.createElement('video'); v.muted = true; v.src = `/${f}.mp4`;
await new Promise((ok, bad) => { v.onloadeddata = ok; v.onerror = bad; });
v.currentTime = 2; await new Promise(r => (v.onseeked = r));
const c = Object.assign(document.createElement('canvas'), { width: 1920, height: 1080 });
const ctx = c.getContext('2d'); ctx.drawImage(v, 0, 0);
return Object.fromEntries(Object.entries(points).map(([k, [x, y]]) =>
[k, Array.from(ctx.getImageData(x, y, 1, 1).data.slice(0, 3))]));
}, { f, points });
console.log(f.padEnd(10), Object.entries(px).map(([k, v]) =>
`${k}=${v} (max off ${Math.max(...v.map((c, i) => Math.abs(c - expected[k][i])))})`).join(' '));
}
await b.close(); srv.close();
})();
$ node probe.js
naive redButton=236,55,40 (max off 17) green=10,144,75 (max off 19) sidebar=26,59,136 (max off 4) blueBox=29,100,233 (max off 8)
tagged redButton=222,38,41 (max off 3) green=21,164,77 (max off 3) sidebar=31,58,134 (max off 4) blueBox=36,99,227 (max off 8)
tagged_hq redButton=219,39,41 (max off 3) green=23,163,77 (max off 3) sidebar=29,58,134 (max off 4) blueBox=36,99,227 (max off 8)
yuv444 redButton=222,38,41 (max off 3) green=24,163,77 (max off 3) sidebar=29,58,134 (max off 4) blueBox=36,99,227 (max off 8)
The untagged file shifts saturated red and green by 17 to 19 points. Tagging brings them within 3. (The blue box is 8 off in every variant, 4:4:4 included, so that one isn't a tagging problem.)
💡 Tip:
out_color_matrixand-colorspacemust agree. Tagging BT.709 while converting with FFmpeg's default matrix just moves the error somewhere else.
4. Score the text, not the frame 📏
Whole-frame metrics hide this, because most of a UI is flat white. Crop to the red error text and compare against the PNG in RGB:
# psnr_crop.sh <file> <matrix> <crop w:h:x:y>
ffmpeg -i "$1" -i screen.png -filter_complex \
"[0:v]trim=start_frame=60:end_frame=61,scale=in_color_matrix=$2:in_range=tv:flags=accurate_rnd+full_chroma_int,format=rgb24,crop=$3[a];\
[1:v]format=rgb24,crop=$3[b];[a][b]psnr" -f null - 2>&1 | grep -o 'average:[0-9.]*'
| region | naive | tagged | tagged_hq | yuv444 |
|---|---|---|---|---|
| red error text (240×24) | 26.87 dB | 27.05 dB | 27.08 dB | 38.22 dB |
| code block (540×110) | 34.38 dB | 34.02 dB | 34.19 dB | 40.83 dB |
(naive was decoded with bt601 for a fair comparison; the rest with bt709.)
The scaler flags buy 0.03 dB. The pixel format buys 11. That's chroma subsampling: in 4:2:0, every 2×2 block of pixels shares one color sample, and thin red strokes get averaged with the white around them.
5. Two ways to keep the color ✅
Option A: 4:4:4
Already done above (yuv444.mp4). 40,452 bytes vs 37,938 for tagged.mp4 on this static clip. Chromium played it.
⚠️ Note: that's the "High 4:4:4 Predictive" profile. I only tested headless Chromium. Test Safari, iOS, Android and any hardware-decode path you depend on before making it your only rendition.
Option B: 2× nearest-neighbour, still 4:2:0
Blow every pixel up into a 2×2 block, and the quarter-resolution chroma plane lines up with the original pixels exactly:
ffmpeg -y -loop 1 -framerate 30 -i screen.png -t 5 \
-vf "scale=3840:2160:flags=neighbor:out_color_matrix=bt709:out_range=tv,format=yuv420p" \
-c:v libx264 -preset medium -crf 18 \
-colorspace bt709 -color_primaries bt709 -color_trc bt709 -color_range tv up2x.mp4
Score it after scaling back to 1080p with scale=1920:1080:flags=area in the PSNR graph:
red up2x average:42.478025
code up2x average:45.198530
Better than 4:4:4, and plain 4:2:0 High profile, so it plays wherever 4K H.264 plays. The cost: 84,012 bytes, about 2.2× tagged.mp4 here. Expect more on real recordings with scrolling.
Results
| variant | color drift (max) | red text PSNR | size (5 s still) | plays as |
|---|---|---|---|---|
-pix_fmt yuv420p |
19 | 26.87 dB | 38,114 B | High, untagged |
| BT.709 + tags | 3 | 27.05 dB | 37,938 B | High |
| + scaler flags | 3 | 27.08 dB | 38,399 B | High |
| 4:4:4 | 3 | 38.22 dB | 40,452 B | High 4:4:4 Predictive |
| 2× upscale, 4:2:0 | n/a | 42.48 dB | 84,012 B | High @ 3840×2160 |
My rules after this:
-
Always convert with
out_color_matrix=bt709and write the four tags. Free. - Black-on-white walkthroughs: stop there. Black text is a brightness edge and survives 4:2:0.
- Colored text, syntax highlighting, charts, brand-colored UI: add the 2× rendition, or 4:4:4 for internal tools.
- If the capture came from a 2× Retina screen, don't downscale to 1080p and then subsample. Encode at the captured size.
⚠️ Caveat: one synthetic still frame, x264 at CRF 18, one browser. Sizes are directional; the color math isn't.
What's next
- Run
probe.jsagainst a real recording from your own library. If you see double-digit drift, check your capture tool's color settings too. - Repeat the PSNR crop test with AV1 or VP9: both have 4:4:4 profiles, with their own playback-support questions.
- If you transcode uploads server-side, read the input's
color_spacewith ffprobe and refuse to guess silently.

Top comments (0)