An audit of 45 real user clips pulled from R2 on 25-jul-2026 found 86% below 1080 wide, 80% of them exactly 406x720 — well under what TikTok, Reels and Shorts expect, so the platforms re-encode an already soft image. Two causes, one on top of the other: - The download capped format selection at 720p whenever PROXY_URL was set. The cap exists to control paid-proxy bandwidth, but it was decided once from `_proxy` rather than per attempt, so it also capped the DIRECT attempt — the one that costs nothing and, with DIRECT_FIRST=1, serves most downloads. It is now chosen per attempt: direct pulls up to 1080p, proxied stays at 720p. - The reframe set out_h = orig_h, so a 720p source could only ever produce a 406x720 crop. `delivery_size()` now upscales to a 1080-wide floor and leaves anything already above it untouched — a 4K source still delivers 1216x2160, so no one loses resolution they supplied. Applied to the v1 fallback too: an emergency render is still the clip the user posts. Its frame loop already resizes to the target dims, so only the target changed. Crop geometry is unaffected — SmoothedCameraman derives the crop from the source dimensions and never reads the output pair, which the call site now documents. Verified end to end: a 1280x720 source that previously rendered 406x720 now renders 1080x1920, correct aspect, no distortion.
279 lines
11 KiB
Python
279 lines
11 KiB
Python
"""Reframe engine v2: analyze in Python, render natively in ffmpeg.
|
|
|
|
v1 decodes every frame at full resolution in OpenCV, crops/resizes in numpy
|
|
and pipes raw frames back into ffmpeg. v2 splits that into:
|
|
|
|
1. ANALYSIS — one ffmpeg-decoded pass at <=640px feeding the same detectors
|
|
and the same SmoothedCameraman/SpeakerTracker state machines as v1, so
|
|
the resulting camera trajectory (crop x per frame) is equivalent.
|
|
2. RENDER — one ffmpeg process per scene doing decode -> dynamic crop
|
|
(sendcmd) -> scale -> encode natively (TRACK scenes), or the blurred
|
|
background filtergraph (GENERAL scenes); segments are then concatenated
|
|
with stream copy and the audio mapped straight from the source clip.
|
|
|
|
No raw-frame piping, no second full-res decode, one less intermediate encode.
|
|
Callers must treat any exception as "fall back to the v1 loop".
|
|
|
|
Pure helpers (sendcmd/concat generation, scene slicing) have no heavy imports
|
|
so they stay unit-testable in CI.
|
|
"""
|
|
import os
|
|
import subprocess
|
|
import tempfile
|
|
|
|
from ffmpeg_utils import video_encode_args, QUALITY_FAST, METADATA_SCRUB
|
|
|
|
ANALYSIS_MAX_WIDTH = 640
|
|
|
|
|
|
# Short-form platforms (TikTok / Reels / Shorts) expect a 1080-wide vertical
|
|
# upload; anything smaller is treated as low quality and re-encoded from the
|
|
# already-soft source. The crop region is whatever the source height allows, so
|
|
# a 720p input yields a 406x720 crop — we scale that up to the delivery floor
|
|
# rather than shipping sub-HD. Sources that already exceed it are left alone
|
|
# (never downscale quality the user supplied).
|
|
DELIVERY_MIN_WIDTH = 1080
|
|
|
|
|
|
# --- pure helpers (CI-testable) --------------------------------------------
|
|
|
|
def delivery_size(orig_w, orig_h, aspect_ratio):
|
|
"""Output (width, height) for a reframe of this source.
|
|
|
|
Picks the largest crop the source allows, then upscales to
|
|
``DELIVERY_MIN_WIDTH`` if that crop is narrower. Both dimensions come back
|
|
even (x264/NVENC reject odd ones).
|
|
"""
|
|
out_h = orig_h
|
|
out_w = int(out_h * aspect_ratio)
|
|
if out_w > orig_w:
|
|
out_w = orig_w
|
|
out_h = int(out_w / aspect_ratio)
|
|
|
|
if out_w < DELIVERY_MIN_WIDTH:
|
|
out_w = DELIVERY_MIN_WIDTH
|
|
out_h = int(round(out_w / aspect_ratio))
|
|
|
|
return out_w + (out_w % 2), out_h + (out_h % 2)
|
|
|
|
|
|
def dedupe_sendcmd_lines(xs, fps, target="crop@c"):
|
|
"""sendcmd lines setting crop x per frame, deduped to change-points.
|
|
|
|
Timestamps are relative to the segment (the render seeks per scene).
|
|
"""
|
|
lines = []
|
|
prev = None
|
|
for i, x in enumerate(xs):
|
|
if x != prev:
|
|
lines.append(f"{i / fps:.4f} {target} x {x};")
|
|
prev = x
|
|
return lines
|
|
|
|
|
|
def scene_frame_ranges(scene_boundaries, strategies, total_frames):
|
|
"""Clamp scene (start, end) frame ranges to the decoded frame count,
|
|
dropping empty ranges. Each range keeps its strategy so later indices
|
|
can't misalign when a range is dropped."""
|
|
ranges = []
|
|
for i, (start_f, end_f) in enumerate(scene_boundaries):
|
|
strategy = strategies[i] if i < len(strategies) else 'TRACK'
|
|
start_f = max(0, min(start_f, total_frames))
|
|
end_f = max(start_f, min(end_f, total_frames))
|
|
if end_f > start_f:
|
|
ranges.append((start_f, end_f, strategy))
|
|
return ranges
|
|
|
|
|
|
def concat_list_content(segment_paths):
|
|
# Single quotes per concat-demuxer spec; our paths are tempfile-generated
|
|
# (no quotes in them).
|
|
return "".join(f"file '{p}'\n" for p in segment_paths)
|
|
|
|
|
|
def general_filtergraph(out_w, out_h):
|
|
"""Blurred-background 'general shot' layout, mirroring v1's
|
|
create_general_frame: bg fills height (center-cropped, blurred), fg fits
|
|
width, centered vertically."""
|
|
return (
|
|
f"[0:v]split=2[bga][fga];"
|
|
f"[bga]scale=-2:{out_h},crop=w=min(iw\\,{out_w}):h={out_h},"
|
|
f"scale={out_w}:{out_h},gblur=sigma=12[bg];"
|
|
f"[fga]scale={out_w}:-2[fg];"
|
|
f"[bg][fg]overlay=x=(W-w)/2:y=(H-h)/2,setsar=1[v]"
|
|
)
|
|
|
|
|
|
# --- analysis ---------------------------------------------------------------
|
|
|
|
def _analyze_trajectory(input_video, scenes_boundaries, scene_strategies,
|
|
fps, orig_w, orig_h, cameraman, tracker):
|
|
"""Replays v1's per-frame decision loop on a downscaled ffmpeg-decoded
|
|
stream. Returns xs: crop x per frame (None on GENERAL frames)."""
|
|
import numpy as np
|
|
import main as m
|
|
|
|
small_w = min(ANALYSIS_MAX_WIDTH, orig_w)
|
|
if small_w % 2:
|
|
small_w -= 1
|
|
small_h = max(int(orig_h * small_w / orig_w), 2)
|
|
if small_h % 2:
|
|
small_h += 1
|
|
scale = orig_w / small_w
|
|
frame_bytes = small_w * small_h * 3
|
|
|
|
proc = subprocess.Popen(
|
|
["ffmpeg", "-loglevel", "error", "-i", input_video,
|
|
"-vf", f"scale={small_w}:{small_h}",
|
|
"-f", "rawvideo", "-pix_fmt", "bgr24", "-"],
|
|
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, bufsize=frame_bytes * 4)
|
|
|
|
xs = []
|
|
frame_number = 0
|
|
current_scene_index = 0
|
|
try:
|
|
while True:
|
|
buf = proc.stdout.read(frame_bytes)
|
|
if len(buf) < frame_bytes:
|
|
break
|
|
frame = np.frombuffer(buf, dtype=np.uint8).reshape((small_h, small_w, 3))
|
|
|
|
if current_scene_index < len(scenes_boundaries):
|
|
start_f, end_f = scenes_boundaries[current_scene_index]
|
|
if frame_number >= end_f and current_scene_index < len(scenes_boundaries) - 1:
|
|
current_scene_index += 1
|
|
|
|
strategy = (scene_strategies[current_scene_index]
|
|
if current_scene_index < len(scene_strategies) else 'TRACK')
|
|
|
|
if strategy == 'GENERAL':
|
|
cameraman.current_center_x = orig_w / 2
|
|
cameraman.target_center_x = orig_w / 2
|
|
xs.append(None)
|
|
else:
|
|
if frame_number % m.DETECT_STRIDE == 0:
|
|
candidates = m.detect_face_candidates(frame)
|
|
for cand in candidates:
|
|
cand['box'] = [int(v * scale) for v in cand['box']]
|
|
cand['score'] = cand['box'][2] * cand['box'][3]
|
|
target_box = tracker.get_target(candidates, frame_number, orig_w)
|
|
if target_box:
|
|
cameraman.update_target(target_box)
|
|
elif frame_number % m.YOLO_FALLBACK_STRIDE == 0:
|
|
person_box = m.detect_person_yolo(frame)
|
|
if person_box:
|
|
cameraman.update_target([int(v * scale) for v in person_box])
|
|
|
|
is_scene_start = (
|
|
current_scene_index < len(scenes_boundaries)
|
|
and frame_number == scenes_boundaries[current_scene_index][0])
|
|
x1, _y1, _x2, _y2 = cameraman.get_crop_box(force_snap=is_scene_start)
|
|
xs.append(x1)
|
|
|
|
frame_number += 1
|
|
finally:
|
|
proc.stdout.close()
|
|
proc.wait()
|
|
|
|
return xs
|
|
|
|
|
|
# --- render -----------------------------------------------------------------
|
|
|
|
def _run(cmd):
|
|
subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.PIPE, timeout=1800)
|
|
|
|
|
|
def render(input_video, final_output_video, aspect_ratio):
|
|
"""Full v2 reframe of one clip. Raises on failure (caller falls back)."""
|
|
import main as m
|
|
|
|
print(" 🚀 Reframe engine v2 (ffmpeg-native render)")
|
|
scenes, fps = m.detect_scenes(input_video)
|
|
fps = float(fps) # PySceneDetect can hand back a Fraction
|
|
orig_w, orig_h = m.get_video_resolution(input_video)
|
|
|
|
out_w, out_h = delivery_size(orig_w, orig_h, aspect_ratio)
|
|
|
|
if not scenes:
|
|
import cv2
|
|
cap = cv2.VideoCapture(input_video)
|
|
total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
|
cap.release()
|
|
from scenedetect import FrameTimecode
|
|
scenes = [(FrameTimecode(0, fps), FrameTimecode(total, fps))]
|
|
|
|
scene_boundaries = [(s.get_frames(), e.get_frames()) for s, e in scenes]
|
|
strategies = m.analyze_scenes_strategy(input_video, scenes)
|
|
|
|
# The crop geometry comes from the SOURCE dims only — SmoothedCameraman
|
|
# derives crop_width/crop_height from video_width/video_height and never
|
|
# reads the output pair. So out_w/out_h being the (possibly upscaled)
|
|
# delivery size doesn't move the camera; only the final scale= uses it.
|
|
cameraman = m.SmoothedCameraman(out_w, out_h, orig_w, orig_h, aspect_ratio=aspect_ratio)
|
|
tracker = m.SpeakerTracker(cooldown_frames=30)
|
|
|
|
xs = _analyze_trajectory(input_video, scene_boundaries, strategies, fps,
|
|
orig_w, orig_h, cameraman, tracker)
|
|
if not xs:
|
|
raise RuntimeError("analysis produced no frames")
|
|
|
|
ranges = scene_frame_ranges(scene_boundaries, strategies, len(xs))
|
|
if not ranges:
|
|
raise RuntimeError("no usable scene ranges")
|
|
|
|
crop_w, crop_h = cameraman.crop_width, cameraman.crop_height
|
|
workdir = tempfile.mkdtemp(prefix="reframe_v2_")
|
|
segments = []
|
|
try:
|
|
for idx, (start_f, end_f, strategy) in enumerate(ranges):
|
|
seg_path = os.path.join(workdir, f"seg_{idx:03d}.mp4")
|
|
ss = start_f / fps
|
|
dur = (end_f - start_f) / fps
|
|
|
|
if strategy == 'GENERAL':
|
|
graph = general_filtergraph(out_w, out_h)
|
|
else:
|
|
seg_xs = [x if x is not None else 0 for x in xs[start_f:end_f]]
|
|
cmd_path = os.path.join(workdir, f"cmd_{idx:03d}.txt")
|
|
with open(cmd_path, "w") as f:
|
|
f.write("\n".join(dedupe_sendcmd_lines(seg_xs, fps)) + "\n")
|
|
graph = (
|
|
f"[0:v]sendcmd=f='{cmd_path}',"
|
|
f"crop@c=w={crop_w}:h={crop_h}:x={seg_xs[0]}:y=0,"
|
|
f"scale={out_w}:{out_h},setsar=1[v]"
|
|
)
|
|
|
|
_run([
|
|
"ffmpeg", "-y", "-loglevel", "error",
|
|
"-ss", f"{ss:.4f}", "-t", f"{dur:.4f}", "-i", input_video,
|
|
"-filter_complex", graph, "-map", "[v]",
|
|
*video_encode_args(QUALITY_FAST), "-an", seg_path,
|
|
])
|
|
segments.append(seg_path)
|
|
|
|
list_path = os.path.join(workdir, "concat.txt")
|
|
with open(list_path, "w") as f:
|
|
f.write(concat_list_content(segments))
|
|
|
|
# Concat video segments (stream copy) + audio straight from the clip.
|
|
_run([
|
|
"ffmpeg", "-y", "-loglevel", "error",
|
|
"-f", "concat", "-safe", "0", "-i", list_path,
|
|
"-i", input_video,
|
|
"-map", "0:v:0", "-map", "1:a:0?",
|
|
"-c:v", "copy", "-c:a", "copy", *METADATA_SCRUB,
|
|
# +faststart moves the moov atom to the front so the browser <video>
|
|
# can start playing before the whole file downloads. Without it the
|
|
# in-app preview spins forever (download still works) — the moov
|
|
# lands at the end of a plain concat.
|
|
"-movflags", "+faststart",
|
|
final_output_video,
|
|
])
|
|
finally:
|
|
import shutil
|
|
shutil.rmtree(workdir, ignore_errors=True)
|
|
|
|
print(f" ✅ Clip saved to {final_output_video}")
|
|
return True
|