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):