Spaces:
Runtime error
Runtime error
fix: add per-segment HRE edit plans
Browse filesFiles changed (8)
95 unmodified lines9697989910010110210310410510632 unmodified lines13914014114214314414514614714814995 unmodified lines│ ││ Normal Mode HRE (High-Retention Editing) ││ ───────────── ────────────────────────────── ││ • pysubs2 ASS • Silence removal (ffmpeg) ││ • User style config • Auto-zoom to face (zoompan) ││ • Font/color/animation • Jump cuts at boundaries ││ • Karaoke/pop/fade • Qwen2.5-VL emoji selection ││ • AMD AMF encode • Impact bold captions │└──────────────────────────────────────────────────────────────────┘│▼32 unmodified lines### High-Retention Editing (HRE)AI chooses everything:- Silence removal (`ffmpeg silenceremove`)- Auto-zoom to face region (`ffmpeg zoompan` using Qwen2.5-VL face_bbox)- Jump cuts at scene boundaries- Qwen2.5-VL selects contextually-appropriate emoji overlay- Impact 64px bold white captions, word-by-word, pop animation---95 unmodified lines9697989910010110210310410510632 unmodified lines13914014114214314414514614714814915095 unmodified lines│ ││ Normal Mode HRE (High-Retention Editing) ││ ───────────── ────────────────────────────── ││ • pysubs2 ASS • Per-segment AI edit plan ││ • User style config • Auto-zoom per segment (zoompan) ││ • Font/color/animation • Word / phrase / sentence captions ││ • Karaoke/pop/fade • Top / bottom / left / right captions ││ • AMD AMF encode • Qwen2.5-VL emoji selection │└──────────────────────────────────────────────────────────────────┘│▼32 unmodified lines### High-Retention Editing (HRE)AI chooses everything:- A per-segment edit plan with timestamps- Auto-zoom direction and speed per segment (`ffmpeg zoompan`)- Caption mode per segment: word, phrase, or sentence- Caption placement per segment: top, bottom, left, right, or center- Caption color, size, and pop emphasis based on segment energy- Qwen2.5-VL selects contextually-appropriate emoji overlay---
171 unmodified lines1721731741751761771785 unmodified lines1841851861871881891901914 unmodified lines19619719819920020120224 unmodified lines22722822923023123223323423523623719 unmodified lines2572582592602612622632642 unmodified lines267268269270271272171 unmodified lines"face_detected": <true|false>,"face_cx": <0.0-1.0>,"face_cy": <0.0-1.0>,"subtitle_position": "<top|bottom>","subtitle_color": "<white|yellow|cyan|orange|green>","energy_level": "<high|medium|low>","moment_type": "<hook|punchline|context|reaction|transition>"5 unmodified lines- zoom IN slow: context, buildup, moderate energy- zoom OUT: reveals, breathing room after intensity- HOLD: stable content, text-heavy moments- subtitle TOP: face is in bottom half → put text at top- subtitle BOTTOM: face is in top half → text at bottom- face_cx/face_cy: face center as 0.0-1.0 fraction of frame"""4 unmodified linesseg_idx: int = 0,n_total: int = 1,) -> dict:"""Per-segment HRE: zoom direction, subtitle position+color for this moment."""try:from openai import OpenAI24 unmodified linesif raw.startswith("json"):raw = raw[4:]analysis = json.loads(raw.strip())logger.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_color')} "f"type={analysis.get('moment_type')}")try:19 unmodified lineselse:zoom_dir, zoom_speed, moment = "in", "slow", "reaction"_colors = ["yellow", "white", "cyan", "orange", "white", "yellow"]_positions = ["bottom", "top", "bottom", "top", "bottom", "top"]return {"zoom_direction": zoom_dir,2 unmodified lines"face_cx": 0.5,"face_cy": 0.38,"subtitle_position": _positions[seg_idx % len(_positions)],"subtitle_color": _colors[seg_idx % len(_colors)],"energy_level": "medium","moment_type": moment,171 unmodified lines1721731741751761771781791805 unmodified lines1861871881891901911921931941951961971984 unmodified lines20320420520620720820924 unmodified lines23423523623723823924024124224324424519 unmodified lines2652662672682692702712722732742 unmodified lines277278279280281282283284171 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>","energy_level": "<high|medium|low>","moment_type": "<hook|punchline|context|reaction|transition>"5 unmodified lines- zoom IN slow: context, buildup, moderate energy- zoom OUT: reveals, breathing room after intensity- HOLD: stable content, text-heavy moments- 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"""4 unmodified linesseg_idx: int = 0,n_total: int = 1,) -> dict:"""Per-segment HRE: zoom, caption placement, caption mode, and color."""try:from openai import OpenAI24 unmodified linesif raw.startswith("json"):raw = raw[4:]analysis = {**_default_hre_analysis(seg_idx, n_total), **json.loads(raw.strip())}logger.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')}")try:19 unmodified lineselse:zoom_dir, zoom_speed, moment = "in", "slow", "reaction"_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,2 unmodified lines"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)],"energy_level": "medium","moment_type": moment,
123456789101112131415159 unmodified lines17517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921 unmodified lines23123223323423523623723823924029 unmodified lines2702712722732742752762772782792802813 unmodified lines2852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253267 unmodified lines33433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836967 unmodified lines43743843944044144244320 unmodified lines464465466467468469470471472473474475476477478479480481482483484485486487488489"""High-Retention Editing pipeline — per-segment AI decisions.Each 3-5s segment gets its own zoom direction, subtitle position,and caption color driven by Qwen2.5-VL analyzing one frame per segment.Pipeline per clip:1. Segment clip at speech pauses (3-5s chunks)2. Extract midpoint frame from each segment3. Qwen2.5-VL analyzes each frame → zoom + subtitle decisions4. ffmpeg filter_complex: per-segment zoompan + concat5. ASS subtitles with per-segment alignment/color/size override tags"""import subprocessimport tempfilefrom pathlib import Path159 unmodified linesif direction == "in":if speed == "fast":z_expr, max_zoom = "min(1.2+n*0.0014\\,1.6)", 1.6else:z_expr, max_zoom = "min(1.05+n*0.0006\\,1.35)", 1.35elif direction == "out":if speed == "fast":z_expr, max_zoom = "max(1.6-n*0.0016\\,1.0)", 1.6else:z_expr, max_zoom = "max(1.4-n*0.0010\\,1.0)", 1.4else: # holdz_expr, max_zoom = "1.1", 1.1if face_detected and direction == "in" and max_zoom > 1.05:raw_cx = int(face_cx * w - w / (max_zoom * 2))raw_cy = int(face_cy * h - h / (max_zoom * 2))safe_cx = max(0, min(w - int(w / max_zoom), raw_cx))safe_cy = max(0, min(h - int(h / max_zoom), raw_cy))ctr_x = w / 2 - w / (max_zoom * 2)ctr_y = h / 2 - h / (max_zoom * 2)x_expr = (f"(iw/2-(iw/zoom/2))+({safe_cx}-{ctr_x:.1f})*(zoom-1)/({max_zoom}-1)")y_expr = (f"(ih/2-(ih/zoom/2))+({safe_cy}-{ctr_y:.1f})*(zoom-1)/({max_zoom}-1)")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"ih*{y_bias:.2f}-(ih/zoom/2)"else:y_expr = "ih/2-(ih/zoom/2)"21 unmodified linese = f"{seg['end']:.3f}"z, x, y = _build_zoom_exprs(analysis, w, h)zp = f"zoompan=z='{z}':x='{x}':y='{y}':d=1:s={w}x{h}:fps=30"filter_parts.append(f"[0:v]trim={s}:{e},setpts=PTS-STARTPTS,{zp}[v{i}]")v_labels.append(f"[v{i}]")if has_audio:filter_parts.append(f"[0:a]atrim={s}:{e},asetpts=PTS-STARTPTS[a{i}]")a_labels.append(f"[a{i}]")n = len(segments)29 unmodified lines"red": "&H000000FF",}def _ts(t: float) -> str:h = int(t // 3600)m = int((t % 3600) // 60)s = t % 60return f"{h}:{m:02d}:{s:06.3f}"def _generate_per_segment_subtitles(3 unmodified linessegments: list[dict],analyses: list[dict],) -> None:"""Write ASS with per-segment alignment, color, and font-size overrides."""events: list[dict] = []# Word-level eventsfor seg in transcript.get("segments", []):for w in seg.get("words", []):t0 = max(0.0, float(w.get("start", 0)) - clip_start)t1 = max(0.0, float(w.get("end", 0)) - clip_start)text = w.get("word", w.get("text", "")).strip()if text and t1 > 0:events.append({"start": t0, "end": max(t1, t0 + 0.08), "text": text})# Sentence-level fallback (split into 3-word chunks)if not events:for seg in transcript.get("segments", []):t0 = max(0.0, float(seg.get("start", 0)) - clip_start)t1 = max(0.0, float(seg.get("end", 0)) - clip_start)text = seg.get("text", "").strip()if not text or t1 <= 0:continuewlist = text.split()chunk = 3n_ch = max(1, (len(wlist) + chunk - 1) // chunk)dur = (t1 - t0) / n_chfor j in range(n_ch):events.append({"start": t0 + j * dur,"end": t0 + (j + 1) * dur,"text": " ".join(wlist[j * chunk:(j + 1) * chunk]),})def get_an(t: float) -> dict:for seg, an in zip(segments, analyses):if seg["start"] <= t < seg["end"]:return anreturn analyses[-1] if analyses else {}lines = ["[Script Info]",7 unmodified lines"OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ""ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, ""Alignment, MarginL, MarginR, MarginV, Encoding","Style: Default,Impact,90,&H00FFFFFF,&H0000FFFF,&H00000000,&H80000000,""-1,0,0,0,100,100,0,0,1,4,0,2,40,40,200,1","","[Events]","Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text",]for ev in events:an = get_an(ev["start"])color = _ASS_COLORS.get(an.get("subtitle_color", "white"), "&H00FFFFFF")pos = an.get("subtitle_position", "bottom")energy = an.get("energy_level", "medium")moment = an.get("moment_type", "context")alignment = 8 if pos == "top" else 2margin_v = 120 if pos == "top" else 200fs = (108 if energy == "high" or moment in ("hook", "punchline")else 80 if energy == "low" else 92)# Pop animation: start 130% scale, shrink to 100% in 120mspop = "{\\fscx130\\fscy130\\t(0,120,\\fscx100\\fscy100)}"tag = f"{{\\an{alignment}\\1c{color}&\\fs{fs}\\b1}}{pop}"lines.append(f"Dialogue: 0,{_ts(ev['start'])},{_ts(ev['end'])},"f"Default,,0,0,{margin_v},,{tag}{ev['text'].upper()}")ass_path.write_text("\n".join(lines), encoding="utf-8")logger.debug(f"ASS: {len(events)} events across {len(segments)} segments")# ─── Emoji ─────────────────────────────────────────────────────────────────────67 unmodified linestranscript: dict,output_path: Path,) -> Path:"""Apply per-segment AI-driven HRE: each 3-5s chunk gets its own zoom + subtitle style."""output_path.parent.mkdir(parents=True, exist_ok=True)clip_start = clip_data.get("start", 0.0)20 unmodified lines_analyze_segment(clip_path, seg, i, n, transcript, clip_start, tmp_dir)for i, seg in enumerate(segments)]for i, (seg, an) in enumerate(zip(segments, analyses)):logger.info(f" [{seg['start']:.1f}s-{seg['end']:.1f}s] "f"zoom={an.get('zoom_direction')}({an.get('zoom_speed')}) "f"sub={an.get('subtitle_position')}/{an.get('subtitle_color')} "f"type={an.get('moment_type')} energy={an.get('energy_level')}")# 3. Per-segment zoom via filter_complexzoomed = _apply_per_segment_zoom(clip_path, segments, analyses, w, h, tmp_zoomed, has_audio=has_audio)# 4. Per-segment ASS subtitlesass_path = output_path.with_suffix(".ass")_generate_per_segment_subtitles(transcript, ass_path, clip_start, segments, analyses)# 5. Emoji from highest-energy segmentemoji = _get_emoji(clip_data, analyses)# 6. Render_render_final(zoomed, ass_path, emoji, output_path)1234567891011121314151617159 unmodified lines17717817918018118218318418518618718818919019119219319419519619719819920020121 unmodified lines22322422522622722822923023123223323429 unmodified lines2642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396403 unmodified lines6446456466476486496506516526536546556566577 unmodified lines66566666766866967067167267367467567667767867968068168268368468568668768868969069169267 unmodified lines76076176276376476576620 unmodified lines787788789790791792793794795796797798799800801802803804805806807808809810811812813814"""High-Retention Editing pipeline — per-segment AI decisions.Each 3-5s segment gets its own zoom direction, subtitle position,subtitle mode, and caption color driven by Qwen2.5-VL analyzing oneframe plus the local transcript for that segment.Pipeline per clip:1. Segment clip at speech pauses (3-5s chunks)2. Extract midpoint frame from each segment3. Qwen2.5-VL analyzes each frame → zoom + subtitle decisions4. ffmpeg filter_complex: per-segment zoompan + concat5. ASS subtitles with per-segment alignment/color/mode override tags"""import jsonimport subprocessimport tempfilefrom pathlib import Path159 unmodified linesif direction == "in":if speed == "fast":z_expr, max_zoom = "min(1.12+on*0.0018\\,1.55)", 1.55else:z_expr, max_zoom = "min(1.04+on*0.0009\\,1.32)", 1.32elif direction == "out":if speed == "fast":z_expr, max_zoom = "max(1.48-on*0.0018\\,1.0)", 1.48else:z_expr, max_zoom = "max(1.28-on*0.0009\\,1.0)", 1.28else: # 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)"21 unmodified linese = f"{seg['end']:.3f}"z, x, y = _build_zoom_exprs(analysis, w, h)zp = f"zoompan=z='{z}':x='{x}':y='{y}':d=1:s={w}x{h}:fps=30"filter_parts.append(f"[0:v]trim=start={s}:end={e},setpts=PTS-STARTPTS,fps=30,{zp},setpts=PTS-STARTPTS[v{i}]")v_labels.append(f"[v{i}]")if has_audio:filter_parts.append(f"[0:a]atrim=start={s}:end={e},asetpts=PTS-STARTPTS[a{i}]")a_labels.append(f"[a{i}]")n = len(segments)29 unmodified lines"red": "&H000000FF",}_POSITIONS = {"top", "bottom", "left", "right", "center"}_MODES = {"word", "phrase", "sentence"}_EMPHASIS = {"pop", "punch", "calm"}def _ts(t: float) -> str:total_cs = max(0, int(round(t * 100)))h = total_cs // 360000total_cs %= 360000m = total_cs // 6000total_cs %= 6000s = total_cs // 100cs = total_cs % 100return f"{h}:{m:02d}:{s:02d}.{cs:02d}"def _pick(value: object, allowed: set[str], fallback: str) -> str:v = str(value or "").strip().lower()return 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"),{"hook", "punchline", "context", "reaction", "transition"},"context",)fallback_mode = "word" if energy == "high" or moment in {"hook", "punchline", "reaction"} else "sentence"if 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"if mode == "sentence":mode = "word"if emphasis == "calm":emphasis = "punch"return {**an,"zoom_direction": zoom_direction,"zoom_speed": zoom_speed,"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,"energy_level": energy,"moment_type": moment,}def _build_hre_plan(segments: list[dict], analyses: list[dict]) -> list[dict]:plan = []n_total = len(segments)for i, (seg, analysis) in enumerate(zip(segments, analyses)):an = _normalise_analysis(analysis, i, n_total)plan.append({**an, "segment_index": i, "start": seg["start"], "end": seg["end"]})# 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"return plandef _ass_escape(text: str) -> str:return (text.replace("{", "(").replace("}", ")").replace("\r", " ").replace("\n", " ").strip())def _wrap_text(text: str, max_chars: int) -> str:text = _ass_escape(text)if len(text) <= max_chars:return textwords = text.split()if len(words) <= 1:return r"\N".join(text[i:i + max_chars] for i in range(0, len(text), max_chars))lines: list[str] = []line = ""for word in words:candidate = f"{line} {word}".strip()if line and len(candidate) > max_chars:lines.append(line)line = wordelse:line = candidateif line:lines.append(line)if len(lines) <= 2:return r"\N".join(lines)return r"\N".join([lines[0], " ".join(lines[1:])])def _collect_clip_words(transcript: dict, clip_start: float, duration: float) -> list[dict]:words: list[dict] = []for seg in transcript.get("segments", []):seg_start = float(seg.get("start", clip_start)) - clip_startseg_end = float(seg.get("end", clip_start)) - clip_startfor word in seg.get("words", []):text = str(word.get("word", word.get("text", ""))).strip()if not text:continuestart = float(word.get("start", seg_start + clip_start)) - clip_startend = float(word.get("end", word.get("start", seg_end + clip_start))) - clip_startif end <= start:end = start + 0.24if end <= 0 or start >= duration:continuewords.append({"start": max(0.0, start),"end": min(duration, end),"text": text,})return sorted(words, key=lambda w: (w["start"], w["end"]))def _segment_text(transcript: dict, clip_start: float, seg: dict) -> str:parts: list[str] = []for item in transcript.get("segments", []):start = float(item.get("start", clip_start)) - clip_startend = float(item.get("end", clip_start)) - clip_startif start < seg["end"] and end > seg["start"]:text = str(item.get("text", "")).strip()if text:parts.append(text)return " ".join(parts).strip()def _words_in_segment(words: list[dict], seg: dict) -> list[dict]:return [w for w in wordsif w["start"] < seg["end"] and w["end"] > seg["start"]]def _display_text(text: str, mode: str, emphasis: str) -> str:text = text.strip()if mode == "sentence" and emphasis == "calm":return textreturn text.upper()def _append_event(events: list[dict], start: float, end: float, text: str, plan: dict) -> None:start = max(float(plan["start"]), start)end = min(float(plan["end"]), end)if end - start < 0.08 or not text.strip():returnevents.append({"start": start,"end": end,"text": text.strip(),"plan": plan,})def _word_events(words: list[dict], seg: dict, plan: dict) -> list[dict]:events: list[dict] = []cursor = seg["start"]min_d = 0.14 if plan["energy_level"] == "high" else 0.18max_d = 0.72 if plan["energy_level"] == "high" else 0.95for i, word in enumerate(words):start = max(seg["start"], word["start"], cursor)next_start = words[i + 1]["start"] if i + 1 < len(words) else seg["end"]natural_end = max(word["end"], start + min_d)end = min(seg["end"], natural_end, start + max_d)if next_start > start:end = min(end, max(start + min_d, next_start - 0.015))if end <= start:end = min(seg["end"], start + min_d)_append_event(events, start, end, word["text"], plan)cursor = end + 0.015if cursor >= seg["end"]:breakreturn eventsdef _line_events(words: list[dict],seg: dict,plan: dict,max_words: int,max_duration: float,max_chars: int,) -> list[dict]:events: list[dict] = []i = 0cursor = seg["start"]while i < len(words) and cursor < seg["end"] - 0.08:group: list[dict] = []start = max(seg["start"], words[i]["start"], cursor)end = startchars = 0while i < len(words):word = words[i]proposed_end = min(seg["end"], max(word["end"], word["start"] + 0.2))proposed_chars = chars + len(word["text"]) + (1 if group else 0)if group and (len(group) >= max_wordsor proposed_end - start > max_durationor proposed_chars > max_chars):breakgroup.append(word)chars = proposed_charsend = max(end, proposed_end)i += 1if not group:i += 1continueend = min(seg["end"], max(end, start + 0.55))text = " ".join(w["text"] for w in group)_append_event(events, start, end, text, plan)cursor = end + 0.04return eventsdef _fallback_text_events(text: str, seg: dict, plan: dict) -> list[dict]:if not text:return []mode = plan["subtitle_mode"]if mode == "word":chunk_size = 1elif mode == "phrase":chunk_size = 3else:chunk_size = 7units = text.split()if len(units) <= 1 and len(text) > 20:step = 10 if mode == "word" else 24 if mode == "phrase" else 36units = [text[i:i + step] for i in range(0, len(text), step)]chunks = [" ".join(units[i:i + chunk_size]) for i in range(0, len(units), chunk_size)]chunks = [c for c in chunks if c.strip()]if not chunks:return []events: list[dict] = []seg_d = max(0.1, seg["end"] - seg["start"])dur = seg_d / len(chunks)for i, chunk in enumerate(chunks):start = seg["start"] + i * durend = seg["start"] + (i + 1) * dur_append_event(events, start, end, chunk, plan)return eventsdef _build_subtitle_events(transcript: dict,clip_start: float,duration: float,segments: list[dict],plan: list[dict],) -> list[dict]:words = _collect_clip_words(transcript, clip_start, duration)events: list[dict] = []for seg, seg_plan in zip(segments, plan):seg_words = _words_in_segment(words, seg)mode = seg_plan["subtitle_mode"]if seg_words and mode == "word":seg_events = _word_events(seg_words, seg, seg_plan)elif seg_words and mode == "phrase":seg_events = _line_events(seg_words, seg, seg_plan, max_words=3, max_duration=1.7, max_chars=28)elif seg_words:seg_events = _line_events(seg_words, seg, seg_plan, max_words=7, max_duration=2.8, max_chars=44)else:seg_events = []if not seg_events:seg_events = _fallback_text_events(_segment_text(transcript, clip_start, seg), seg, seg_plan)events.extend(seg_events)events = sorted(events, key=lambda ev: (ev["start"], ev["end"]))# ASS draws all active events at once; keep one visible caption event at a# time so word/phrase/sentence modes never stack on top of each other.cleaned: list[dict] = []cursor = 0.0for ev in events:start = max(ev["start"], cursor)end = min(duration, ev["end"])if end - start < 0.08:continuecleaned.append({**ev, "start": start, "end": end})cursor = end + 0.01return cleaneddef _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)}"return base, max_charsdef _generate_per_segment_subtitles(3 unmodified linessegments: list[dict],analyses: list[dict],) -> None:"""Write one ASS file from the HRE plan.The important rule is that HRE can change style every segment, but it mustnever emit simultaneous caption events at the same timestamp."""duration = max((float(seg["end"]) for seg in segments), default=0.0)plan = _build_hre_plan(segments, analyses)events = _build_subtitle_events(transcript, clip_start, duration, segments, plan)lines = ["[Script Info]",7 unmodified lines"OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ""ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, ""Alignment, MarginL, MarginR, MarginV, Encoding","Style: Default,Noto Sans,82,&H00FFFFFF,&H0000FFFF,&H00000000,&H80000000,""-1,0,0,0,100,100,0,0,1,5,1,2,40,40,200,1","","[Events]","Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text",]for ev in events:seg_plan = ev["plan"]tag, max_chars = _subtitle_tag(seg_plan)text = _display_text(ev["text"], seg_plan["subtitle_mode"], seg_plan["subtitle_emphasis"])text = _wrap_text(text, max_chars)lines.append(f"Dialogue: 0,{_ts(ev['start'])},{_ts(ev['end'])},"f"Default,,0,0,0,,{tag}{text}")ass_path.write_text("\n".join(lines), encoding="utf-8")plan_path = ass_path.with_suffix(".hre_plan.json")plan_path.write_text(json.dumps(plan, ensure_ascii=False, indent=2), encoding="utf-8")logger.debug(f"ASS: {len(events)} events across {len(segments)} HRE segments")# ─── Emoji ─────────────────────────────────────────────────────────────────────67 unmodified linestranscript: dict,output_path: Path,) -> Path:"""Apply per-segment AI-driven HRE with varied zoom and caption plans."""output_path.parent.mkdir(parents=True, exist_ok=True)clip_start = clip_data.get("start", 0.0)20 unmodified lines_analyze_segment(clip_path, seg, i, n, transcript, clip_start, tmp_dir)for i, seg in enumerate(segments)]plan = _build_hre_plan(segments, analyses)for i, (seg, an) in enumerate(zip(segments, plan)):logger.info(f" [{seg['start']:.1f}s-{seg['end']:.1f}s] "f"zoom={an.get('zoom_direction')}({an.get('zoom_speed')}) "f"sub={an.get('subtitle_position')}/{an.get('subtitle_mode')}/"f"{an.get('subtitle_color')} "f"type={an.get('moment_type')} energy={an.get('energy_level')}")# 3. Per-segment zoom via filter_complexzoomed = _apply_per_segment_zoom(clip_path, segments, plan, w, h, tmp_zoomed, has_audio=has_audio)# 4. Per-segment ASS subtitlesass_path = output_path.with_suffix(".ass")_generate_per_segment_subtitles(transcript, ass_path, clip_start, segments, plan)# 5. Emoji from highest-energy segmentemoji = _get_emoji(clip_data, plan)# 6. Render_render_final(zoomed, ass_path, emoji, output_path)
39 unmodified lines40414243444546475 unmodified lines53545556575859605 unmodified lines666768697071727339 unmodified linesnormalTitle: "Normal Subtitles",normalDesc: "Customize font, colors, animations",hreTitle: "High-Retention",hreDesc: "AI picks everything + auto-zoom + jump cuts",hreInfo: "AI will auto-select font/colors/animation, remove silence, zoom on faces, and add emoji overlays.",},th: {style: "สไตล์คลิป",5 unmodified linesnormalTitle: "ซับปกติ",normalDesc: "เลือกรูปแบบซับได้เอง",hreTitle: "High-Retention",hreDesc: "AI เลือกทุกอย่างให้ + auto-zoom + jump cuts",hreInfo: "AI จะเลือก font/สี/animation + ตัด silence + zoom หน้าคน + ใส่ emoji ให้อัตโนมัติ",},zh: {style: "片段风格",5 unmodified linesnormalTitle: "普通字幕",normalDesc: "自定义字体、颜色、动画",hreTitle: "高留存",hreDesc: "AI 自动处理 + 自动缩放 + 跳切",hreInfo: "AI 将自动选择字体/颜色/动画,去除静音段,放大人脸,并添加表情覆盖。",},} as const;39 unmodified lines40414243444546475 unmodified lines53545556575859605 unmodified lines666768697071727339 unmodified linesnormalTitle: "Normal Subtitles",normalDesc: "Customize font, colors, animations",hreTitle: "High-Retention",hreDesc: "AI picks timing, captions, and zoom",hreInfo: "AI will create a per-segment edit plan, vary caption placement/mode, zoom on key moments, and add emoji overlays.",},th: {style: "สไตล์คลิป",5 unmodified linesnormalTitle: "ซับปกติ",normalDesc: "เลือกรูปแบบซับได้เอง",hreTitle: "High-Retention",hreDesc: "AI เลือกจังหวะ ซับ และซูมให้",hreInfo: "AI จะสร้างแผนตัดต่อรายช่วง เลือกตำแหน่ง/รูปแบบซับ ซูมช่วงสำคัญ และใส่ emoji ให้อัตโนมัติ",},zh: {style: "片段风格",5 unmodified linesnormalTitle: "普通字幕",normalDesc: "自定义字体、颜色、动画",hreTitle: "高留存",hreDesc: "AI 自动选择节奏、字幕和缩放",hreInfo: "AI 将生成分段剪辑计划,调整字幕位置/模式,缩放关键时刻,并添加表情覆盖。",},} as const;
19 unmodified lines2021222324252619 unmodified lines"mode_label": "Editing Mode","normal_mode": "Normal Subtitles","hre_mode": "High-Retention Editing (AI decides)","hre_hint": "AI will auto-select caption style, apply auto-zoom, silence removal, and TikTok-style cuts."},"step3": {"title": "Subtitle Designer",19 unmodified lines2021222324252619 unmodified lines"mode_label": "Editing Mode","normal_mode": "Normal Subtitles","hre_mode": "High-Retention Editing (AI decides)","hre_hint": "AI will create a per-segment edit plan with varied captions, auto-zoom, and TikTok-style emphasis."},"step3": {"title": "Subtitle Designer",
22 unmodified lines2324252627282922 unmodified lines"mode_label": "โหมดการตัด","normal_mode": "ซับปกติ","hre_mode": "High-Retention Editing (AI เลือกให้)","hre_hint": "AI จะเลือกรูปแบบซับ จัด auto-zoom และตัดแบบ TikTok ให้อัตโนมัติ"},"step3": {"title": "ออกแบบซับไตเติ้ล",22 unmodified lines2324252627282922 unmodified lines"mode_label": "โหมดการตัด","normal_mode": "ซับปกติ","hre_mode": "High-Retention Editing (AI เลือกให้)","hre_hint": "AI จะสร้างแผนตัดต่อรายช่วง เลือกซับหลายรูปแบบ จัด auto-zoom และเน้นจังหวะแบบ TikTok"},"step3": {"title": "ออกแบบซับไตเติ้ล",
1234567{"nav": { "brand": "ElevenClip AI", "tagline": "AI智能剪辑精彩片段" },"step1": { "title": "添加视频", "upload_tab": "上传文件", "youtube_tab": "YouTube链接", "drop_hint": "拖放视频文件到此处,或点击选择", "youtube_placeholder": "粘贴YouTube链接...", "channel_label": "频道描述(可选)", "channel_placeholder": "例如:中文游戏频道,专注于搞笑时刻", "fetch_info": "获取信息" },"step2": { "title": "剪辑设置", "style_label": "剪辑风格", "duration_label": "目标时长(秒)", "count_label": "剪辑数量", "clip_lang_label": "视频语言", "sub_lang_label": "字幕语言", "mode_label": "编辑模式", "normal_mode": "普通字幕", "hre_mode": "高留存率编辑(AI决定)", "hre_hint": "AI将自动选择字幕样式、应用自动缩放、去除静音并进行TikTok风格剪辑。" },"step3": { "title": "字幕设计", "font_label": "字体", "size_label": "字体大小", "primary_color": "主要颜色", "secondary_color": "卡拉OK颜色", "outline_color": "描边颜色", "shadow_color": "阴影颜色", "outline_size": "描边大小", "shadow_size": "阴影大小", "display_mode": "显示模式", "word_by_word": "逐字", "sentence": "句子", "animation": "动画", "alignment": "对齐", "preview": "预览" },"generate": { "button": "生成剪辑", "processing": "处理中..." },"styles": { "funny": "搞笑", "serious": "严肃", "educational": "教育", "gaming": "游戏", "entertainment": "娱乐" },1234567{"nav": { "brand": "ElevenClip AI", "tagline": "AI智能剪辑精彩片段" },"step1": { "title": "添加视频", "upload_tab": "上传文件", "youtube_tab": "YouTube链接", "drop_hint": "拖放视频文件到此处,或点击选择", "youtube_placeholder": "粘贴YouTube链接...", "channel_label": "频道描述(可选)", "channel_placeholder": "例如:中文游戏频道,专注于搞笑时刻", "fetch_info": "获取信息" },"step2": { "title": "剪辑设置", "style_label": "剪辑风格", "duration_label": "目标时长(秒)", "count_label": "剪辑数量", "clip_lang_label": "视频语言", "sub_lang_label": "字幕语言", "mode_label": "编辑模式", "normal_mode": "普通字幕", "hre_mode": "高留存率编辑(AI决定)", "hre_hint": "AI将生成分段剪辑计划,改变字幕样式,应用自动缩放,并突出TikTok节奏。" },"step3": { "title": "字幕设计", "font_label": "字体", "size_label": "字体大小", "primary_color": "主要颜色", "secondary_color": "卡拉OK颜色", "outline_color": "描边颜色", "shadow_color": "阴影颜色", "outline_size": "描边大小", "shadow_size": "阴影大小", "display_mode": "显示模式", "word_by_word": "逐字", "sentence": "句子", "animation": "动画", "alignment": "对齐", "preview": "预览" },"generate": { "button": "生成剪辑", "processing": "处理中..." },"styles": { "funny": "搞笑", "serious": "严肃", "educational": "教育", "gaming": "游戏", "entertainment": "娱乐" },
123456/// <reference types="next" />/// <reference types="next/image-types/global" />import "./.next/types/routes.d.ts";
// NOTE: This file should not be edited// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.README.md+10-9
backend/src/analysis/vision.py+20-8
backend/src/processing/high_retention.py+410-85
frontend/components/ClipSettings.tsx+6-6
frontend/messages/en.json+1-1
frontend/messages/th.json+1-1
frontend/messages/zh.json+1-1
frontend/next-env.d.tsAdded+6