clipwright/ffmpeg_utils.py
juancarlos.cavero ea78445953 feat(clips): four quality fixes found by auditing real user output
Captions must always be the LAST layer. Editing or hooking a clip that already
had them burned in produced `edited_subtitled_<...>`, and the next subtitle pass
stacked a second caption layer on top of the first — visibly doubled and
unreadable in a real user clip found in prod today. /api/edit and /api/hook now
derive from the clean file and put captions back afterwards, so a restyle always
has an un-captioned base to burn onto. The latent bug predates auto-captions but
shipping them by default made it the normal path. The walk-back is now one
helper instead of being open-coded in the subtitle endpoint.

Loudness was uncontrolled. A clip inherited whatever its source was mastered at,
measured across real delivered clips from -13.8 LUFS on a loud upload down to
-28 on a quiet talk, while TikTok/Reels/Shorts all normalise playback to around
-14 — so the quiet ones just sound thin in the feed. loudnorm at the clip cut,
where the audio is already being encoded, so it costs nothing. Verified to pull
both extremes to target: -29.7 -> -13.7 and -10.1 -> -13.5. AUDIO_NORMALIZE=0.

The GENERAL layout wasted the frame. Fitting a 16:9 source to full width left
it 608px tall in a 1920 frame: 32% content, 68% blurred filler. Now scaled by
height to 42%, trimming the overflow. Deliberately conservative — GENERAL is
chosen for groups and landscapes, exactly where cropping the sides cuts someone
out, so 0.55 was tried and rejected for throwing away 42% of the width. 0.42
keeps ~76%. GENERAL_CONTENT_HEIGHT_RATIO=0.32 restores the old behaviour.

The TRACK/GENERAL classifier only counted faces, so a screencast that happened
to contain one face was cropped to it — real prod clips came out with headlines
cut mid-word and unreadable. A cheap Canny pass over the side thirds (the part a
9:16 crop discards), with face boxes blanked first, now routes text-dense scenes
to GENERAL instead. GRAPHIC_DENSITY_THRESHOLD=1.0 disables it.
2026-07-26 20:59:04 +02:00

142 lines
5.8 KiB
Python

"""Central video-encoder selection for every ffmpeg encode call site.
FFMPEG_ENCODER env values:
x264 (default) — CPU libx264, exact pre-GPU behavior
nvenc — force h264_nvenc; probed once and falls back to x264
(with a warning) if the GPU/driver is unavailable
auto — h264_nvenc when the probe succeeds, else x264
Only the codec/quality args live here; surrounding args (-movflags, -pix_fmt,
audio codecs, filters) stay at each call site.
"""
import os
import subprocess
import threading
# Quality tiers pinning the historical libx264 settings.
QUALITY = "quality" # was: -preset medium -crf 18
QUALITY_FAST = "quality_fast" # was: -preset fast -crf 18
DELIVERY = "delivery" # was: -preset fast -crf 22
_X264_ARGS = {
QUALITY: ["-c:v", "libx264", "-preset", "medium", "-crf", "18"],
QUALITY_FAST: ["-c:v", "libx264", "-preset", "fast", "-crf", "18"],
DELIVERY: ["-c:v", "libx264", "-preset", "fast", "-crf", "22"],
}
# 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", "-pix_fmt", "yuv420p"],
QUALITY_FAST: ["-c:v", "h264_nvenc", "-preset", "p4", "-tune", "hq",
"-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",
"-pix_fmt", "yuv420p"],
}
# Output args that drop container/stream metadata carried over from the source
# — most notably YouTube's "produced by Google Inc." stream handler, which
# otherwise survives every re-encode (ffmpeg copies input metadata by default)
# and rides into the published clip. The per-stream specifiers are required:
# global -map_metadata -1 alone leaves the audio handler_name intact on a
# stream copy. Empty audio/video specifiers are harmless when a clip lacks that
# stream (ffmpeg ignores them, verified). Spliced in before the output filename
# at each final-artifact producer; kept out of video_encode_args() so that
# stays purely codec/quality args.
METADATA_SCRUB = ["-map_metadata", "-1", "-map_chapters", "-1",
"-map_metadata:s:v", "-1", "-map_metadata:s:a", "-1"]
# Loudness normalisation for the delivered clip.
#
# Without this the clip inherits whatever the source was mastered at, so a
# user's clips land anywhere: measured across real delivered clips on
# 26-jul-2026, from -13.8 LUFS on a loud upload down to -28 LUFS on a quiet
# talk. TikTok, Reels and Shorts all normalise playback to roughly -14 LUFS,
# which means the quiet ones just sound thin next to everything else in the
# feed — the loud ones aren't rewarded, the quiet ones are punished.
#
# I=-14 matches the platforms' target, TP=-1.5 leaves headroom so their own
# re-encode can't clip, LRA=11 is the usual allowance for speech. Applied at
# the clip cut, where the audio is being encoded to AAC anyway, so it costs
# nothing extra. AUDIO_NORMALIZE=0 turns it off.
LOUDNORM_FILTER = "loudnorm=I=-14:TP=-1.5:LRA=11"
def audio_encode_args():
"""AAC encode args for a delivered clip, with loudness normalisation."""
args = ["-c:a", "aac"]
if os.environ.get("AUDIO_NORMALIZE", "1").strip() != "0":
args = ["-af", LOUDNORM_FILTER] + args
return args
_probe_lock = threading.Lock()
_nvenc_ok = None # None = not probed yet
_announced = False
def _probe_nvenc():
"""One tiny lavfi encode to prove h264_nvenc works end-to-end.
NVENC rejects frames smaller than ~145px, so the probe uses 256x256.
Any failure (no ffmpeg binary, no GPU, no driver libs) means False.
"""
cmd = [
"ffmpeg", "-hide_banner", "-loglevel", "error",
"-f", "lavfi", "-i", "color=black:s=256x256:d=0.1",
"-c:v", "h264_nvenc", "-f", "null", "-",
]
try:
result = subprocess.run(
cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=30
)
return result.returncode == 0
except Exception:
return False
def nvenc_available():
"""Probe h264_nvenc once and cache the verdict (thread-safe)."""
global _nvenc_ok
if _nvenc_ok is None:
with _probe_lock:
if _nvenc_ok is None:
_nvenc_ok = _probe_nvenc()
return _nvenc_ok
def reset_encoder_cache():
"""Test hook: forget the cached probe result."""
global _nvenc_ok, _announced
with _probe_lock:
_nvenc_ok = None
_announced = False
def video_encode_args(tier=QUALITY):
"""Return the codec/quality args for one encode, honoring FFMPEG_ENCODER."""
global _announced
if tier not in _X264_ARGS:
raise ValueError(f"Unknown encode tier: {tier!r}")
mode = os.environ.get("FFMPEG_ENCODER", "x264").strip().lower()
use_nvenc = False
if mode in ("nvenc", "auto"):
use_nvenc = nvenc_available()
if mode == "nvenc" and not use_nvenc:
print("⚠️ [Encoder] FFMPEG_ENCODER=nvenc but h264_nvenc is not "
"usable here — falling back to libx264")
if not _announced:
_announced = True
print(f"🎞️ [Encoder] video encoder: {'h264_nvenc' if use_nvenc else 'libx264'} "
f"(FFMPEG_ENCODER={mode})")
return list((_NVENC_ARGS if use_nvenc else _X264_ARGS)[tier])