Spaces:
Runtime error
Runtime error
fix: improve HRE spatial editing
Browse filesFiles changed (3)
160 unmodified lines1611621631641651661674 unmodified lines17217317417517617717810 unmodified lines1891901911921931941951961971981992002012022032043 unmodified lines2082092102112122132142152162172182192202212222232242252262272282292307 unmodified lines23823924024124224310 unmodified lines2542552562572582597 unmodified lines2672682692702712722732741 unmodified line276277278279280281282160 unmodified lines}HRE_SEGMENT_PROMPT = """Analyze this video frame for high-retention TikTok editing decisions.Segment {seg_idx} of {n_total}. Transcript: "{context}"4 unmodified lines"face_detected": <true|false>,"face_cx": <0.0-1.0>,"face_cy": <0.0-1.0>,"subtitle_position": "<top|bottom|left|right|center>","subtitle_mode": "<word|phrase|sentence>","subtitle_emphasis": "<pop|punch|calm>","subtitle_color": "<white|yellow|cyan|orange|green>",10 unmodified lines- subtitle WORD: short hooks, reactions, punchlines, important keywords- subtitle PHRASE: fast but understandable speech, 2-4 words at a time- subtitle SENTENCE: explanation, normal conversation, low/medium energy- subtitle TOP: face is in bottom half- subtitle BOTTOM: face is in top half- subtitle LEFT/RIGHT: face or main object is on the opposite side- Avoid choosing the exact same subtitle_position and subtitle_mode for every segment.- face_cx/face_cy: face center as 0.0-1.0 fraction of frame"""def analyze_frame_for_hre(frame_path: "Path",context: str = "",seg_idx: int = 0,n_total: int = 1,3 unmodified linesfrom openai import OpenAIclient = OpenAI(base_url=VLLM_BASE_URL, api_key=VLLM_API_KEY)if not Path(frame_path).exists():return _default_hre_analysis(seg_idx, n_total)b64 = _encode_image(str(frame_path))prompt = HRE_SEGMENT_PROMPT.format(seg_idx=seg_idx, n_total=n_total, context=context[:200])response = client.chat.completions.create(model=VLLM_MODEL,messages=[{"role": "user","content": [{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64}"}},{"type": "text", "text": prompt},],}],max_tokens=200,temperature=0.1,)raw = response.choices[0].message.content.strip()7 unmodified lineslogger.debug(f"HRE seg {seg_idx}/{n_total}: "f"zoom={analysis.get('zoom_direction')}({analysis.get('zoom_speed')}) "f"sub={analysis.get('subtitle_position')}/{analysis.get('subtitle_mode')}/"f"{analysis.get('subtitle_color')} "f"type={analysis.get('moment_type')}"10 unmodified linesreturn _default_hre_analysis(seg_idx, n_total)def _default_hre_analysis(seg_idx: int = 0, n_total: int = 1) -> dict:"""Fallback with varied decisions based on position in clip."""if seg_idx == 0:7 unmodified lines_colors = ["yellow", "white", "cyan", "orange", "white", "yellow"]_positions = ["bottom", "top", "left", "bottom", "right", "top"]_modes = ["word", "sentence", "phrase", "word", "sentence", "phrase"]_emphasis = ["punch", "calm", "pop", "punch", "calm", "pop"]return {"zoom_direction": zoom_dir,1 unmodified line"face_detected": False,"face_cx": 0.5,"face_cy": 0.38,"subtitle_position": _positions[seg_idx % len(_positions)],"subtitle_mode": _modes[seg_idx % len(_modes)],"subtitle_emphasis": _emphasis[seg_idx % len(_emphasis)],"subtitle_color": _colors[seg_idx % len(_colors)],160 unmodified lines1611621631641651661674 unmodified lines17217317417517617717817918018118218318418510 unmodified lines1961971981992002012022032042052062072082092102112122132143 unmodified lines2182192202212222232242252262272282292302312322332342352362372382392402412422432442452467 unmodified lines25425525625725825926010 unmodified lines2712722732742752762772782792802812822832842852867 unmodified lines2942952962972982993003013023033041 unmodified line306307308309310311312313314315316317318319160 unmodified lines}HRE_SEGMENT_PROMPT = """Analyze these video frames for high-retention TikTok editing decisions.Segment {seg_idx} of {n_total}. Transcript: "{context}"4 unmodified lines"face_detected": <true|false>,"face_cx": <0.0-1.0>,"face_cy": <0.0-1.0>,"subject_bbox": [<x1>, <y1>, <x2>, <y2>] or null,"zoom_anchor_x": <0.0-1.0>,"zoom_anchor_y": <0.0-1.0>,"subtitle_position": "<top|bottom|left|right|center>","caption_x": <0.10-0.90>,"caption_y": <0.12-0.88>,"caption_anchor": <1-9>,"caption_max_width_pct": <0.35-0.82>,"subtitle_mode": "<word|phrase|sentence>","subtitle_emphasis": "<pop|punch|calm>","subtitle_color": "<white|yellow|cyan|orange|green>",10 unmodified lines- subtitle WORD: short hooks, reactions, punchlines, important keywords- subtitle PHRASE: fast but understandable speech, 2-4 words at a time- subtitle SENTENCE: explanation, normal conversation, low/medium energy- subject_bbox: main face/person/product/object box in normalized frame coordinates, or null if unclear- zoom_anchor_x/y: center of the face/person/product to keep important content in frame; never choose a blank wall/window- caption_x/y: choose an actually empty readable area in this frame, not just fixed top/bottom- caption_anchor: ASS anchor 1-9 matching caption_x/y (1 bottom-left, 5 center, 9 top-right)- caption_max_width_pct: smaller when the empty space is narrow; captions must stay fully inside the 9:16 frame- Keep captions away from face, product, hands, and important screen/object regions.- Avoid choosing the exact same caption_x/y and subtitle_mode for every segment.- face_cx/face_cy: face center as 0.0-1.0 fraction of frame"""def analyze_frames_for_hre(frame_paths: list["Path"],context: str = "",seg_idx: int = 0,n_total: int = 1,3 unmodified linesfrom openai import OpenAIclient = OpenAI(base_url=VLLM_BASE_URL, api_key=VLLM_API_KEY)valid_frames = [Path(p) for p in frame_paths[:3] if Path(p).exists()]if not valid_frames:return _default_hre_analysis(seg_idx, n_total)content = []for frame_path in valid_frames:b64 = _encode_image(str(frame_path))content.append({"type": "image_url","image_url": {"url": f"data:image/jpeg;base64,{b64}"},})prompt = HRE_SEGMENT_PROMPT.format(seg_idx=seg_idx, n_total=n_total, context=context[:320])content.append({"type": "text", "text": prompt})response = client.chat.completions.create(model=VLLM_MODEL,messages=[{"role": "user","content": content,}],max_tokens=380,temperature=0.1,)raw = response.choices[0].message.content.strip()7 unmodified lineslogger.debug(f"HRE seg {seg_idx}/{n_total}: "f"zoom={analysis.get('zoom_direction')}({analysis.get('zoom_speed')}) "f"caption=({analysis.get('caption_x')},{analysis.get('caption_y')}) "f"sub={analysis.get('subtitle_position')}/{analysis.get('subtitle_mode')}/"f"{analysis.get('subtitle_color')} "f"type={analysis.get('moment_type')}"10 unmodified linesreturn _default_hre_analysis(seg_idx, n_total)def analyze_frame_for_hre(frame_path: "Path",context: str = "",seg_idx: int = 0,n_total: int = 1,) -> dict:"""Backward-compatible wrapper for callers that provide one frame."""return analyze_frames_for_hre([frame_path], context, seg_idx, n_total)def _default_hre_analysis(seg_idx: int = 0, n_total: int = 1) -> dict:"""Fallback with varied decisions based on position in clip."""if seg_idx == 0:7 unmodified lines_colors = ["yellow", "white", "cyan", "orange", "white", "yellow"]_positions = ["bottom", "top", "left", "bottom", "right", "top"]_coords = [(0.50, 0.76), (0.50, 0.18), (0.28, 0.56), (0.50, 0.72), (0.72, 0.56), (0.50, 0.20)]_anchors = [2, 8, 4, 2, 6, 8]_modes = ["word", "sentence", "phrase", "word", "sentence", "phrase"]_emphasis = ["punch", "calm", "pop", "punch", "calm", "pop"]caption_x, caption_y = _coords[seg_idx % len(_coords)]return {"zoom_direction": zoom_dir,1 unmodified line"face_detected": False,"face_cx": 0.5,"face_cy": 0.38,"subject_bbox": None,"zoom_anchor_x": 0.5,"zoom_anchor_y": 0.38,"subtitle_position": _positions[seg_idx % len(_positions)],"caption_x": caption_x,"caption_y": caption_y,"caption_anchor": _anchors[seg_idx % len(_anchors)],"caption_max_width_pct": 0.62,"subtitle_mode": _modes[seg_idx % len(_modes)],"subtitle_emphasis": _emphasis[seg_idx % len(_emphasis)],"subtitle_color": _colors[seg_idx % len(_colors)],
4 unmodified lines567891026 unmodified lines373839404142434445464 unmodified linesfrom loguru import loggerdef extract_clip(video_path: Path,start: float,26 unmodified lineselse:# Crop: scale to 1920 height first, then center-crop to 1080 wide# Optionally center on face_bbox x when availableif face_bbox and len(face_bbox) == 4:x1, _, x2, _ = face_bboxface_cx = int((x1 + x2) / 2)crop = f"scale=-1:1920,crop=1080:1920:max(0\\,min(iw-1080\\,{face_cx}*iw/in_w-540)):0"else:crop = "scale=-1:1920,crop=1080:1920:(iw-1080)/2:0"vf_filters.append(crop)4 unmodified lines5678910111213141516171819202122232425262728293026 unmodified lines5758596061626364654 unmodified linesfrom loguru import loggerdef _face_center_expr(face_bbox: list | None) -> str | None:"""Return a crop expression x-center from Qwen's normalized face bbox."""if not face_bbox or len(face_bbox) != 4:return Nonetry:x1, _, x2, _ = [float(v) for v in face_bbox]except Exception:return None# Qwen prompt asks for normalized percentages. Older comments said pixels,# so keep a conservative pixel fallback, but prefer normalized handling.face_cx = (x1 + x2) / 2.0if max(abs(x1), abs(x2)) <= 1.5:face_cx = min(1.0, max(0.0, face_cx))return f"{face_cx:.4f}*iw-540"if 0 <= face_cx <= 1080:return f"({face_cx:.1f}/1080)*iw-540"return Nonedef extract_clip(video_path: Path,start: float,26 unmodified lineselse:# Crop: scale to 1920 height first, then center-crop to 1080 wide# Optionally center on face_bbox x when availableface_expr = _face_center_expr(face_bbox)if face_expr:crop = f"scale=-1:1920,crop=1080:1920:max(0\\,min(iw-1080\\,{face_expr})):0"else:crop = "scale=-1:1920,crop=1080:1920:(iw-1080)/2:0"vf_filters.append(crop)
124 unmodified lines1251261271281291305 unmodified lines1361371381391401411421431441451461478 unmodified lines1561571581591601611628 unmodified lines1711721731741751761771781798 unmodified lines18818919019119219319419519619719819920063 unmodified lines26426526626726826927027127212 unmodified lines2852862872882892902912922935 unmodified lines2993003013023033043053063073083093103113123133143153169 unmodified lines32632732832933033133211 unmodified lines344345346347348349350351352353354244 unmodified lines599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636124 unmodified linesreturn result.returncode == 0 and out_path.exists()# ─── Per-segment AI analysis ──────────────────────────────────────────────────def _analyze_segment(5 unmodified linesclip_start: float,tmp_dir: Path,) -> dict:from src.analysis.vision import analyze_frame_for_hre, _default_hre_analysismid_t = (seg["start"] + seg["end"]) / 2.0frame_path = tmp_dir / f"seg_{seg_idx:03d}.jpg"if not _extract_frame(video_path, mid_t, frame_path):return _default_hre_analysis(seg_idx, n_total)words_all: list[dict] = []8 unmodified linesif w.get("start", 0) < abs_end and w.get("end", 0) > abs_start).strip()return analyze_frame_for_hre(frame_path, context, seg_idx, n_total)# ─── Zoom expression builders ─────────────────────────────────────────────────8 unmodified lines"""direction = analysis.get("zoom_direction", "in")speed = analysis.get("zoom_speed", "slow")face_detected = bool(analysis.get("face_detected", False))face_cx = float(analysis.get("face_cx") or 0.5)face_cy = float(analysis.get("face_cy") or 0.38)if direction == "in":if speed == "fast":8 unmodified lineselse: # holdz_expr, max_zoom = "1.08", 1.08if face_detected and direction == "in" and max_zoom > 1.05:x_expr = f"max(0\\,min(iw-iw/zoom\\,iw*{face_cx:.3f}-iw/zoom/2))"y_expr = f"max(0\\,min(ih-ih/zoom\\,ih*{face_cy:.3f}-ih/zoom/2))"else:x_expr = "iw/2-(iw/zoom/2)"if direction == "in":y_bias = min(face_cy, 0.5) if face_cy < 0.55 else 0.38y_expr = f"max(0\\,min(ih-ih/zoom\\,ih*{y_bias:.2f}-(ih/zoom/2)))"else:y_expr = "ih/2-(ih/zoom/2)"63 unmodified lines"red": "&H000000FF",}_POSITIONS = {"top", "bottom", "left", "right", "center"}_MODES = {"word", "phrase", "sentence"}_EMPHASIS = {"pop", "punch", "calm"}def _ts(t: float) -> str:12 unmodified linesreturn v if v in allowed else fallbackdef _normalise_analysis(analysis: dict, seg_idx: int, n_total: int) -> dict:"""Validate model output and fill HRE fields used by the renderer."""an = dict(analysis or {})energy = _pick(an.get("energy_level"), {"high", "medium", "low"}, "medium")moment = _pick(an.get("moment_type"),5 unmodified linesif energy == "medium" and moment not in {"context", "transition"}:fallback_mode = "phrase"pos = _pick(an.get("subtitle_position"), _POSITIONS, "bottom")mode = _pick(an.get("subtitle_mode"), _MODES, fallback_mode)emphasis = _pick(an.get("subtitle_emphasis"), _EMPHASIS, "punch" if mode == "word" else "calm")color = _pick(an.get("subtitle_color"), set(_ASS_COLORS), "white")zoom_direction = _pick(an.get("zoom_direction"), {"in", "out", "hold"}, "in")zoom_speed = _pick(an.get("zoom_speed"), {"fast", "slow"}, "slow")try:face_cx = min(1.0, max(0.0, float(an.get("face_cx", 0.5))))face_cy = min(1.0, max(0.0, float(an.get("face_cy", 0.38))))except Exception:face_cx, face_cy = 0.5, 0.38if seg_idx == 0:zoom_direction, zoom_speed = "in", "fast"9 unmodified lines"face_detected": bool(an.get("face_detected", False)),"face_cx": face_cx,"face_cy": face_cy,"subtitle_position": pos,"subtitle_mode": mode,"subtitle_emphasis": emphasis,"subtitle_color": color,11 unmodified lines# If the model repeats the same caption treatment for every segment, rotate# through safe defaults so HRE visibly changes across the clip.if len(plan) > 1 and len({(p["subtitle_position"], p["subtitle_mode"]) for p in plan}) == 1:positions = ["bottom", "top", "left", "bottom", "right", "top"]modes = ["word", "sentence", "phrase", "word", "sentence", "phrase"]for i, p in enumerate(plan):p["subtitle_position"] = positions[i % len(positions)]p["subtitle_mode"] = modes[i % len(modes)]if p["subtitle_mode"] == "word":p["subtitle_emphasis"] = "punch"244 unmodified linesdef _subtitle_tag(plan: dict) -> tuple[str, int]:pos = plan["subtitle_position"]mode = plan["subtitle_mode"]energy = plan["energy_level"]emphasis = plan["subtitle_emphasis"]color = _ASS_COLORS.get(plan["subtitle_color"], "&H00FFFFFF")anchors = {"top": (8, 540, 230),"bottom": (2, 540, 1660),"left": (4, 95, 960),"right": (6, 985, 960),"center": (5, 540, 960),}alignment, x, y = anchors.get(pos, anchors["bottom"])if mode == "sentence":font_size = 66 if energy != "high" else 74max_chars = 34elif mode == "phrase":font_size = 82 if energy != "low" else 76max_chars = 24else:font_size = 102 if energy == "high" else 92max_chars = 18if pos in {"left", "right"}:font_size -= 8max_chars = min(max_chars, 22)base = (f"{{\\an{alignment}\\pos({x},{y})\\1c{color}&\\fs{font_size}""\\b1\\bord5\\shad1\\q2}}")if emphasis in {"pop", "punch"} or mode == "word":base += "{\\fscx125\\fscy125\\t(0,120,\\fscx100\\fscy100)}"124 unmodified lines1251261271281291301311321331341351361371381391401411421431441451461471485 unmodified lines1541551561571581591601611621638 unmodified lines1721731741751761771788 unmodified lines1871881891901911921931948 unmodified lines20320420520620720820921021121221321421563 unmodified lines27928028128228328428528628728812 unmodified lines3013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653665 unmodified lines3723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114129 unmodified lines42242342442542642742842943043143243343443511 unmodified lines447448449450451452453454455456457458459460244 unmodified lines705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734124 unmodified linesreturn result.returncode == 0 and out_path.exists()def _extract_segment_frames(video_path: Path, seg: dict, seg_idx: int, tmp_dir: Path) -> list[Path]:"""Extract a few representative frames so HRE decisions see motion, not one random still."""start = float(seg["start"])end = float(seg["end"])duration = max(0.1, end - start)times = [start + duration * 0.25,start + duration * 0.50,start + duration * 0.75,]frames: list[Path] = []for j, t in enumerate(times):frame_path = tmp_dir / f"seg_{seg_idx:03d}_{j}.jpg"if _extract_frame(video_path, min(max(start, t), max(start, end - 0.05)), frame_path):frames.append(frame_path)return frames# ─── Per-segment AI analysis ──────────────────────────────────────────────────def _analyze_segment(5 unmodified linesclip_start: float,tmp_dir: Path,) -> dict:from src.analysis.vision import analyze_frames_for_hre, _default_hre_analysisframe_paths = _extract_segment_frames(video_path, seg, seg_idx, tmp_dir)if not frame_paths:return _default_hre_analysis(seg_idx, n_total)words_all: list[dict] = []8 unmodified linesif w.get("start", 0) < abs_end and w.get("end", 0) > abs_start).strip()return analyze_frames_for_hre(frame_paths, context, seg_idx, n_total)# ─── Zoom expression builders ─────────────────────────────────────────────────8 unmodified lines"""direction = analysis.get("zoom_direction", "in")speed = analysis.get("zoom_speed", "slow")zoom_anchor_x = _clamp_float(analysis.get("zoom_anchor_x"), _clamp_float(analysis.get("face_cx"), 0.5))zoom_anchor_y = _clamp_float(analysis.get("zoom_anchor_y"), _clamp_float(analysis.get("face_cy"), 0.38))if direction == "in":if speed == "fast":8 unmodified lineselse: # holdz_expr, max_zoom = "1.08", 1.08if direction == "in" and max_zoom > 1.05:x_expr = f"max(0\\,min(iw-iw/zoom\\,iw*{zoom_anchor_x:.3f}-iw/zoom/2))"y_expr = f"max(0\\,min(ih-ih/zoom\\,ih*{zoom_anchor_y:.3f}-ih/zoom/2))"else:x_expr = "iw/2-(iw/zoom/2)"if direction == "in":y_bias = min(zoom_anchor_y, 0.5) if zoom_anchor_y < 0.55 else 0.38y_expr = f"max(0\\,min(ih-ih/zoom\\,ih*{y_bias:.2f}-(ih/zoom/2)))"else:y_expr = "ih/2-(ih/zoom/2)"63 unmodified lines"red": "&H000000FF",}_POSITIONS = {"top", "bottom", "left", "right", "center", "free"}_MODES = {"word", "phrase", "sentence"}_EMPHASIS = {"pop", "punch", "calm"}_ANCHORS = set(range(1, 10))def _ts(t: float) -> str:12 unmodified linesreturn v if v in allowed else fallbackdef _clamp_float(value: object, fallback: float, low: float = 0.0, high: float = 1.0) -> float:try:return min(high, max(low, float(value)))except Exception:return fallbackdef _clamp_int(value: object, fallback: int, allowed: set[int]) -> int:try:v = int(value)except Exception:return fallbackreturn v if v in allowed else fallbackdef _normalise_bbox(value: object) -> list[float] | None:if not isinstance(value, (list, tuple)) or len(value) != 4:return Nonetry:coords = [float(v) for v in value]except Exception:return Noneif max(abs(v) for v in coords) > 1.5:return Nonex1, y1, x2, y2 = coordsx1, x2 = sorted((min(1.0, max(0.0, x1)), min(1.0, max(0.0, x2))))y1, y2 = sorted((min(1.0, max(0.0, y1)), min(1.0, max(0.0, y2))))if x2 - x1 < 0.02 or y2 - y1 < 0.02:return Nonereturn [x1, y1, x2, y2]def _caption_anchor_for(x: float, y: float) -> int:if y < 0.34:return 8 if 0.30 <= x <= 0.70 else 7 if x < 0.5 else 9if y > 0.66:return 2 if 0.30 <= x <= 0.70 else 1 if x < 0.5 else 3return 5 if 0.34 <= x <= 0.66 else 4 if x < 0.5 else 6def _safe_caption_point(subject_x: float, subject_y: float, seg_idx: int) -> tuple[float, float, int]:"""Pick a varied but readable empty-ish zone opposite the main subject."""left_side = subject_x < 0.50high_subject = subject_y < 0.42low_subject = subject_y > 0.62candidates = [(0.68 if left_side else 0.32, 0.72 if high_subject else 0.24 if low_subject else 0.76),(0.72 if left_side else 0.28, 0.50),(0.50, 0.18 if subject_y > 0.45 else 0.82),(0.50, 0.72),]x, y = candidates[seg_idx % len(candidates)]return x, y, _caption_anchor_for(x, y)def _normalise_analysis(analysis: dict, seg_idx: int, n_total: int) -> dict:"""Validate model output and fill HRE fields used by the renderer."""an = dict(analysis or {})subject_bbox = _normalise_bbox(an.get("subject_bbox"))energy = _pick(an.get("energy_level"), {"high", "medium", "low"}, "medium")moment = _pick(an.get("moment_type"),5 unmodified linesif energy == "medium" and moment not in {"context", "transition"}:fallback_mode = "phrase"if subject_bbox:subject_x = (subject_bbox[0] + subject_bbox[2]) / 2.0subject_y = (subject_bbox[1] + subject_bbox[3]) / 2.0else:subject_x = _clamp_float(an.get("face_cx"), 0.5)subject_y = _clamp_float(an.get("face_cy"), 0.38)pos = _pick(an.get("subtitle_position"), _POSITIONS, "free")mode = _pick(an.get("subtitle_mode"), _MODES, fallback_mode)emphasis = _pick(an.get("subtitle_emphasis"), _EMPHASIS, "punch" if mode == "word" else "calm")color = _pick(an.get("subtitle_color"), set(_ASS_COLORS), "white")zoom_direction = _pick(an.get("zoom_direction"), {"in", "out", "hold"}, "in")zoom_speed = _pick(an.get("zoom_speed"), {"fast", "slow"}, "slow")face_cx = _clamp_float(an.get("face_cx"), subject_x)face_cy = _clamp_float(an.get("face_cy"), subject_y)zoom_anchor_x = _clamp_float(an.get("zoom_anchor_x"), face_cx)zoom_anchor_y = _clamp_float(an.get("zoom_anchor_y"), face_cy)fallback_x, fallback_y, fallback_anchor = _safe_caption_point(subject_x, subject_y, seg_idx)caption_x = _clamp_float(an.get("caption_x"), fallback_x, 0.10, 0.90)caption_y = _clamp_float(an.get("caption_y"), fallback_y, 0.12, 0.88)caption_anchor = _clamp_int(an.get("caption_anchor"), fallback_anchor, _ANCHORS)caption_max_width_pct = _clamp_float(an.get("caption_max_width_pct"),0.58 if mode != "sentence" else 0.72,0.35,0.82,)if subject_bbox:x1, y1, x2, y2 = subject_bboxoverlaps_subject = (x1 - 0.08) <= caption_x <= (x2 + 0.08) and (y1 - 0.08) <= caption_y <= (y2 + 0.08)if overlaps_subject:caption_x, caption_y, caption_anchor = fallback_x, fallback_y, fallback_anchorif seg_idx == 0:zoom_direction, zoom_speed = "in", "fast"9 unmodified lines"face_detected": bool(an.get("face_detected", False)),"face_cx": face_cx,"face_cy": face_cy,"subject_bbox": subject_bbox,"zoom_anchor_x": zoom_anchor_x,"zoom_anchor_y": zoom_anchor_y,"subtitle_position": pos,"caption_x": caption_x,"caption_y": caption_y,"caption_anchor": caption_anchor,"caption_max_width_pct": caption_max_width_pct,"subtitle_mode": mode,"subtitle_emphasis": emphasis,"subtitle_color": color,11 unmodified lines# If the model repeats the same caption treatment for every segment, rotate# through safe defaults so HRE visibly changes across the clip.if len(plan) > 1 and len({(round(p["caption_x"], 2), round(p["caption_y"], 2), p["subtitle_mode"]) for p in plan}) == 1:positions = ["free", "free", "free", "free", "free", "free"]coords = [(0.50, 0.76), (0.50, 0.18), (0.28, 0.56), (0.72, 0.52), (0.50, 0.82), (0.50, 0.22)]modes = ["word", "sentence", "phrase", "word", "sentence", "phrase"]for i, p in enumerate(plan):p["subtitle_position"] = positions[i % len(positions)]p["caption_x"], p["caption_y"] = coords[i % len(coords)]p["caption_anchor"] = _caption_anchor_for(p["caption_x"], p["caption_y"])p["subtitle_mode"] = modes[i % len(modes)]if p["subtitle_mode"] == "word":p["subtitle_emphasis"] = "punch"244 unmodified linesdef _subtitle_tag(plan: dict) -> tuple[str, int]:mode = plan["subtitle_mode"]energy = plan["energy_level"]emphasis = plan["subtitle_emphasis"]color = _ASS_COLORS.get(plan["subtitle_color"], "&H00FFFFFF")alignment = int(plan.get("caption_anchor", 5))x = round(_clamp_float(plan.get("caption_x"), 0.5, 0.08, 0.92) * 1080)y = round(_clamp_float(plan.get("caption_y"), 0.75, 0.10, 0.90) * 1920)max_width_px = max(360, min(886, int(_clamp_float(plan.get("caption_max_width_pct"), 0.62, 0.35, 0.82) * 1080)))if mode == "sentence":font_size = 56 if energy != "high" else 62elif mode == "phrase":font_size = 68 if energy != "low" else 62else:font_size = 80 if energy == "high" else 72if alignment in {4, 5, 6}:font_size = max(54, font_size - 4)max_chars = max(8, min(34, int(max_width_px / (font_size * 0.58))))base = (f"{{\\an{alignment}\\pos({x},{y})\\1c{color}&\\fs{font_size}""\\b1\\bord5\\shad1\\q2}")if emphasis in {"pop", "punch"} or mode == "word":base += "{\\fscx125\\fscy125\\t(0,120,\\fscx100\\fscy100)}"
backend/src/analysis/vision.py+52-15
backend/src/processing/clip_extractor.py+23-4
backend/src/processing/high_retention.py+140-42