Skip to content

Open-source AI podcast clipper. Generate vertical clips with face tracking and burned-in captions. CLI, MCP server, web app.

feat: bucket-based "find more" clips, opt-in review flags, smarter track hints

Nika Siradzefc7a14cdf3a2
diff --git a/backend/cli.py b/backend/cli.py
index 1f32fdd..58363e6 100644
--- a/backend/cli.py
+++ b/backend/cli.py
@@ -161,6 +161,10 @@ def cmd_process(args):
config["crop_strategy"] = args.crop
if args.top:
config["top_clips"] = args.top
+ if getattr(args, "review_each", False):
+ config["review_each_clip"] = True
+ if getattr(args, "post_review", False):
+ config["post_render_review"] = True
if args.logo:
from services.asset_store import resolve as resolve_asset
resolved = resolve_asset(args.logo)
@@ -449,7 +453,7 @@ def cmd_process(args):
print(f"\n [4/4] Exporting {len(clips)} clips{_ai_label} to {output_dir}/")
results = []
t0 = time.time()
- _skip_review = False
+ _skip_review = not config.get("review_each_clip", False)
for i, clip in enumerate(clips):
ok = False
@@ -696,17 +700,18 @@ def cmd_process(args):
print(f"\n {gray}For titles, descriptions & tags: install Claude Code or Codex CLI{reset}")
# ── Post-render iteration ──
- _post_render_loop(
- clips=clips,
- results=results,
- segments=segments,
- words=words,
- config=config,
- video_path=video_path,
- output_dir=output_dir,
- face_map=face_map,
- energy_scores=energy_scores,
- )
+ if config.get("post_render_review", False):
+ _post_render_loop(
+ clips=clips,
+ results=results,
+ segments=segments,
+ words=words,
+ config=config,
+ video_path=video_path,
+ output_dir=output_dir,
+ face_map=face_map,
+ energy_scores=energy_scores,
+ )
class _Spinner:
@@ -949,10 +954,10 @@ def _review_clips(clips: list, segments: list, energy_scores: list | None, confi
_print_clips(clips)
choices = [
- questionary.Choice(f"Render {len(selected)} clips", value="render"),
+ questionary.Choice(f"Render {len(selected)} selected clips", value="render"),
questionary.Choice("Toggle clips on/off", value="toggle"),
questionary.Choice("Find a specific moment", value="find"),
- questionary.Choice("Get more suggestions from Claude", value="more"),
+ questionary.Choice("Find additional clips beyond current picks", value="more"),
questionary.Choice("Quit", value="quit"),
]
@@ -1020,34 +1025,46 @@ def _review_clips(clips: list, segments: list, energy_scores: list | None, confi
elif action == "more":
top_n = config.get("top_clips", 5)
- from services.claude_suggest import suggest_with_claude
- with _Spinner(f"Asking AI for {top_n} more suggestions...") as sp:
- more_clips = suggest_with_claude(
+ from services.claude_suggest import suggest_more_with_claude
+ request_n = min(18, max(top_n + 4, top_n * config.get("more_suggestions_multiplier", 3)))
+ with _Spinner(f"Finding up to {request_n} additional non-overlapping clips...") as sp:
+ more_clips = suggest_more_with_claude(
segments=segments,
- top_n=top_n,
+ existing_clips=clips,
+ top_n=request_n,
progress_callback=lambda pct, msg: sp.update(msg) if msg else None,
)
if more_clips:
- # Filter out duplicates (overlapping timestamps)
- new_count = 0
- for mc in more_clips:
- is_dup = False
- for existing in clips:
- # Check if timestamps overlap significantly
- overlap_start = max(mc["start_second"], existing["start_second"])
- overlap_end = min(mc["end_second"], existing["end_second"])
- if overlap_end - overlap_start > 5:
- is_dup = True
- break
- if not is_dup:
- mc["_selected"] = True
- clips.append(mc)
- new_count += 1
+ new_clips = _filter_duplicate_clip_suggestions(more_clips, clips)
+ for mc in new_clips:
+ mc["_selected"] = True
+ clips.append(mc)
+ new_count = len(new_clips)
print(f" Added {new_count} new suggestions ({len(more_clips) - new_count} duplicates skipped)")
else:
print(f" No additional suggestions found.")
+def _filter_duplicate_clip_suggestions(candidates: list, existing: list, overlap_threshold: float = 5.0) -> list:
+ """Drop suggestions that significantly overlap already-selected clips."""
+ filtered = []
+ all_existing = list(existing)
+
+ for candidate in candidates:
+ is_dup = False
+ for current in all_existing:
+ overlap_start = max(candidate["start_second"], current["start_second"])
+ overlap_end = min(candidate["end_second"], current["end_second"])
+ if overlap_end - overlap_start > overlap_threshold:
+ is_dup = True
+ break
+ if not is_dup:
+ filtered.append(candidate)
+ all_existing.append(candidate)
+
+ return filtered
+
+
def _post_render_loop(
clips: list, results: list, segments: list, words: list,
config: dict, video_path: str, output_dir: str, face_map, energy_scores,
@@ -1185,7 +1202,7 @@ def _post_render_loop(
questionary.Choice("Done — open output folder", value="done"),
questionary.Choice("Re-review a clip", value="rerender"),
questionary.Choice("Find another moment to clip", value="find"),
- questionary.Choice("Get more suggestions from Claude", value="more"),
+ questionary.Choice("Find additional clips beyond current picks", value="more"),
]
action = questionary.select(
@@ -1300,25 +1317,17 @@ def _post_render_loop(
elif action == "more":
top_n = config.get("top_clips", 5)
- print(f"\n Asking AI for more suggestions...")
- from services.claude_suggest import suggest_with_claude
- more_clips = suggest_with_claude(
+ request_n = min(18, max(top_n + 4, top_n * config.get("more_suggestions_multiplier", 3)))
+ print(f"\n Finding additional non-overlapping clip ideas...")
+ from services.claude_suggest import suggest_more_with_claude
+ more_clips = suggest_more_with_claude(
segments=segments,
- top_n=top_n,
+ existing_clips=clips,
+ top_n=request_n,
progress_callback=lambda pct, msg: print(f" {msg}") if msg else None,
)
if more_clips:
- new_clips = []
- for mc in more_clips:
- is_dup = False
- for existing in clips:
- overlap_start = max(mc["start_second"], existing["start_second"])
- overlap_end = min(mc["end_second"], existing["end_second"])
- if overlap_end - overlap_start > 5:
- is_dup = True
- break
- if not is_dup:
- new_clips.append(mc)
+ new_clips = _filter_duplicate_clip_suggestions(more_clips, clips)
if new_clips:
for nc in new_clips:
@@ -1695,6 +1704,10 @@ def cmd_presets(args):
config["time_adjust"] = args.time_adjust
if args.quality:
config["quality"] = args.quality
+ if args.review_each:
+ config["review_each_clip"] = True
+ if args.post_review:
+ config["post_render_review"] = True
if args.no_energy:
config["energy_boost"] = False
if args.no_speakers:
@@ -2289,6 +2302,8 @@ def main():
proc.add_argument("--no-speakers", action="store_true", help="Skip speaker detection (faster, uses face detection only)")
proc.add_argument("--no-cache", action="store_true", help="Force re-transcription (ignore cached transcript)")
proc.add_argument("--quality", choices=["low", "medium", "high", "max"], help="Output quality (default: high)")
+ proc.add_argument("--review-each", action="store_true", help="Review each rendered clip interactively")
+ proc.add_argument("--post-review", action="store_true", help="Open the post-render review loop after export")
# ── presets ──
pre = sub.add_parser("presets", help="Manage presets")
@@ -2308,6 +2323,8 @@ def main():
pre_save.add_argument("--top", type=int, help="Default top clips count")
pre_save.add_argument("--time-adjust", type=float)
pre_save.add_argument("--quality", choices=["low", "medium", "high", "max"])
+ pre_save.add_argument("--review-each", action="store_true", help="Enable per-clip interactive review")
+ pre_save.add_argument("--post-review", action="store_true", help="Enable post-render review loop")
pre_save.add_argument("--no-energy", action="store_true", help="Skip audio energy analysis")
pre_save.add_argument("--no-speakers", action="store_true", help="Skip speaker detection")
pre_save.add_argument("--with-corrections", action="store_true", help="Include current global corrections in preset")
diff --git a/backend/presets.py b/backend/presets.py
index ead4fa0..cb2d58a 100644
--- a/backend/presets.py
+++ b/backend/presets.py
@@ -36,6 +36,9 @@ DEFAULT_PRESET = {
"energy_boost": True,
"quality": "max",
"no_speakers": False,
+ "review_each_clip": False,
+ "post_render_review": False,
+ "more_suggestions_multiplier": 3,
"corrections": {},
}
diff --git a/backend/services/claude_suggest.py b/backend/services/claude_suggest.py
index f070e22..8c249ae 100644
--- a/backend/services/claude_suggest.py
+++ b/backend/services/claude_suggest.py
@@ -8,6 +8,7 @@ Priority: Claude Code → Codex → heuristic fallback.
"""
import json
+import math
import os
import subprocess
import sys
@@ -143,7 +144,13 @@ def _load_existing_shorts(episodes_path: str) -> list[str]:
return []
-def _build_prompt(transcript_text: str, segment_count: int, duration_min: float, top_n: int) -> str:
+def _build_prompt(
+ transcript_text: str,
+ segment_count: int,
+ duration_min: float,
+ top_n: int,
+ exclude_clips: list[dict] | None = None,
+) -> str:
"""Build the prompt for Claude to extract clips.
Inlines key rules from the knowledge base since Claude --print mode
@@ -183,6 +190,21 @@ def _build_prompt(transcript_text: str, segment_count: int, duration_min: float,
episodes_path = os.path.join(kb_dir, "03-episodes-database.md")
existing_shorts = _load_existing_shorts(episodes_path)
+ excluded_ranges = ""
+ if exclude_clips:
+ lines = []
+ for clip in exclude_clips[:24]:
+ start = round(float(clip.get("start_second", 0)), 1)
+ end = round(float(clip.get("end_second", 0)), 1)
+ title = str(clip.get("title", "Untitled")).strip()
+ if end > start:
+ lines.append(f"- {start:.1f}s to {end:.1f}s: {title[:80]}")
+ if lines:
+ excluded_ranges = (
+ "\nALREADY SELECTED CLIPS (do NOT return overlapping moments with these):\n"
+ + "\n".join(lines)
+ )
+
return f"""You are a viral clip editor for TikTok and YouTube Shorts. Find the {top_n} most scroll-stopping moments in this podcast transcript.
IMPORTANT: Return ONLY valid JSON. No markdown, no explanation, no code fences.
@@ -214,10 +236,12 @@ MOMENT SELECTION (think like a TikTok editor):
- Single focused idea — one concept, fully delivered, no loose threads
- Prioritize: controversial takes, surprising numbers, founder war stories, "wait what?" moments, emotional peaks
- Skip: generic advice, obvious statements, context-dependent references
+- On long episodes, search the ENTIRE timeline and diversify the picks. Do not cluster all clips in one section if later sections contain strong standalone moments.
{f"KNOWLEDGE BASE:{kb_context}" if kb_context else ""}
{f"EXISTING SHORTS (avoid duplicating these moments):{chr(10).join('- ' + s for s in existing_shorts)}" if existing_shorts else ""}
+{excluded_ranges}
Score each moment on 4 dimensions (1-5 each):
- standalone: Makes sense without episode context?
@@ -273,6 +297,7 @@ Transcript ({segment_count} segments, ~{duration_min:.0f} min):
def suggest_with_claude(
segments: list[dict],
top_n: int = 5,
+ exclude_clips: list[dict] | None = None,
progress_callback: Optional[Callable[[int, str], None]] = None,
) -> Optional[list[dict]]:
"""
@@ -307,7 +332,13 @@ def suggest_with_claude(
if segments:
duration_min = (segments[-1].get("end", 0) - segments[0].get("start", 0)) / 60
- prompt = _build_prompt(transcript_text, len(segments), duration_min, top_n)
+ prompt = _build_prompt(
+ transcript_text,
+ len(segments),
+ duration_min,
+ top_n,
+ exclude_clips=exclude_clips,
+ )
# Write prompt to temp file to avoid shell escaping issues
project_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..")
@@ -453,3 +484,155 @@ def suggest_with_claude(
os.unlink(prompt_file)
except Exception:
pass
+
+
+def _bucket_coverage_seconds(existing_clips: list[dict], start: float, end: float) -> float:
+ """Total selected-clip overlap inside a time bucket."""
+ covered = 0.0
+ for clip in existing_clips:
+ overlap_start = max(start, float(clip.get("start_second", 0)))
+ overlap_end = min(end, float(clip.get("end_second", 0)))
+ if overlap_end > overlap_start:
+ covered += overlap_end - overlap_start
+ return covered
+
+
+def _slice_segments_for_range(segments: list[dict], start: float, end: float) -> list[dict]:
+ """Return transcript segments that overlap a bucket range."""
+ return [
+ seg for seg in segments
+ if float(seg.get("end", seg.get("start", 0))) > start
+ and float(seg.get("start", 0)) < end
+ ]
+
+
+def suggest_more_with_claude(
+ segments: list[dict],
+ existing_clips: list[dict],
+ top_n: int = 8,
+ progress_callback: Optional[Callable[[int, str], None]] = None,
+) -> Optional[list[dict]]:
+ """
+ Ask the AI for more clips by searching under-covered time buckets first.
+
+ This prevents the common failure mode where repeated whole-episode prompts
+ keep returning the same few obvious moments.
+ """
+ if not segments:
+ return None
+
+ start_bound = float(segments[0].get("start", 0))
+ end_bound = float(segments[-1].get("end", segments[-1].get("start", 0)))
+ duration = max(0.0, end_bound - start_bound)
+ if duration <= 0:
+ return None
+
+ bucket_count = min(6, max(3, math.ceil((duration / 60.0) / 25.0)))
+ bucket_size = duration / bucket_count
+ buckets = []
+ for idx in range(bucket_count):
+ bucket_start = start_bound + idx * bucket_size
+ bucket_end = end_bound if idx == bucket_count - 1 else start_bound + (idx + 1) * bucket_size
+ bucket_segments = _slice_segments_for_range(segments, bucket_start, bucket_end)
+ if len(bucket_segments) < 3:
+ continue
+ coverage_seconds = _bucket_coverage_seconds(existing_clips, bucket_start, bucket_end)
+ bucket_duration = max(1.0, bucket_end - bucket_start)
+ buckets.append({
+ "start": bucket_start,
+ "end": bucket_end,
+ "segments": bucket_segments,
+ "coverage_ratio": coverage_seconds / bucket_duration,
+ "coverage_seconds": coverage_seconds,
+ })
+
+ if not buckets:
+ return suggest_with_claude(
+ segments=segments,
+ top_n=top_n,
+ exclude_clips=existing_clips,
+ progress_callback=progress_callback,
+ )
+
+ buckets.sort(key=lambda b: (b["coverage_ratio"], b["coverage_seconds"], b["start"]))
+ target_bucket_count = min(len(buckets), max(2, min(4, math.ceil(top_n / 3.0))))
+ selected_buckets = buckets[:target_bucket_count]
+ remaining_buckets = buckets[target_bucket_count:]
+ per_bucket_top_n = max(2, math.ceil(top_n / max(1, len(selected_buckets))))
+
+ aggregated: list[dict] = []
+ for idx, bucket in enumerate(selected_buckets):
+ bucket_label = f"bucket {idx + 1}/{len(selected_buckets)} [{int(bucket['start'] // 60)}:{int(bucket['start'] % 60):02d}-{int(bucket['end'] // 60)}:{int(bucket['end'] % 60):02d}]"
+ if progress_callback:
+ progress_callback(0, f"Searching {bucket_label}...")
+
+ bucket_clips = suggest_with_claude(
+ segments=bucket["segments"],
+ top_n=per_bucket_top_n,
+ exclude_clips=existing_clips + aggregated,
+ progress_callback=(
+ None if progress_callback is None
+ else lambda pct, msg, bucket_label=bucket_label: progress_callback(
+ pct,
+ f"{bucket_label}: {msg}" if msg else msg,
+ )
+ ),
+ )
+ if not bucket_clips:
+ continue
+
+ aggregated.extend(bucket_clips)
+ if len(aggregated) >= top_n:
+ break
+
+ if len(aggregated) < top_n and remaining_buckets:
+ for idx, bucket in enumerate(remaining_buckets, start=len(selected_buckets) + 1):
+ bucket_label = f"bucket {idx}/{len(buckets)} [{int(bucket['start'] // 60)}:{int(bucket['start'] % 60):02d}-{int(bucket['end'] // 60)}:{int(bucket['end'] % 60):02d}]"
+ if progress_callback:
+ progress_callback(0, f"Searching {bucket_label}...")
+
+ bucket_clips = suggest_with_claude(
+ segments=bucket["segments"],
+ top_n=max(1, min(per_bucket_top_n, top_n - len(aggregated))),
+ exclude_clips=existing_clips + aggregated,
+ progress_callback=(
+ None if progress_callback is None
+ else lambda pct, msg, bucket_label=bucket_label: progress_callback(
+ pct,
+ f"{bucket_label}: {msg}" if msg else msg,
+ )
+ ),
+ )
+ if not bucket_clips:
+ continue
+
+ aggregated.extend(bucket_clips)
+ if len(aggregated) >= top_n:
+ break
+
+ if len(aggregated) < max(2, min(top_n, 4)):
+ fallback_clips = suggest_with_claude(
+ segments=segments,
+ top_n=top_n,
+ exclude_clips=existing_clips + aggregated,
+ progress_callback=(
+ None if progress_callback is None
+ else lambda pct, msg: progress_callback(
+ pct,
+ f"global pass: {msg}" if msg else msg,
+ )
+ ),
+ )
+ if fallback_clips:
+ aggregated.extend(fallback_clips)
+
+ deduped = []
+ seen_ranges = set()
+ for clip in sorted(aggregated, key=lambda c: c.get("start_second", 0)):
+ key = (round(float(clip.get("start_second", 0)), 1), round(float(clip.get("end_second", 0)), 1))
+ if key in seen_ranges:
+ continue
+ seen_ranges.add(key)
+ deduped.append(clip)
+
+ return deduped[:top_n] if deduped else None
diff --git a/backend/services/video_processor.py b/backend/services/video_processor.py
index ae3451b..8b9c60b 100644
--- a/backend/services/video_processor.py
+++ b/backend/services/video_processor.py
@@ -574,7 +574,11 @@ def _resolve_speaker_sides(
) -> dict:
"""
Build left/right speaker hints for mixed split-screen clips.
- Prefer precomputed face_map mappings, then fall back to first-speaker order.
+ Only use precomputed face_map mappings here.
+
+ Guessing speaker sides from transcript order is low-confidence and can
+ easily reverse the camera on host/guest clips. Clip-local track evidence
+ should make that decision instead.
"""
speaker_side = {}
mid_x = width // 2
@@ -587,23 +591,6 @@ def _resolve_speaker_sides(
continue
speaker_side[speaker] = "left" if clusters[cluster_index]["center_x"] < mid_x else "right"
- split_count = sum(1 for _, faces in detections if len(faces) >= 2)
- has_any_split = split_count >= 3
- speakers = sorted(set(seg[2] for seg in segments)) if segments else []
-
- if has_any_split and len(speakers) >= 2 and len(speaker_side) < 2:
- talk = {}
- first = {}
- for start_t, end_t, speaker in segments:
- talk[speaker] = talk.get(speaker, 0) + (end_t - start_t)
- if speaker not in first:
- first[speaker] = start_t
- top2 = sorted(speakers, key=lambda s: talk.get(s, 0), reverse=True)[:2]
- by_first = sorted(top2, key=lambda s: first.get(s, float("inf")))
- speaker_side[by_first[0]] = "left"
- if len(by_first) > 1:
- speaker_side[by_first[1]] = "right"
-
return speaker_side
@@ -725,6 +712,16 @@ def _choose_segment_tracks(
prev_track_id = last_track_for_speaker.get(speaker)
hint_anchor = learned_anchor_x.get(speaker)
hint_side = learned_side.get(speaker)
+ side_hint_strength = 1.1
+ anchor_hint_penalty = 1.8
+
+ if hint_side is None:
+ other_sides = {sp: side for sp, side in learned_side.items() if sp != speaker}
+ if len(other_sides) == 1:
+ only_side = next(iter(other_sides.values()))
+ hint_side = "right" if only_side == "left" else "left"
+ side_hint_strength = 0.6
+ anchor_hint_penalty = 0.9
def _local_score(item):
track_id, data = item
@@ -750,22 +747,44 @@ def _choose_segment_tracks(
if hint_side:
on_hint_side = median_x < (width / 2) if hint_side == "left" else median_x >= (width / 2)
- score += 1.1 if on_hint_side else -0.8
+ score += side_hint_strength if on_hint_side else -side_hint_strength * 0.72
if hint_anchor is not None:
- score -= min(1.8, abs(median_x - hint_anchor) / max(width * 0.30, 1))
+ score -= min(anchor_hint_penalty, abs(median_x - hint_anchor) / max(width * 0.30, 1))
return score
local_best_track_id, local_best_data = max(candidates.items(), key=_local_score)
chosen_track_id, chosen_data = max(candidates.items(), key=_score)
local_best_score = _local_score((local_best_track_id, local_best_data))
+ chosen_local_score = _local_score((chosen_track_id, chosen_data))
hinted_score = _score((chosen_track_id, chosen_data))
local_scored_with_hints = _score((local_best_track_id, local_best_data))
+ local_best_x = float(median(local_best_data["cxs"]))
+ local_best_opposes_hint = False
+ if learned_side.get(speaker):
+ expected_left = learned_side[speaker] == "left"
+ local_best_left = local_best_x < (width / 2)
+ local_best_opposes_hint = expected_left != local_best_left
+ local_best_has_decisive_local_evidence = (
+ local_best_score >= chosen_local_score + 2.5
+ or local_best_data["split_frames"] >= chosen_data["split_frames"] + 2
+ )
+ hint_is_inferred_or_weak = hint_anchor is None or side_hint_strength < 1.0
+
if (
local_best_track_id != chosen_track_id
- and local_best_score >= _local_score((chosen_track_id, chosen_data)) + 1.5
- and local_scored_with_hints >= hinted_score - 0.8
+ and (
+ (
+ local_best_score >= chosen_local_score + 1.2
+ and local_scored_with_hints >= hinted_score - 0.6
+ )
+ or (
+ local_best_opposes_hint
+ and local_best_has_decisive_local_evidence
+ and hint_is_inferred_or_weak
+ )
+ )
):
chosen_track_id, chosen_data = local_best_track_id, local_best_data
diff --git a/tests/test_ai_fallback.py b/tests/test_ai_fallback.py
index 3ec4506..0917160 100644
--- a/tests/test_ai_fallback.py
+++ b/tests/test_ai_fallback.py
@@ -17,6 +17,23 @@ from services import thumbnail_ai as tai
class AIFallbackTests(unittest.TestCase):
+ def test_build_prompt_includes_excluded_ranges(self):
+ prompt = cs._build_prompt(
+ transcript_text="[0.0s] Test transcript",
+ segment_count=1,
+ duration_min=100 / 60,
+ top_n=8,
+ exclude_clips=[
+ {"start_second": 120.0, "end_second": 152.0, "title": "Grid connection hot take"},
+ {"start_second": 900.0, "end_second": 932.0, "title": "Gigawatts nobody considers"},
+ ],
+ )
+
+ self.assertIn("ALREADY SELECTED CLIPS", prompt)
+ self.assertIn("120.0s to 152.0s", prompt)
+ self.assertIn("Grid connection hot take", prompt)
+ self.assertIn("search the ENTIRE timeline and diversify the picks", prompt)
+
def test_suggest_with_claude_retries_with_codex_after_runtime_failure(self):
segments = [
{"start": 0.0, "end": 12.0, "speaker": "SPEAKER_00", "text": "Warmup context."},
@@ -63,6 +80,93 @@ class AIFallbackTests(unittest.TestCase):
self.assertIn("Retrying with Codex...", progress)
self.assertIn("Codex suggested 1 clips", progress)
+ def test_suggest_more_with_claude_searches_undercovered_buckets(self):
+ segments = []
+ for i in range(12):
+ start = float(i * 300)
+ segments.append({
+ "start": start,
+ "end": start + 40.0,
+ "speaker": "SPEAKER_00",
+ "text": f"Segment {i}",
+ })
+
+ calls = []
+
+ def fake_suggest(*, segments, top_n, exclude_clips=None, progress_callback=None):
+ calls.append({
+ "start": segments[0]["start"],
+ "end": segments[-1]["end"],
+ "top_n": top_n,
+ "exclude_len": len(exclude_clips or []),
+ })
+ first_start = segments[0]["start"]
+ return [{
+ "title": f"Clip {first_start}",
+ "start_second": first_start,
+ "end_second": first_start + 28.0,
+ "segments": [{"start": first_start, "end": first_start + 28.0}],
+ "duration": 28,
+ }]
+
+ with mock.patch.object(cs, "suggest_with_claude", side_effect=fake_suggest):
+ clips = cs.suggest_more_with_claude(
+ segments=segments,
+ existing_clips=[
+ {"start_second": 0.0, "end_second": 180.0, "title": "Early clip"},
+ {"start_second": 320.0, "end_second": 500.0, "title": "Another early clip"},
+ ],
+ top_n=6,
+ )
+
+ self.assertIsNotNone(clips)
+ self.assertGreaterEqual(len(calls), 2)
+ self.assertTrue(all(call["exclude_len"] >= 2 for call in calls))
+ bucketed_calls = [call for call in calls if call["end"] - call["start"] < 2000]
+ self.assertGreaterEqual(len(bucketed_calls), 2)
+ self.assertTrue(all(call["start"] > 500.0 for call in bucketed_calls[:2]))
+
+ def test_suggest_more_with_claude_uses_global_fallback_after_bucket_passes(self):
+ segments = []
+ for i in range(9):
+ start = float(i * 240)
+ segments.append({
+ "start": start,
+ "end": start + 35.0,
+ "speaker": "SPEAKER_00",
+ "text": f"Segment {i}",
+ })
+
+ calls = []
+
+ def fake_suggest(*, segments, top_n, exclude_clips=None, progress_callback=None):
+ calls.append({
+ "start": segments[0]["start"],
+ "end": segments[-1]["end"],
+ "top_n": top_n,
+ })
+ if segments[0]["start"] == 0.0 and segments[-1]["end"] > 1000.0:
+ return [{
+ "title": "Global clip",
+ "start_second": 1440.0,
+ "end_second": 1468.0,
+ "segments": [{"start": 1440.0, "end": 1468.0}],
+ "duration": 28,
+ }]
+ return None
+
+ with mock.patch.object(cs, "suggest_with_claude", side_effect=fake_suggest):
+ clips = cs.suggest_more_with_claude(
+ segments=segments,
+ existing_clips=[],
+ top_n=5,
+ )
+
+ self.assertIsNotNone(clips)
+ self.assertEqual(clips[0]["title"], "Global clip")
+ self.assertGreaterEqual(len(calls), 2)
+ self.assertEqual(calls[-1]["start"], 0.0)
+
def test_generate_clip_content_retries_with_codex(self):
clip = {
"title": "Power demand is exploding",
diff --git a/tests/test_video_processor.py b/tests/test_video_processor.py
index c2fa0c6..d8d0d9b 100644
--- a/tests/test_video_processor.py
+++ b/tests/test_video_processor.py
@@ -13,6 +13,20 @@ from services import video_processor as vp
class VideoProcessorTests(unittest.TestCase):
+ def test_resolve_speaker_sides_does_not_guess_from_transcript_order(self):
+ speaker_side = vp._resolve_speaker_sides(
+ segments=[(0.0, 2.0, "SPEAKER_00"), (2.0, 4.0, "SPEAKER_01")],
+ detections=[
+ (0.0, [{"cx": 420, "fw": 180}, {"cx": 1500, "fw": 180}]),
+ (0.4, [{"cx": 430, "fw": 180}, {"cx": 1490, "fw": 180}]),
+ (0.8, [{"cx": 440, "fw": 180}, {"cx": 1480, "fw": 180}]),
+ ],
+ width=1920,
+ face_map=None,
+ )
+
+ self.assertEqual(speaker_side, {})
+
def test_assign_face_tracks_keeps_ids_stable_across_frames(self):
tracked = vp._assign_face_tracks(
detections=[
@@ -88,6 +102,28 @@ class VideoProcessorTests(unittest.TestCase):
self.assertLess(anchors["SPEAKER_03"], 1200)
self.assertEqual(sides["SPEAKER_03"], "left")
+ def test_choose_segment_tracks_infers_opposite_side_for_other_speaker(self):
+ tracked_detections = [
+ (0.0, [{"cx": 510, "fw": 360, "track_id": 0}]),
+ (0.5, [{"cx": 535, "fw": 352, "track_id": 0}]),
+ (1.0, [{"cx": 540, "fw": 300, "track_id": 0}, {"cx": 1510, "fw": 255, "track_id": 1}]),
+ (1.5, [{"cx": 548, "fw": 298, "track_id": 0}, {"cx": 1496, "fw": 260, "track_id": 1}]),
+ (2.0, [{"cx": 560, "fw": 296, "track_id": 0}, {"cx": 1488, "fw": 262, "track_id": 1}]),
+ ]
+
+ segment_tracks, anchors, sides = vp._choose_segment_tracks(
+ segments=[(0.0, 0.9, "SPEAKER_00"), (0.9, 2.2, "SPEAKER_01")],
+ tracked_detections=tracked_detections,
+ speaker_side={},
+ speaker_anchor_x={},
+ width=1920,
+ )
+
+ self.assertEqual(segment_tracks[0][3], 0)
+ self.assertEqual(sides["SPEAKER_00"], "left")
+ self.assertEqual(segment_tracks[1][3], 1)
+ self.assertEqual(sides["SPEAKER_01"], "right")
+
def test_choose_track_segment_targets_picks_one_stable_target_per_turn(self):
targets = vp._choose_track_segment_targets(
segment_tracks=[