clipwright/layout_picker.py
victormanuel.caverogracia 90b10c1902 perf(render): burn the watermark in the reframe encode; read AV1 sources
- The free-plan watermark (~97% of jobs) rode a pass of its own: decode
  the finished clip, overlay, encode it all again. It now goes into each
  reframe segment's own encode (reframe_v2.render(watermark=True), same
  geometry via main.watermark_filter). One NVENC encode less per clip; the
  v1 fallback and horizontal clips still use apply_watermark. The editor's
  paths (recut.py) are untouched.
- layout_picker.sample_frames falls back to the ffmpeg CLI when OpenCV
  reads no frame: OpenCV's bundled FFmpeg has no AV1 software decoder, so
  AV1 YouTube sources always got the default layout. H.264 sources return
  byte-identical frames (same JPEG MD5).

Bench on balrog, 3 videos, old and new run side by side: job CPU -27 to
-43%, wall -6 to -12%; clips SSIM 0.990-0.999 vs before (one generation
fewer), audio identical, watermark position checked by eye.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 19:47:34 +02:00

245 lines
9.4 KiB
Python

"""Ask Gemini which layout a video needs, once per video.
The four previous attempts at this problem asked the model (or a pixel
heuristic) to MEASURE something — edge density, MSER text density, what fraction
of the duration had content on screen, what fraction of the width it spanned —
and let thresholds turn that number into a routing decision. All four failed the
same way: they could not separate a spreadsheet from a corner scoreboard.
This asks for the decision itself. Measured over the 48-clip corpus against
hand-checked labels (`labels-layout.json` in the reframe-testing skill), three
runs:
run 1 45/48 94% 18/20 content found 1 false positive of 28
run 2 44/48 92% 17/20 1 of 28
run 3 46/48 96% 18/20 0 of 28
Two clips out of 48 changed answer between runs. That matters because the
earlier note in this repo said Gemini was too non-deterministic to build on —
the same video scored 1% and 97% coverage on consecutive runs. The variance was
in asking for a continuous measurement, not in the model: a categorical choice
between closed options is stable.
It samples FRAMES instead of uploading the video. Gemini bills video at ~300
tokens per second, so an hour of source is ~1.08M tokens — past a 1M context
window before it starts — and means pushing a 1-2GB upload to get back one word.
This pipeline ingests hour-long podcasts, so that scales badly in exactly the
normal case. Twelve stills cost ~3k tokens whatever the source runs to.
Measured on the same 48 clips (whole video vs sampled frames):
whole video 94% / 92% / 96% 18,17,18 of 20 found
12 frames @ 640px 90% / 90% 15,16 of 20
12 frames @ 1024px 92% 17 of 20
24 frames @ 1024px 90% 16 of 20
Resolution was the gap, not frame count: at 640px a spreadsheet is unreadable,
and doubling the frames made it slightly worse rather than better. At 1024px the
difference from sending the whole video sits inside the run-to-run variance the
whole-video mode already has, at 2.2s per clip instead of ~15s.
Off by default (``AUTO_LAYOUT=1``). A caller that already switched layouts on
by hand wins: this only ever ADDS, so an explicit choice is never overridden.
"""
import json
import os
# AUTO_LAYOUT=1 decides and applies. AUTO_LAYOUT=shadow decides, logs, and
# applies NOTHING: the render is byte-for-byte what it would have been.
#
# Shadow exists because everything measured about this picker was measured on 48
# YouTube clips chosen by hand, and the material users actually upload is a
# different distribution nobody has looked at. A week of shadow answers "what
# does it say about OUR videos" for 0.002 USD and ~2s per video, with no way to
# damage a clip somebody paid for.
_MODE = os.environ.get("AUTO_LAYOUT", "0").strip().lower()
SHADOW = _MODE == "shadow"
ENABLED = _MODE == "1" or SHADOW
# 12 frames at 1024px wide. Both numbers are measured, not guessed: see above.
SAMPLE_FRAMES = int(os.environ.get("LAYOUT_SAMPLE_FRAMES", "12"))
SAMPLE_WIDTH = int(os.environ.get("LAYOUT_SAMPLE_WIDTH", "1024"))
# What each decision turns on. Keys match the layout names in the prompt.
DECISION_FLAGS = {
"none": [],
"screencast": ["screencast_layout"],
"split": ["split_layout", "active_speaker"],
}
VALID = set(DECISION_FLAGS)
def _module_flags(decision):
"""Modules to enable for a decision, ignoring anything unrecognised."""
return DECISION_FLAGS.get(str(decision or "none").strip().lower(), [])
def apply(decision):
"""Switch on the modules a decision needs. Returns the modules touched.
Deliberately additive: an operator who set SPLIT_LAYOUT=1 for a job wants
stacking regardless of what the model thinks, and a model that says "none"
must not quietly undo that.
"""
import active_speaker
import screencast_layout
import split_layout
modules = {"split_layout": split_layout,
"screencast_layout": screencast_layout,
"active_speaker": active_speaker}
touched = []
for name in _module_flags(decision):
module = modules.get(name)
if module is not None and not getattr(module, "ENABLED", False):
module.ENABLED = True
touched.append(name)
return touched
def _encode_frame(frame, width):
import cv2
h, w = frame.shape[:2]
scaled = cv2.resize(frame, (width, max(2, int(h * width / w))),
interpolation=cv2.INTER_AREA)
ok, buf = cv2.imencode(".jpg", scaled, [cv2.IMWRITE_JPEG_QUALITY, 80])
return buf.tobytes() if ok else None
def _ffmpeg_frames(video_path, n):
"""The same evenly spread frames, decoded by the ffmpeg CLI.
OpenCV's bundled FFmpeg has no software AV1 decoder, so on the AV1 sources
YouTube often serves every read failed ("Failed to get pixel format") and
the picker fell back to the default layout for the whole video (prod,
25-sep-2026). The system ffmpeg decodes AV1 with libdav1d.
"""
import subprocess
import cv2
import numpy as np
try:
probe = subprocess.run(
["ffprobe", "-v", "error", "-show_entries", "format=duration",
"-of", "default=nw=1:nk=1", video_path],
capture_output=True, text=True, timeout=60)
duration = float(probe.stdout.strip())
except (OSError, ValueError, subprocess.SubprocessError):
return []
frames = []
for i in range(n):
try:
r = subprocess.run(
["ffmpeg", "-v", "error", "-ss", f"{i * duration / n:.3f}",
"-i", video_path, "-frames:v", "1", "-c:v", "png",
"-f", "image2pipe", "-"],
capture_output=True, timeout=120)
except (OSError, subprocess.SubprocessError):
continue
frame = cv2.imdecode(np.frombuffer(r.stdout, np.uint8), cv2.IMREAD_COLOR)
if frame is not None:
frames.append(frame)
return frames
def sample_frames(video_path, n=None, width=None):
"""JPEG bytes for ``n`` frames spread evenly across the video."""
import cv2
n = n or SAMPLE_FRAMES
width = width or SAMPLE_WIDTH
cap = cv2.VideoCapture(video_path)
total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
out = []
try:
if total > 0:
for i in range(n):
cap.set(cv2.CAP_PROP_POS_FRAMES, int(i * total / n))
ok, frame = cap.read()
if not ok:
continue
jpg = _encode_frame(frame, width)
if jpg:
out.append(jpg)
finally:
cap.release()
if not out and os.path.exists(video_path):
out = [j for j in (_encode_frame(f, width) for f in _ffmpeg_frames(video_path, n)) if j]
return out
def pick(video_path, video_duration):
"""The layout Gemini picks for this video, or "none" on any failure.
Never raises: a missing answer has to degrade to today's routing rather
than break the job.
"""
if not ENABLED:
return "none"
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
return "none"
model_name = os.environ.get("GEMINI_MODEL") or 'gemini-3.1-flash-lite'
print("🎛️ Choosing a layout for this video…")
try:
# Inside the try on purpose: the contract above is that this never
# raises, and an unimportable SDK is just one more reason to fall back.
from google import genai
from google.genai import types as genai_types
import gemini_worker
frames = sample_frames(video_path)
if not frames:
print(" ⚠️ No readable frames — keeping the default layout.")
return "none"
client = genai.Client(api_key=api_key)
parts = [genai_types.Part.from_bytes(data=b, mime_type="image/jpeg")
for b in frames]
response = client.models.generate_content(
model=model_name,
contents=parts + [gemini_worker.LAYOUT_CHOICE_PROMPT],
config=genai_types.GenerateContentConfig(
response_mime_type="application/json",
response_schema=gemini_worker.LayoutChoice,
))
gemini_worker.raise_if_blocked(response)
answer = json.loads(response.text) or {}
except Exception as e:
print(f" ⚠️ Layout choice failed ({e}) — keeping the default layout.")
return "none"
decision = str(answer.get("layout", "none")).strip().lower()
if decision not in VALID:
print(f" ⚠️ Unknown layout '{decision}' — keeping the default layout.")
return "none"
why = str(answer.get("why", ""))[:80]
confidence = answer.get("confidence")
print(f" 🎬 Layout: {decision} (confianza {confidence}) — {why}")
return decision
def pick_and_apply(video_path, video_duration):
"""Decide, switch on (unless shadowing), report what changed."""
decision = pick(video_path, video_duration)
if SHADOW:
# One greppable line per job. Deliberately not routed through the
# analytics module: that one is opt-in and host-scoped, and a shadow
# run has to work on any deployment, including self-hosted.
would = _module_flags(decision)
print(f"[layout-shadow] decision={decision} "
f"would_enable={','.join(would) if would else 'none'} "
f"duration={video_duration:.0f}s")
return decision
touched = apply(decision)
if touched:
print(f" ✅ Enabled: {', '.join(touched)}")
return decision