From 913c461b9be3df54f8f1ff5c6b5cf1e6c46376e1 Mon Sep 17 00:00:00 2001 From: "juancarlos.cavero" Date: Sat, 18 Jul 2026 02:00:18 +0200 Subject: [PATCH] =?UTF-8?q?fix(nvenc):=20force=20yuv420p=20output=20?= =?UTF-8?q?=E2=80=94=20RGB=20pipe=20input=20made=20nvenc=20emit=20GBR-colo?= =?UTF-8?q?rspace=20H.264=20that=20web=20players=20render=20with=20swapped?= =?UTF-8?q?=20colors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ffmpeg_utils.py | 11 ++++++++--- tests/test_ffmpeg_utils.py | 10 +++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/ffmpeg_utils.py b/ffmpeg_utils.py index dcb032a..f16d2ee 100644 --- a/ffmpeg_utils.py +++ b/ffmpeg_utils.py @@ -27,14 +27,19 @@ _X264_ARGS = { # NVENC -cq is not 1:1 with x264 CRF: benchmarked on the prod GPU (RTX 4000 # Ada), cq ≈ crf + 7 lands in the same file-size ballpark, with vbr + AQ for # quality. Presets p1-p7: p5 ≈ "medium", p4 ≈ "fast". +# -pix_fmt yuv420p is REQUIRED: with RGB input (the bgr24 rawvideo pipe from +# OpenCV) nvenc otherwise emits H.264 in gbrp/GBR colorspace, which ffmpeg +# reads fine but web players render as a magenta/green mess. _NVENC_ARGS = { QUALITY: ["-c:v", "h264_nvenc", "-preset", "p5", "-tune", "hq", "-rc", "vbr", "-cq", "25", "-b:v", "0", - "-spatial-aq", "1", "-temporal-aq", "1"], + "-spatial-aq", "1", "-temporal-aq", "1", "-pix_fmt", "yuv420p"], QUALITY_FAST: ["-c:v", "h264_nvenc", "-preset", "p4", "-tune", "hq", - "-rc", "vbr", "-cq", "25", "-b:v", "0", "-spatial-aq", "1"], + "-rc", "vbr", "-cq", "25", "-b:v", "0", "-spatial-aq", "1", + "-pix_fmt", "yuv420p"], DELIVERY: ["-c:v", "h264_nvenc", "-preset", "p4", - "-rc", "vbr", "-cq", "29", "-b:v", "0", "-spatial-aq", "1"], + "-rc", "vbr", "-cq", "29", "-b:v", "0", "-spatial-aq", "1", + "-pix_fmt", "yuv420p"], } _probe_lock = threading.Lock() diff --git a/tests/test_ffmpeg_utils.py b/tests/test_ffmpeg_utils.py index 8beba5b..056a635 100644 --- a/tests/test_ffmpeg_utils.py +++ b/tests/test_ffmpeg_utils.py @@ -37,9 +37,13 @@ def test_unknown_tier_raises(): def test_nvenc_mode_uses_nvenc_when_probe_passes(monkeypatch): monkeypatch.setenv("FFMPEG_ENCODER", "nvenc") monkeypatch.setattr(ffmpeg_utils, "_probe_nvenc", lambda: True) - args = video_encode_args(QUALITY) - assert args[:2] == ["-c:v", "h264_nvenc"] - assert "-cq" in args + for tier in (QUALITY, QUALITY_FAST, DELIVERY): + args = video_encode_args(tier) + assert args[:2] == ["-c:v", "h264_nvenc"] + assert "-cq" in args + # Without an explicit yuv420p, RGB input makes nvenc emit GBR-space + # H.264 that web players render with wrong colors. + assert args[-2:] == ["-pix_fmt", "yuv420p"] def test_nvenc_mode_falls_back_to_x264_when_probe_fails(monkeypatch):