Open-source AI podcast clipper. Generate vertical clips with face tracking and burned-in captions. CLI, MCP server, web app.
Python ai-toolsauto-captionscaptionscliffmpegmcpmcp-serveropusclip-alternativepodcast-clipspodcastsshortform-videovertical-videowhisper
fix: smooth captions, aggressive face detection, filler removal, and clip-number-based MCP workflow
Nika Siradzeeca50544655c
diff --git a/backend/cli.py b/backend/cli.pyindex 4e5b70a..0f7893f 100644--- a/backend/cli.py+++ b/backend/cli.py@@ -160,7 +160,7 @@ def cmd_process(args):start_second=clip["start_second"],end_second=clip["end_second"],caption_style=config.get("caption_style", "branded"),- crop_strategy=config.get("crop_strategy", "center"),+ crop_strategy=config.get("crop_strategy", "face"),transcript_words=words,title=clip.get("title", f"clip_{i+1}"),output_dir=output_dir,diff --git a/backend/config/caption_styles.py b/backend/config/caption_styles.pyindex 441a6dc..10afda4 100644--- a/backend/config/caption_styles.py+++ b/backend/config/caption_styles.py@@ -5,21 +5,69 @@ Each style defines fonts, colors, sizes, and behavior for thesupported caption types."""+import subprocess+import functools++[email protected]_cache(maxsize=1)+def _detect_font() -> str:+ """+ Detect the best available sans-serif font on this system.+ Falls back through a priority list until one is found.+ """+ # Priority order: common cross-platform sans-serif fonts+ candidates = [+ "Arial",+ "Helvetica",+ "Liberation Sans",+ "Noto Sans",+ "DejaVu Sans",+ "FreeSans",+ "sans-serif", # Generic fallback (ffmpeg/fontconfig may resolve it)+ ]++ try:+ result = subprocess.run(+ ["fc-list", "--format=%{family}\n"],+ capture_output=True, text=True, timeout=5,+ )+ if result.returncode == 0:+ available = set()+ for line in result.stdout.splitlines():+ # fc-list returns "Family1,Family2" for multi-name fonts+ for name in line.split(","):+ available.add(name.strip())++ for candidate in candidates:+ if candidate in available:+ return candidate+ except Exception:+ pass++ # If fc-list isn't available (macOS without fontconfig), try common ones+ # Arial is almost always available on macOS/Windows+ return candidates[0]+++DETECTED_FONT = _detect_font()+# ASS color format: &HAABBGGRR (hex, alpha-blue-green-red)# Note: ASS uses BGR order, NOT RGB!STYLES = {"hormozi": {"description": "Bold, centered, 2-3 words at a time, active word gets color pop",- "font_name": "Arial",+ "font_name": DETECTED_FONT,"font_size": 80,"primary_color": "&H00FFFFFF", # White (inactive words)"active_color": "&H0000FFFF", # Yellow (active word) — BGR for yellow"outline_color": "&H00000000", # Black outline- "back_color": "&H80000000", # Semi-transparent black shadow+ "back_color": "&H80000000", # Semi-transparent black box+ "active_box_color": "&H00000000", # Black box behind active word"bold": True,- "outline_width": 4,- "shadow_depth": 2,+ "outline_width": 0,+ "shadow_depth": 0,+ "border_style": 3, # Opaque box (uses back_color as fill)"alignment": 2, # Bottom-center"margin_v": 180, # Vertical margin from bottom"words_per_chunk": 3, # Show 3 words at a time@@ -29,7 +77,7 @@ STYLES = {},"karaoke": {"description": "Full sentence visible, words highlight progressively",- "font_name": "Arial",+ "font_name": DETECTED_FONT,"font_size": 60,"primary_color": "&H00808080", # Gray (unspoken words)"active_color": "&H00FFFFFF", # White (spoken words)@@ -40,14 +88,14 @@ STYLES = {"shadow_depth": 1,"alignment": 2,"margin_v": 160,- "words_per_chunk": None, # Full sentence+ "words_per_chunk": 5,"uppercase": False,"gradient_overlay": False,"logo_support": False,},"subtle": {"description": "Clean white text at bottom with shadow, professional look",- "font_name": "Arial",+ "font_name": DETECTED_FONT,"font_size": 52,"primary_color": "&H00FFFFFF", # White"active_color": None, # No active word highlighting@@ -58,14 +106,14 @@ STYLES = {"shadow_depth": 2,"alignment": 2,"margin_v": 100,- "words_per_chunk": None, # Full sentence/segment+ "words_per_chunk": 5,"uppercase": False,"gradient_overlay": False,"logo_support": False,},"branded": {"description": "Large bold text, ~7 words wrapping across 2-3 lines, dark box on active word. TikTok/Reels style.",- "font_name": "Arial",+ "font_name": DETECTED_FONT,"font_size": 90,"primary_color": "&H00FFFFFF", # White (all words)"active_color": "&H00FFFFFF", # White text on box@@ -77,14 +125,14 @@ STYLES = {"shadow_depth": 0,"alignment": 2, # Bottom-center"margin_v": 360, # Lower-third area- "words_per_chunk": 7, # Show ~7 words, wraps across 2-3 lines+ "words_per_chunk": 4, # Show ~4 words at a time"uppercase": False, # Mixed case"gradient_overlay": True, # Enable bottom 50% dark gradient"gradient_opacity": 0.7, # 70% black at bottom edge"logo_support": True, # Enable top-left logo- "logo_margin_x": 30, # Logo X offset from left- "logo_margin_y": 40, # Logo Y offset from top- "logo_height": 80, # Logo render height in px+ "logo_margin_x": 60, # Logo X offset from left+ "logo_margin_y": 80, # Logo Y offset from top+ "logo_height": 130, # Logo render height in px},}diff --git a/backend/main.py b/backend/main.pyindex 2e31c30..a11787a 100644--- a/backend/main.py+++ b/backend/main.py@@ -65,11 +65,13 @@ def handle_create_clip(task_id: str, params: dict):start_second=params["start_second"],end_second=params["end_second"],caption_style=params.get("caption_style", "hormozi"),- crop_strategy=params.get("crop_strategy", "center"),+ crop_strategy=params.get("crop_strategy", "face"),transcript_words=params.get("transcript_words", []),title=params.get("title", "clip"),output_dir=params.get("output_dir"),logo_path=params.get("logo_path"),+ outro_path=params.get("outro_path"),+ clean_fillers=params.get("clean_fillers", True),progress_callback=lambda pct, msg: emit_progress(task_id, "processing", pct, msg),)emit_result(task_id, "success", data=result)@@ -96,11 +98,13 @@ def handle_batch_clips(task_id: str, params: dict):start_second=clip["start_second"],end_second=clip["end_second"],caption_style=clip.get("caption_style", "hormozi"),- crop_strategy=clip.get("crop_strategy", "center"),+ crop_strategy=clip.get("crop_strategy", "face"),transcript_words=params.get("transcript_words", []),title=clip.get("title", f"clip_{i + 1}"),output_dir=params.get("output_dir"),logo_path=clip.get("logo_path") or params.get("logo_path"),+ outro_path=params.get("outro_path"),+ clean_fillers=params.get("clean_fillers", True),progress_callback=lambda pct, msg: emit_progress(task_id, "batch", int((i / total) * 100 + pct / total), msg),diff --git a/backend/presets.py b/backend/presets.pyindex 5d72ab6..38a4d19 100644--- a/backend/presets.py+++ b/backend/presets.py@@ -18,7 +18,7 @@ PRESETS_DIR = os.path.join(DEFAULT_PRESET = {"caption_style": "branded",- "crop_strategy": "center",+ "crop_strategy": "face","time_adjust": -1.0,"logo_path": "","whisper_model": "base",diff --git a/backend/services/caption_renderer.py b/backend/services/caption_renderer.pyindex 6ab72ef..a8b843b 100644--- a/backend/services/caption_renderer.py+++ b/backend/services/caption_renderer.py@@ -17,6 +17,7 @@ from utils.timing_utils import seconds_to_assdef generate_ass_header(style: dict, play_res_x: int = 1080, play_res_y: int = 1920) -> str:"""Generate the ASS file header with style definitions."""bold_val = -1 if style["bold"] else 0+ border_style = style.get("border_style", 1)return f"""[Script Info]Title: Podcast Clip Captions@@ -28,7 +29,7 @@ ScaledBorderAndShadow: yes[V4+ Styles]Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding-Style: Default,{style["font_name"]},{style["font_size"]},{style["primary_color"]},{style["primary_color"]},{style["outline_color"]},{style["back_color"]},{bold_val},0,0,0,100,100,0,0,1,{style["outline_width"]},{style["shadow_depth"]},{style["alignment"]},40,40,{style["margin_v"]},1+Style: Default,{style["font_name"]},{style["font_size"]},{style["primary_color"]},{style["primary_color"]},{style["outline_color"]},{style["back_color"]},{bold_val},0,0,0,100,100,0,0,{border_style},{style["outline_width"]},{style["shadow_depth"]},{style["alignment"]},40,40,{style["margin_v"]},1[Events]Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text@@ -50,13 +51,43 @@ ScaledBorderAndShadow: yes[V4+ Styles]Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding-Style: BrandedNormal,{style["font_name"]},{style["font_size"]},&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,0,1,0,0,{style["alignment"]},80,80,{style["margin_v"]},1+Style: BrandedNormal,{style["font_name"]},{style["font_size"]},&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,0,3,0,0,{style["alignment"]},80,80,{style["margin_v"]},1[Events]Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"""+MIN_WORD_DURATION = 0.05 # 50ms minimum per word+++def _sanitize_words(words: list[dict]) -> list[dict]:+ """+ Fix timing edge cases before rendering:+ - Enforce minimum word duration (50ms)+ - Remove words with empty text+ - Ensure end > start+ """+ cleaned = []+ for w in words:+ text = (w.get("word") or "").strip()+ if not text:+ continue++ start = float(w.get("start", 0))+ end = float(w.get("end", 0))++ # Ensure end > start with minimum duration+ if end <= start:+ end = start + MIN_WORD_DURATION+ elif (end - start) < MIN_WORD_DURATION:+ end = start + MIN_WORD_DURATION++ cleaned.append({**w, "word": text, "start": start, "end": end})++ return cleaned++def render_captions(words: list[dict],caption_style: str,@@ -75,6 +106,13 @@ def render_captions(Returns:Path to the generated .ass file"""+ words = _sanitize_words(words)+ if not words:+ # Write empty subtitle file rather than crashing+ with open(output_path, "w", encoding="utf-8") as f:+ f.write(generate_ass_header(get_style(caption_style)))+ return output_path+style = get_style(caption_style)if caption_style == "hormozi":@@ -96,7 +134,8 @@ def render_captions(def _render_hormozi(words: list[dict], style: dict, offset: float) -> str:"""- Hormozi style: Show 2-3 words at a time, highlight active word.+ Hormozi style: Show 2-3 words at a time, smooth karaoke-fill highlight.+ Uses \\kf tags for progressive word fill — 1 Dialogue per chunk, no flashing."""header = generate_ass_header(style)events = []@@ -112,26 +151,28 @@ def _render_hormozi(words: list[dict], style: dict, offset: float) -> str:if not chunk:continue- for word_idx, active_word in enumerate(chunk):- w_start = max(0, active_word["start"] - offset)- w_end = max(0, active_word["end"] - offset)+ chunk_start = max(0, chunk[0]["start"] - offset)+ chunk_end = max(0, chunk[-1]["end"] - offset)- parts = []- for j, w in enumerate(chunk):- text = w["word"].upper() if uppercase else w["word"]- if j == word_idx:- active_c = style["active_color"]- parts.append(f"{{\\c{active_c}}}{text}{{\\c{style['primary_color']}}}")- else:- parts.append(text)+ # Build \kf karaoke-fill parts: each word fills progressively+ parts = []+ for w in chunk:+ duration_cs = int((w["end"] - w["start"]) * 100)+ text = w["word"].upper() if uppercase else w["word"]+ parts.append(f"{{\\kf{duration_cs}}}{text}")- line_text = " ".join(parts)- start_ts = seconds_to_ass(w_start)- end_ts = seconds_to_ass(w_end)+ # \c = active (filled) color, \2c = inactive (unfilled) color+ line_text = (+ f"{{\\c{style['active_color']}\\2c{style['primary_color']}}}"+ + " ".join(parts)+ )- events.append(- f"Dialogue: 0,{start_ts},{end_ts},Default,,0,0,0,,{line_text}"- )+ start_ts = seconds_to_ass(chunk_start)+ end_ts = seconds_to_ass(chunk_end)++ events.append(+ f"Dialogue: 0,{start_ts},{end_ts},Default,,0,0,0,,{line_text}"+ )return header + "\n".join(events) + "\n"@@ -143,7 +184,7 @@ def _render_karaoke(words: list[dict], style: dict, offset: float) -> str:header = generate_ass_header(style)events = []- sentence_size = 10+ sentence_size = style.get("words_per_chunk", 5)sentences = []for i in range(0, len(words), sentence_size):sentences.append(words[i : i + sentence_size])@@ -181,7 +222,7 @@ def _render_subtle(words: list[dict], style: dict, offset: float) -> str:header = generate_ass_header(style)events = []- line_size = 7+ line_size = style.get("words_per_chunk", 5)lines = []for i in range(0, len(words), line_size):lines.append(words[i : i + line_size])@@ -203,19 +244,28 @@ def _render_subtle(words: list[dict], style: dict, offset: float) -> str:return header + "\n".join(events) + "\n"+def _normalize_case(text: str) -> str:+ """Lowercase a word unless it's an acronym or proper 'I'."""+ stripped = text.strip(".,!?;:-–—'\"")+ if stripped.isupper() and len(stripped) >= 2:+ return text # Likely acronym (e.g. "AI", "CEO")+ if stripped == "I" or stripped == "I'm" or stripped.startswith("I'"):+ return text+ return text.lower()++def _render_branded(words: list[dict], style: dict, offset: float) -> str:"""- Branded style — large bold text wrapping across 2-3 lines.- Active word gets a dark rounded-box background.- All text stays white. Designed for TikTok/Reels vertical video.-- Shows ~7 words at a time (wrapping naturally via ASS WrapStyle: 0),- cycling through each word as active with a dark box highlight.+ Branded style — large bold text, dark rounded box on the active word.+ Each chunk shows ~4 words. For each word's duration, that word gets a+ dark background box (BorderStyle=3) while the others stay plain white."""header = generate_branded_header(style)events = []- chunk_size = style.get("words_per_chunk", 7)- box_color = style.get("active_box_color", "&H00181818")+ chunk_size = style.get("words_per_chunk", 4)+ raw_box = style.get("active_box_color", "&H00181818")+ # Ensure color has trailing & for inline ASS overrides+ box_color = raw_box if raw_box.endswith("&") else raw_box + "&"# Group words into chunkschunks = []@@ -227,20 +277,34 @@ def _render_branded(words: list[dict], style: dict, offset: float) -> str:if not chunk:continue- for word_idx, active_word in enumerate(chunk):- w_start = max(0, active_word["start"] - offset)- w_end = max(0, active_word["end"] - offset)-+ chunk_start = max(0, chunk[0]["start"] - offset)+ chunk_end = max(0, chunk[-1]["end"] - offset)++ # Normalize casing: lowercase except acronyms, capitalize first word+ normalized = []+ for j, w in enumerate(chunk):+ text = _normalize_case(w["word"])+ if j == 0:+ text = text[0].upper() + text[1:] if len(text) > 1 else text.upper()+ normalized.append(text)++ # For each word in the chunk, emit a dialogue line for its active window+ # where THAT word has a dark box and the rest are plain+ for wi, w in enumerate(chunk):+ w_start = max(0, w["start"] - offset)+ w_end = max(0, w["end"] - offset)+ if w_end <= w_start:+ continue++ # Build line: all words visible, active one gets box overrideparts = []- for j, w in enumerate(chunk):- text = w["word"]- if j == word_idx:- # Dark box via thick border: \3c sets border color as box fill- # \xbord\ybord control horizontal/vertical padding+ for wj, text in enumerate(normalized):+ if wj == wi:+ # Active word: dark rounded box background, white text+ # BorderStyle=3: \4c = box fill color, \3c = box border+ # \xbord/\ybord = horizontal/vertical padding around textparts.append(- f"{{\\bord10\\xbord14\\ybord8\\3c{box_color}\\shad0}}"- f"{text}"- f"{{\\bord0\\3c&H00000000&}}"+ f"{{\\4c{box_color}\\3c{box_color}\\xbord14\\ybord8\\bord1\\shad0}}{text}{{\\4c&H00000000&\\3c&H00000000&\\xbord0\\ybord0\\bord0}}")else:parts.append(text)@@ -254,4 +318,7 @@ def _render_branded(words: list[dict], style: dict, offset: float) -> str:f"Dialogue: 0,{start_ts},{end_ts},BrandedNormal,,0,0,0,,{line_text}")+ # Also show the chunk text in the gap between chunks (if any)+ # This ensures words stay visible for the full chunk duration+return header + "\n".join(events) + "\n"diff --git a/backend/services/clip_generator.py b/backend/services/clip_generator.pyindex ddc0f8e..a91df77 100644--- a/backend/services/clip_generator.py+++ b/backend/services/clip_generator.py@@ -19,20 +19,77 @@ from services.video_processor import (crop_to_vertical,burn_captions,normalize_audio,+ concat_outro,)from config.caption_styles import get_style+# Filler words to strip from captions (not aggressive — just obvious fillers)+_FILLER_WORDS = frozenset([+ "um", "uh", "uhh", "uhm", "umm", "hmm", "hm", "mhm",+ "ah", "er", "erm", "eh",+])+++def _clean_transcript_words(words: list[dict]) -> list[dict]:+ """+ Remove filler words and trim large gaps from transcript.++ - Strips obvious filler words (um, uh, hmm, etc.) so they don't appear in captions.+ - Caps gaps between consecutive words: if a gap exceeds MAX_GAP, the next word's+ start is pulled forward so captions don't hang on empty silence.+ - Not aggressive — only removes standalone fillers, preserves natural pacing.+ """+ MAX_GAP = 1.5 # seconds — gaps larger than this get compressed+ COMPRESSED_GAP = 0.3 # seconds — what large gaps shrink to++ # Step 1: Remove filler words+ cleaned = []+ for w in words:+ text = w.get("word", "").strip()+ # Strip punctuation for matching, but keep original text+ bare = text.lower().strip(".,!?;:-–—'\"")+ if bare in _FILLER_WORDS:+ continue+ cleaned.append(w)++ if not cleaned:+ return cleaned++ # Step 2: Compress large gaps by shifting timestamps forward+ # This tightens the caption timing without affecting the video itself+ result = [cleaned[0]]+ time_shift = 0.0++ for i in range(1, len(cleaned)):+ prev_end = cleaned[i - 1]["end"]+ curr_start = cleaned[i]["start"]+ gap = curr_start - prev_end++ if gap > MAX_GAP:+ # Compress this gap — shift everything after it forward+ time_shift += gap - COMPRESSED_GAP++ result.append({+ **cleaned[i],+ "start": cleaned[i]["start"] - time_shift,+ "end": cleaned[i]["end"] - time_shift,+ })++ return result+def generate_clip(video_path: str,start_second: float,end_second: float,caption_style: str = "hormozi",- crop_strategy: str = "center",+ crop_strategy: str = "face",transcript_words: list[dict] = None,title: str = "clip",output_dir: Optional[str] = None,logo_path: Optional[str] = None,+ outro_path: Optional[str] = None,+ clean_fillers: bool = True,progress_callback: Optional[Callable[[int, str], None]] = None,) -> dict:"""@@ -75,16 +132,18 @@ def generate_clip(work_dir = tempfile.mkdtemp(prefix="podcast_clip_")try:+ total_steps = 4 + (1 if outro_path and os.path.exists(str(outro_path)) else 0)+# Step 1: Cut the segment from the source videoif progress_callback:- progress_callback(10, "Cutting video segment...")+ progress_callback(10, f"Trimming to selected time range (1/{total_steps})")segment_path = os.path.join(work_dir, "segment.mp4")cut_segment(video_path, segment_path, start_second, end_second)# Step 2: Crop to vertical 9:16if progress_callback:- progress_callback(30, "Cropping to 9:16 vertical...")+ progress_callback(30, f"Resizing for vertical format (2/{total_steps})")cropped_path = os.path.join(work_dir, "cropped.mp4")crop_to_vertical(segment_path, cropped_path, strategy=crop_strategy)@@ -92,14 +151,20 @@ def generate_clip(# Step 3: Generate captions + burn (with optional gradient & logo)if transcript_words:if progress_callback:- progress_callback(50, f"Rendering {caption_style} captions...")+ progress_callback(50, f"Adding {caption_style} captions (3/{total_steps})")- # Filter words that fall within our clip's time range+ # Filter words that overlap with our clip's time range+ # Use overlap check instead of strict containment to avoid+ # dropping words at boundariesclip_words = [w for w in transcript_words- if w["start"] >= start_second and w["end"] <= end_second+ if w["end"] > start_second and w["start"] < end_second]+ # Clean filler words and compress large gaps (opt-out via clean_fillers=false)+ if clean_fillers:+ clip_words = _clean_transcript_words(clip_words)+if clip_words:# Generate ASS subtitle fileass_path = os.path.join(work_dir, "captions.ass")@@ -112,7 +177,7 @@ def generate_clip(# Burn captions into videoif progress_callback:- progress_callback(65, "Burning captions into video...")+ progress_callback(65, f"Rendering captions into video (3/{total_steps})")captioned_path = os.path.join(work_dir, "captioned.mp4")@@ -139,14 +204,25 @@ def generate_clip(# Step 4: Normalize audioif progress_callback:- progress_callback(80, "Normalizing audio to -14 LUFS...")+ progress_callback(70, f"Balancing audio levels (4/{total_steps})")normalized_path = os.path.join(work_dir, "normalized.mp4")normalize_audio(captioned_path, normalized_path)- # Step 5: Move to output+ # Step 5: Append outro (if provided)+ if outro_path and os.path.exists(outro_path):+ if progress_callback:+ progress_callback(85, f"Adding outro ({total_steps}/{total_steps})")++ with_outro_path = os.path.join(work_dir, "with_outro.mp4")+ concat_outro(normalized_path, outro_path, with_outro_path)+ final_video_path = with_outro_path+ else:+ final_video_path = normalized_path++ # Step 6: Move to outputif progress_callback:- progress_callback(95, "Finalizing output...")+ progress_callback(95, "Saving final clip...")# Clean filenamesafe_title = "".join(c if c.isalnum() or c in "-_ " else "" for c in title)@@ -159,7 +235,7 @@ def generate_clip(else:final_path = os.path.join(work_dir, output_filename)- shutil.copy2(normalized_path, final_path)+ shutil.copy2(final_video_path, final_path)# Get file sizefile_size = os.path.getsize(final_path)diff --git a/backend/services/encoder.py b/backend/services/encoder.pyindex 44d0e85..0e10bfb 100644--- a/backend/services/encoder.py+++ b/backend/services/encoder.py@@ -103,34 +103,36 @@ def _get_encoder_flags(encoder: str) -> list[str]:flags = {"h264_videotoolbox": ["-c:v", "h264_videotoolbox",- "-b:v", "6M", # Bitrate mode (more reliable than -q:v)+ "-b:v", "12M", # High bitrate for crisp 1080x1920+ "-profile:v", "high","-allow_sw", "1", # Allow software fallback],"h264_nvenc": ["-c:v", "h264_nvenc",- "-preset", "p4",- "-cq", "23",+ "-preset", "p6", # Slower = higher quality+ "-cq", "18", # High quality"-profile:v", "high",],"h264_amf": ["-c:v", "h264_amf",- "-quality", "balanced",+ "-quality", "quality", # Prioritize quality over speed"-rc", "cqp",- "-qp_i", "23", "-qp_p", "23",+ "-qp_i", "18", "-qp_p", "18",],"h264_vaapi": ["-c:v", "h264_vaapi",- "-qp", "23",+ "-qp", "18",],"h264_qsv": ["-c:v", "h264_qsv",- "-preset", "medium",- "-global_quality", "23",+ "-preset", "slow",+ "-global_quality", "18",],"libx264": ["-c:v", "libx264",- "-crf", "23",- "-preset", "medium",+ "-crf", "18", # Near-lossless quality+ "-preset", "slow", # Better compression at same quality+ "-profile:v", "high",],}return flags.get(encoder, flags["libx264"])@@ -144,7 +146,7 @@ def get_video_encode_flags() -> list[str]:except Exception:# Absolute fallback — never let encoder detection break the pipelineprint("Warning: encoder detection failed, using libx264", file=sys.stderr)- return ["-c:v", "libx264", "-crf", "23", "-preset", "medium"]+ return ["-c:v", "libx264", "-crf", "18", "-preset", "slow", "-profile:v", "high"]def get_encoder_info() -> dict:@@ -153,5 +155,5 @@ def get_encoder_info() -> dict:return detect_encoders()except Exception:return {"available": ["libx264"], "best": "libx264",- "best_flags": ["-c:v", "libx264", "-crf", "23", "-preset", "medium"],+ "best_flags": ["-c:v", "libx264", "-crf", "18", "-preset", "slow", "-profile:v", "high"],"system": platform.system()}diff --git a/backend/services/video_processor.py b/backend/services/video_processor.pyindex fa69c9d..36e6e9e 100644--- a/backend/services/video_processor.py+++ b/backend/services/video_processor.py@@ -13,7 +13,7 @@ from typing import Optionalfrom services.encoder import get_video_encode_flagsimport sys-CPU_FLAGS = ["-c:v", "libx264", "-crf", "23", "-preset", "medium"]+CPU_FLAGS = ["-c:v", "libx264", "-crf", "18", "-preset", "slow", "-profile:v", "high"]def _run_ffmpeg_with_fallback(cmd_parts_before_enc: list, cmd_parts_after_enc: list, output_path: str, label: str = "encode") -> str:@@ -95,7 +95,7 @@ def cut_segment(def crop_to_vertical(input_path: str,output_path: str,- strategy: str = "center",+ strategy: str = "face",) -> str:"""Crop/scale video to 1080x1920 (9:16 vertical).@@ -288,6 +288,75 @@ def burn_captions(return output_path+def concat_outro(+ input_path: str,+ outro_path: str,+ output_path: str,+) -> str:+ """+ Append an outro video to the end of the main clip.+ Re-encodes the outro to match the main clip's resolution and codec.+ """+ width, height = get_dimensions(input_path)++ concat_list = os.path.join(os.path.dirname(output_path), "concat_list.txt")++ # Re-encode outro to match main clip's dimensions+ outro_scaled = output_path + ".outro_scaled.mp4"+ _run_ffmpeg_with_fallback(+ cmd_parts_before_enc=[+ "ffmpeg", "-y",+ "-i", outro_path,+ "-vf", f"scale={width}:{height}:force_original_aspect_ratio=decrease,"+ f"pad={width}:{height}:(ow-iw)/2:(oh-ih)/2:black",+ ],+ cmd_parts_after_enc=[+ "-c:a", "aac", "-b:a", "128k", "-ar", "44100",+ "-movflags", "+faststart",+ ],+ output_path=outro_scaled,+ label="outro_scale",+ )++ # Re-encode main clip to ensure compatible streams for concat+ main_reenc = output_path + ".main_reenc.mp4"+ _run_ffmpeg_with_fallback(+ cmd_parts_before_enc=[+ "ffmpeg", "-y",+ "-i", input_path,+ "-vf", f"scale={width}:{height}",+ ],+ cmd_parts_after_enc=[+ "-c:a", "aac", "-b:a", "128k", "-ar", "44100",+ "-movflags", "+faststart",+ ],+ output_path=main_reenc,+ label="main_reenc",+ )++ try:+ with open(concat_list, "w") as f:+ f.write(f"file '{main_reenc}'\n")+ f.write(f"file '{outro_scaled}'\n")++ cmd = [+ "ffmpeg", "-y",+ "-f", "concat", "-safe", "0",+ "-i", concat_list,+ "-c", "copy",+ "-movflags", "+faststart",+ output_path,+ ]+ result = subprocess.run(cmd, capture_output=True, text=True)+ if result.returncode != 0:+ raise RuntimeError(f"FFmpeg concat failed: {result.stderr[-500:]}")+ return output_path+ finally:+ for tmp in [concat_list, outro_scaled, main_reenc]:+ if os.path.exists(tmp):+ os.remove(tmp)++def normalize_audio(input_path: str,output_path: str,@@ -359,45 +428,144 @@ def _detect_face_offset(target_ratio: float,) -> Optional[int]:"""- Detect face position in first few seconds and return crop x-offset.+ Detect face position across the video and return crop x-offset.+ Uses DNN-based face detector (more robust than Haar cascades).+ Strongly favors the largest/closest face and clusters positions+ to lock onto the dominant speaker when multiple faces are visible.Returns None if no face detected (caller should fall back to center)."""try:import cv2import numpy as np- face_cascade = cv2.CascadeClassifier(- cv2.data.haarcascades + "haarcascade_frontalface_default.xml"- )-cap = cv2.VideoCapture(video_path)+ if not cap.isOpened():+ return None++ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))+ fps = cap.get(cv2.CAP_PROP_FPS) or 30+ duration = total_frames / fps++ # Sample more frames across the full clip for better coverage+ sample_count = 60+ sample_times = [i * duration / sample_count for i in range(sample_count)]+face_positions = []- # Sample ~30 frames from first 5 seconds- for _ in range(30):+ # Try DNN detector first (much more robust than Haar cascades)+ dnn_detector = None+ proto_path = os.path.join(os.path.dirname(cv2.__file__), "data",+ "deploy.prototxt")+ model_path = os.path.join(os.path.dirname(cv2.__file__), "data",+ "res10_300x300_ssd_iter_140000.caffemodel")+ if os.path.exists(proto_path) and os.path.exists(model_path):+ dnn_detector = cv2.dnn.readNetFromCaffe(proto_path, model_path)++ # Fallback: load multiple Haar cascades for better coverage+ cascades = []+ if dnn_detector is None:+ for cascade_name in [+ "haarcascade_frontalface_default.xml",+ "haarcascade_frontalface_alt2.xml",+ "haarcascade_profileface.xml",+ ]:+ cascade_path = cv2.data.haarcascades + cascade_name+ if os.path.exists(cascade_path):+ cascades.append(cv2.CascadeClassifier(cascade_path))++ for t in sample_times:+ cap.set(cv2.CAP_PROP_POS_MSEC, t * 1000)ret, frame = cap.read()if not ret:- break-- gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)- faces = face_cascade.detectMultiScale(gray, 1.3, 5, minSize=(50, 50))-- if len(faces) > 0:- # Get largest face- largest = max(faces, key=lambda f: f[2] * f[3])- face_center_x = largest[0] + largest[2] // 2- face_positions.append(face_center_x)+ continue++ h, w = frame.shape[:2]++ if dnn_detector is not None:+ # DNN-based detection: handles frontal, profile, angled faces+ blob = cv2.dnn.blobFromImage(+ cv2.resize(frame, (300, 300)), 1.0, (300, 300),+ (104.0, 177.0, 123.0)+ )+ dnn_detector.setInput(blob)+ detections = dnn_detector.forward()++ best_score = 0+ best_cx = None+ for i in range(detections.shape[2]):+ confidence = detections[0, 0, i, 2]+ if confidence > 0.3:+ x1 = int(detections[0, 0, i, 3] * w)+ x2 = int(detections[0, 0, i, 5] * w)+ face_w = x2 - x1+ # Quadratic face-size weighting: strongly favor larger/closer faces+ score = confidence * (face_w ** 2)+ if score > best_score:+ best_score = score+ best_cx = (x1 + x2) // 2++ if best_cx is not None:+ face_positions.append(best_cx)+ else:+ # Haar cascade fallback: try all loaded cascades+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)+ for cascade in cascades:+ faces = cascade.detectMultiScale(+ gray, 1.1, 5, minSize=(60, 60)+ )+ if len(faces) > 0:+ largest = max(faces, key=lambda f: f[2] * f[3])+ face_center_x = largest[0] + largest[2] // 2+ face_positions.append(face_center_x)+ break # found face with this cascade, move to next framecap.release()if not face_positions:return None- # Average face center x position- avg_face_x = int(np.mean(face_positions))+ # Cluster face positions to find the dominant speaker.+ # When two speakers are visible, positions will form two groups.+ # Pick the largest cluster (most frequently detected face).+ crop_w = int(height * target_ratio)+ cluster_radius = crop_w * 0.25 # positions within 25% of crop width = same face++ positions = np.array(face_positions)++ # Find all distinct clusters+ clusters = []+ used = np.zeros(len(positions), dtype=bool)+ sorted_idx = np.argsort(positions)++ for idx in sorted_idx:+ if used[idx]:+ continue+ mask = np.abs(positions - positions[idx]) < cluster_radius+ mask &= ~used+ cluster = positions[mask]+ if len(cluster) > 0:+ clusters.append(cluster)+ used |= mask++ if not clusters:+ clusters = [positions]++ # Sort clusters by size (largest first)+ clusters.sort(key=lambda c: len(c), reverse=True)++ # If top cluster is clearly dominant (>50% more detections), use it+ # Otherwise pick the cluster whose center is closest to frame center+ if len(clusters) == 1 or len(clusters[0]) > len(clusters[1]) * 1.5:+ best_cluster = clusters[0]+ else:+ # Two roughly equal clusters = two speakers.+ # Pick the one closer to frame center for a safer crop.+ frame_center = width / 2+ best_cluster = min(clusters[:2], key=lambda c: abs(np.median(c) - frame_center))++ avg_face_x = int(np.median(best_cluster))# Calculate crop window centered on face- crop_w = int(height * target_ratio)crop_x = avg_face_x - crop_w // 2# Clamp to valid rangediff --git a/src/config/paths.ts b/src/config/paths.tsindex e387ef7..b84d1d5 100644--- a/src/config/paths.ts+++ b/src/config/paths.ts@@ -20,6 +20,7 @@ export const paths = {history: join(home, "history"),clipsHistory: join(home, "history", "clips.json"),knowledge: join(home, "knowledge"),+ uiState: join(home, "ui-state.json"),pythonBackend: join(__dirname, "..", "..", "backend", "main.py"),pythonPath: process.env.PYTHON_PATH || "python3",ffmpegPath: process.env.FFMPEG_PATH || "ffmpeg",diff --git a/src/handlers/batch-clips.handler.ts b/src/handlers/batch-clips.handler.tsindex ccb000f..4586461 100644--- a/src/handlers/batch-clips.handler.ts+++ b/src/handlers/batch-clips.handler.ts@@ -1,3 +1,4 @@+import { readFileSync } from "fs";import { PythonExecutor } from "../services/python-executor.js";import { FileManager } from "../services/file-manager.js";import { paths } from "../config/paths.js";@@ -5,22 +6,45 @@ import { paths } from "../config/paths.js";const executor = new PythonExecutor();const fileManager = new FileManager();+/** Load UI state from disk. Returns null if unavailable. */+function loadState(): Record<string, unknown> | null {+ try {+ return JSON.parse(readFileSync(paths.uiState, "utf-8"));+ } catch {+ return null;+ }+}+export const batchClipsToolDef = {name: "batch_create_clips",description:- "Create multiple short-form clips from a podcast in one batch. " +- "Each clip is independently cropped, captioned, and exported. " +- "Returns status for each clip (success/error).",+ "Create multiple short-form clips in one batch.\n\n" ++ "EASIEST: pass export_selected=true to export all selected clips, " ++ "or clip_numbers=[1, 3, 5] for specific clips — " ++ "everything is loaded from session state automatically.",inputSchema: {type: "object" as const,properties: {+ export_selected: {+ type: "boolean",+ description:+ "Export all selected (non-deselected) clips from session state. Simplest option.",+ },+ clip_numbers: {+ type: "array",+ items: { type: "number" },+ description:+ "Export specific clips by number (e.g. [1, 3, 5]). " ++ "Auto-fills everything from session state.",+ },video_path: {type: "string",- description: "Path to the original podcast video",+ description:+ "Path to the podcast video. Auto-loaded from session state if omitted.",},clips: {type: "array",- description: "Array of clips to create",+ description: "Array of clips to create (not needed if using clip_numbers)",items: {type: "object",properties: {@@ -39,9 +63,16 @@ export const batchClipsToolDef = {required: ["start_second", "end_second"],},},+ clean_fillers: {+ type: "boolean",+ description:+ "Remove filler words (um, uh, hmm) from captions and compress long silences. Default: true",+ default: true,+ },transcript_words: {type: "array",- description: "Word-level timestamps from transcribe_podcast",+ description:+ "Word-level timestamps. Auto-loaded from session state if omitted.",items: {type: "object",properties: {@@ -53,7 +84,7 @@ export const batchClipsToolDef = {},},},- required: ["video_path", "clips", "transcript_words"],+ required: [],},};@@ -62,11 +93,85 @@ export async function handleBatchClips(): Promise<string> {await fileManager.ensureDirectories();+ const state = loadState();+ const settings = (state?.settings as Record<string, string>) || {};+ const suggestions = (state?.suggestions as Array<Record<string, unknown>>) || [];+ const transcript = state?.transcript as Record<string, unknown> | null;++ // Auto-resolve video path+ const videoPath =+ (input.video_path as string) || (state?.videoPath as string) || "";+ if (!videoPath) {+ return JSON.stringify({ error: "video_path is required (no video in session state)" });+ }++ // Auto-resolve transcript words+ const transcriptWords =+ (input.transcript_words as Array<unknown>) ||+ (transcript?.words as Array<unknown>) ||+ [];++ // Build clips array: from export_selected, clip_numbers, or explicit clips+ let clips: Array<Record<string, unknown>>;+ const deselected = (state?.deselectedIndices as number[]) || [];++ const buildClipFromSuggestion = (s: Record<string, unknown>, num: number) => ({+ start_second: s.start_second,+ end_second: s.end_second,+ title: s.title || `clip_${num}`,+ caption_style:+ (s.suggested_caption_style as string) || settings.captionStyle || "hormozi",+ crop_strategy: settings.cropStrategy || "face",+ logo_path: settings.logoPath || null,+ });++ if (input.export_selected) {+ // Export all selected (non-deselected) clips+ clips = [];+ for (let i = 0; i < suggestions.length; i++) {+ if (!deselected.includes(i)) {+ clips.push(buildClipFromSuggestion(suggestions[i], i + 1));+ }+ }+ if (clips.length === 0) {+ return JSON.stringify({+ error: `No selected clips to export. ${suggestions.length} total, all deselected.`,+ });+ }+ } else if (input.clip_numbers) {+ const numbers = input.clip_numbers as number[];+ const errors: string[] = [];+ clips = [];++ for (const num of numbers) {+ const idx = num - 1;+ if (idx < 0 || idx >= suggestions.length) {+ errors.push(`Clip #${num} not found`);+ continue;+ }+ clips.push(buildClipFromSuggestion(suggestions[idx], num));+ }++ if (clips.length === 0) {+ return JSON.stringify({+ error: `No valid clips found. ${errors.join(", ")}. Available: 1-${suggestions.length}`,+ });+ }+ } else if (input.clips) {+ clips = input.clips as Array<Record<string, unknown>>;+ } else {+ return JSON.stringify({+ error: "Use export_selected=true, clip_numbers=[1, 3], or pass clips array",+ });+ }+const result = await executor.execute("batch_clips", {- video_path: input.video_path,- clips: input.clips,- transcript_words: input.transcript_words || [],+ video_path: videoPath,+ clips,+ transcript_words: transcriptWords,+ clean_fillers: input.clean_fillers !== false,output_dir: paths.output,+ logo_path: settings.logoPath || null,});const data = result.data as Record<string, unknown>;diff --git a/src/handlers/create-clip.handler.ts b/src/handlers/create-clip.handler.tsindex dbbd30b..39abf67 100644--- a/src/handlers/create-clip.handler.ts+++ b/src/handlers/create-clip.handler.ts@@ -1,3 +1,4 @@+import { readFileSync } from "fs";import { PythonExecutor } from "../services/python-executor.js";import { FileManager } from "../services/file-manager.js";import { paths } from "../config/paths.js";@@ -6,45 +7,63 @@ import type { ClipResult } from "../models/index.js";const executor = new PythonExecutor();const fileManager = new FileManager();+/** Load UI state from disk. Returns null if unavailable. */+function loadState(): Record<string, unknown> | null {+ try {+ return JSON.parse(readFileSync(paths.uiState, "utf-8"));+ } catch {+ return null;+ }+}+export const createClipToolDef = {name: "create_clip",description:"Create a finished short-form video clip ready for TikTok/YouTube Shorts. " +- "Takes a segment from the podcast, crops to 9:16 vertical (1080x1920), " +- "burns styled captions, normalizes audio to -14 LUFS, and exports as H.264 MP4. " +- "Requires transcript_words from transcribe_podcast for caption timing.",+ "Crops to 9:16 vertical (1080x1920), burns styled captions, normalizes audio to -14 LUFS, exports H.264 MP4.\n\n" ++ "EASIEST: just pass clip_number (e.g. 3) to export a suggested clip — " ++ "video_path, timestamps, transcript_words, and title are all loaded from session state automatically.\n\n" ++ "You can also pass explicit start_second/end_second/video_path/transcript_words to override.",inputSchema: {type: "object" as const,properties: {+ clip_number: {+ type: "number",+ description:+ "Export a suggested clip by its number (from suggest_clips). " ++ "Auto-fills video_path, start/end times, title, and transcript_words from session state.",+ },video_path: {type: "string",- description: "Path to the original podcast video file",+ description:+ "Path to the podcast video. Auto-loaded from session state if omitted.",},start_second: {type: "number",- description: "Clip start time in seconds",+ description:+ "Clip start time in seconds. Auto-loaded from clip_number if omitted.",},end_second: {type: "number",- description: "Clip end time in seconds (max 180s clip length)",+ description:+ "Clip end time in seconds. Auto-loaded from clip_number if omitted.",},caption_style: {type: "string",enum: ["hormozi", "karaoke", "subtle", "branded"],description:- "Caption style: hormozi (bold word-by-word), karaoke (progressive highlight), subtle (clean bottom text). Default: hormozi",- default: "hormozi",+ "Caption style. Auto-loaded from session settings if omitted.",},crop_strategy: {type: "string",enum: ["center", "face"],description:- "How to crop to vertical: center (fast, default) or face (detect speaker face). Default: center",- default: "center",+ "How to crop to vertical. Auto-loaded from session settings if omitted.",},transcript_words: {type: "array",- description: "Word-level timestamps from transcribe_podcast",+ description:+ "Word-level timestamps. Auto-loaded from session state if omitted.",items: {type: "object",properties: {@@ -57,16 +76,23 @@ export const createClipToolDef = {},title: {type: "string",- description: "Short title for the clip (used in filename)",+ description:+ "Short title for the clip. Auto-loaded from suggestion if clip_number is used.",},logo_path: {type: "string",- description: "Path to a PNG logo image. Shown in top-left corner (used with 'branded' style).",+ description:+ "Path to a PNG logo image. Auto-loaded from session settings if omitted.",+ },+ clean_fillers: {+ type: "boolean",+ description:+ "Remove filler words (um, uh, hmm) from captions and compress long silences. Default: true",+ default: true,},- // === EXTENSIBILITY: Future pipeline steps ===- outro_video_path: {+ outro_path: {type: "string",- description: "[Future] Path to an outro video to append at the end",+ description: "Path to an outro video to append at the end of the clip",},generate_thumbnail: {type: "boolean",@@ -74,7 +100,7 @@ export const createClipToolDef = {default: false,},},- required: ["video_path", "start_second", "end_second", "transcript_words"],+ required: [],},};@@ -83,24 +109,79 @@ export async function handleCreateClip(): Promise<string> {await fileManager.ensureDirectories();+ const state = loadState();+ const settings = (state?.settings as Record<string, string>) || {};+ const suggestions = (state?.suggestions as Array<Record<string, unknown>>) || [];+ const transcript = state?.transcript as Record<string, unknown> | null;++ // Resolve clip from suggestion number+ let suggestion: Record<string, unknown> | null = null;+ if (input.clip_number != null) {+ const idx = (input.clip_number as number) - 1;+ if (idx < 0 || idx >= suggestions.length) {+ return JSON.stringify({+ error: `Clip #${input.clip_number} not found. Available: 1-${suggestions.length}`,+ });+ }+ suggestion = suggestions[idx];+ }++ // Auto-resolve fields: explicit input > suggestion > state+ const videoPath =+ (input.video_path as string) || (state?.videoPath as string) || "";+ const startSecond =+ (input.start_second as number) ?? (suggestion?.start_second as number);+ const endSecond =+ (input.end_second as number) ?? (suggestion?.end_second as number);+ const title =+ (input.title as string) ||+ (suggestion?.title as string) ||+ "clip";+ const captionStyle =+ (input.caption_style as string) ||+ (suggestion?.suggested_caption_style as string) ||+ settings.captionStyle ||+ "hormozi";+ const cropStrategy =+ (input.crop_strategy as string) || settings.cropStrategy || "face";+ const logoPath =+ (input.logo_path as string) || settings.logoPath || null;+ const outroPath =+ (input.outro_path as string) || settings.outroPath || null;+ const transcriptWords =+ (input.transcript_words as Array<unknown>) ||+ (transcript?.words as Array<unknown>) ||+ [];++ // Validate required fields+ if (!videoPath) {+ return JSON.stringify({ error: "video_path is required (no video in session state)" });+ }+ if (startSecond == null || endSecond == null) {+ return JSON.stringify({+ error: "start_second and end_second are required (use clip_number to reference a suggestion)",+ });+ }+const result = await executor.execute("create_clip", {- video_path: input.video_path,- start_second: input.start_second,- end_second: input.end_second,- caption_style: input.caption_style || "hormozi",- crop_strategy: input.crop_strategy || "center",- transcript_words: input.transcript_words || [],- title: input.title || "clip",+ video_path: videoPath,+ start_second: startSecond,+ end_second: endSecond,+ caption_style: captionStyle,+ crop_strategy: cropStrategy,+ transcript_words: transcriptWords,+ title,output_dir: paths.output,- logo_path: input.logo_path || null,- // Pass through future extensibility params- outro_video_path: input.outro_video_path || null,+ clean_fillers: input.clean_fillers !== false,+ logo_path: logoPath,+ outro_path: outroPath,generate_thumbnail: input.generate_thumbnail || false,});const data = result.data as unknown as ClipResult;return JSON.stringify({+ clip_number: input.clip_number || null,output_path: data.output_path,duration: data.duration,file_size_mb: data.file_size_mb,diff --git a/src/handlers/suggest-clips.handler.ts b/src/handlers/suggest-clips.handler.tsindex e227893..bf93499 100644--- a/src/handlers/suggest-clips.handler.ts+++ b/src/handlers/suggest-clips.handler.ts@@ -5,6 +5,8 @@* in conversation, then calls this tool to store/format the suggestions.*/+import { randomUUID } from "crypto";+export const suggestClipsToolDef = {name: "suggest_clips",description:@@ -69,7 +71,8 @@ export async function handleSuggestClips(// Validate and enrich suggestionsconst enriched = suggestions.map((s, i) => ({- clip_id: `clip_${i + 1}`,+ clip_number: i + 1,+ clip_id: randomUUID(),title: s.title,start_second: s.start_second,end_second: s.end_second,diff --git a/src/server.ts b/src/server.tsindex 10cd5b6..a19f750 100644--- a/src/server.ts+++ b/src/server.ts@@ -97,12 +97,13 @@ export function createServer(): McpServer {try {const result = await handleSuggestClips({ suggestions });- // Push suggestions to Web UI (silent fail if UI isn't running)+ // Push enriched suggestions (with clip_ids) to Web UItry {+ const parsed = JSON.parse(result);await fetch("http://localhost:3847/api/ui-state", {method: "POST",headers: { "Content-Type": "application/json" },- body: JSON.stringify({ suggestions, phase: "review" }),+ body: JSON.stringify({ suggestions: parsed.clips, phase: "review" }),});} catch {}@@ -144,7 +145,7 @@ export function createServer(): McpServer {crop_strategy: z.enum(["center", "face"]).optional()- .default("center")+ .default("face").describe("Cropping strategy"),transcript_words: z.array(@@ -161,10 +162,10 @@ export function createServer(): McpServer {.string().optional().describe("Path or registered asset name for PNG logo. Shown in top-left (branded style)."),- outro_video_path: z+ outro_path: z.string().optional()- .describe("[Future] Path to outro video to append"),+ .describe("Path to an outro video to append at the end of the clip"),generate_thumbnail: z.boolean().optional()@@ -185,7 +186,7 @@ export function createServer(): McpServer {params.start_second as number,params.end_second as number,(params.caption_style || "hormozi") as string,- (params.crop_strategy || "center") as string+ (params.crop_strategy || "face") as string);if (dup) {return {@@ -205,7 +206,7 @@ export function createServer(): McpServer {start_second: params.start_second as number,end_second: params.end_second as number,caption_style: (params.caption_style || "hormozi") as string,- crop_strategy: (params.crop_strategy || "center") as string,+ crop_strategy: (params.crop_strategy || "face") as string,logo_path: params.logo_path as string | undefined,title: (params.title || "clip") as string,output_path: parsed.output_path,@@ -314,7 +315,7 @@ export function createServer(): McpServer {start_second: r.start_second || 0,end_second: r.end_second || 0,caption_style: r.caption_style || "hormozi",- crop_strategy: r.crop_strategy || "center",+ crop_strategy: r.crop_strategy || "face",title: r.title || "clip",output_path: r.output_path,file_size_mb: r.file_size_mb || 0,@@ -396,7 +397,7 @@ export function createServer(): McpServer {"manage_assets","Register and manage reusable assets (logos, videos). Registered assets can be referenced by name in create_clip instead of full paths.",{- action: z.enum(["list", "register", "unregister", "resolve"]).describe("Action to perform"),+ action: z.enum(["list", "register", "unregister", "resolve", "import"]).describe("Action to perform"),name: z.string().optional().describe("Asset name (e.g. 'podcast-logo')"),path: z.string().optional().describe("Absolute file path (for register)"),type: z.enum(["logo", "video", "image", "other"]).optional().describe("Asset type (for register/list filter)"),@@ -421,6 +422,10 @@ export function createServer(): McpServer {const resolved = await assets.resolve(name);return { content: [{ type: "text" as const, text: resolved || `Asset "${name}" not found.` }] };}+ if (action === "import" && path && name) {+ const asset = await assets.importFile(path, name, type || "other");+ return { content: [{ type: "text" as const, text: `Imported "${asset.name}" → ${asset.path}` }] };+ }return { content: [{ type: "text" as const, text: "Invalid action or missing parameters." }] };} catch (err: unknown) {const msg = err instanceof Error ? err.message : String(err);@@ -459,7 +464,7 @@ export function createServer(): McpServer {if (action === "check" && source_video && start_second !== undefined && end_second !== undefined) {const dup = await history.findDuplicate(source_video, start_second, end_second,- caption_style || "hormozi", crop_strategy || "center"+ caption_style || "hormozi", crop_strategy || "face");if (dup) {return { content: [{ type: "text" as const, text: `Duplicate found: "${dup.title}" created ${dup.created_at}\nOutput: ${dup.output_path}` }] };@@ -479,9 +484,16 @@ export function createServer(): McpServer {// =============================================server.tool("get_ui_state",- "Read the current state of the podcli Web UI — selected clips, video path, settings, transcript, and phase. Always returns the full transcript segments for clip analysis.",- {},- async () => {+ "Read the current podcli session state — clips, video path, settings, phase. " ++ "Clips are numbered #1, #2, etc. Use these numbers with create_clip(clip_number), " ++ "batch_create_clips(clip_numbers), modify_clip, and toggle_clip.\n\n" ++ "Set include_transcript=true only when you need to analyze transcript content (e.g. for suggesting clips).",+ {+ include_transcript: z.boolean().optional().default(false).describe(+ "Include full transcript segments in the response. Only set true when analyzing content for clip suggestions."+ ),+ },+ async ({ include_transcript }) => {try {const res = await fetch("http://localhost:3847/api/ui-state");if (!res.ok) throw new Error(`HTTP ${res.status}`);@@ -492,36 +504,54 @@ export function createServer(): McpServer {lines.push(`Video: ${state.videoPath || state.filePath || "(none)"}`);lines.push(`Settings: caption=${state.settings?.captionStyle}, crop=${state.settings?.cropStrategy}, logo=${state.settings?.logoPath || "none"}`);lines.push(`Transcript: ${state.transcriptWordCount} words`);- lines.push(`Clips: ${state.selectedClips?.length || 0} selected (${state.totalSuggestions || 0} total, ${state.deselectedCount || 0} deselected)`);- if (state.selectedClips?.length) {+ const allSuggestions = state.suggestions || [];+ const deselected: number[] = state.deselectedIndices || [];+ const selectedCount = allSuggestions.length - deselected.length;+ lines.push(`Clips: ${selectedCount} selected, ${allSuggestions.length} total`);++ if (allSuggestions.length) {+ lines.push("");+ lines.push("Clips (use these numbers with create_clip/batch_create_clips):");+ for (let i = 0; i < allSuggestions.length; i++) {+ const clip = allSuggestions[i] as any;+ const num = i + 1;+ const title = clip.title || "untitled";+ const start = clip.start_second ?? "?";+ const end = clip.end_second ?? "?";+ const duration = typeof start === "number" && typeof end === "number"+ ? `${Math.round(end - start)}s`+ : "?";+ const style = clip.suggested_caption_style || "hormozi";+ const tag = deselected.includes(i) ? " [DESELECTED]" : "";+ lines.push(` #${num}: "${title}" (${start}s–${end}s, ${duration}) [${style}]${tag}`);+ }lines.push("");- lines.push("Selected clips:");- for (const clip of state.selectedClips) {- const title = (clip as any).title || "untitled";- const start = (clip as any).start_second ?? "?";- const end = (clip as any).end_second ?? "?";- lines.push(` - "${title}" (${start}s – ${end}s)`);+ lines.push(`To export: create_clip(clip_number: N) or batch_create_clips(clip_numbers: [1, 3, 5])`);+ if (selectedCount > 0 && selectedCount < allSuggestions.length) {+ lines.push(`To export all selected: batch_create_clips(export_selected: true)`);}}- if (state.transcript) {- const segments = state.transcript.segments || [];- if (segments.length) {- lines.push("");- lines.push("=== TRANSCRIPT ===");- for (const seg of segments) {- const speaker = seg.speaker || "?";- const start = Math.floor(seg.start || 0);- const end = Math.floor(seg.end || 0);- const text = seg.text || "";- lines.push(`[${start}s-${end}s] ${speaker}: ${text}`);+ if (include_transcript) {+ if (state.transcript) {+ const segments = state.transcript.segments || [];+ if (segments.length) {+ lines.push("");+ lines.push("=== TRANSCRIPT ===");+ for (const seg of segments) {+ const speaker = seg.speaker || "?";+ const start = Math.floor(seg.start || 0);+ const end = Math.floor(seg.end || 0);+ const text = seg.text || "";+ lines.push(`[${start}s-${end}s] ${speaker}: ${text}`);+ }}+ } else if (state.rawTranscriptText) {+ lines.push("");+ lines.push("=== RAW TRANSCRIPT (not yet parsed — use this to analyze and suggest clips) ===");+ lines.push(state.rawTranscriptText);}- } else if (state.rawTranscriptText) {- lines.push("");- lines.push("=== RAW TRANSCRIPT (not yet parsed — use this to analyze and suggest clips) ===");- lines.push(state.rawTranscriptText);}return { content: [{ type: "text" as const, text: lines.join("\n") }] };@@ -540,5 +570,573 @@ export function createServer(): McpServer {});+ // =============================================+ // Tool: modify_clip+ // =============================================+ server.tool(+ "modify_clip",+ "Modify or delete a suggested clip by clip_number. Use action='delete' to remove a clip entirely.",+ {+ clip_number: z.number().optional().describe("Clip number (1-based, from get_ui_state)"),+ clip_id: z.string().optional().describe("UUID of the clip (alternative to clip_number)"),+ index: z.number().optional().describe("0-based index (deprecated, use clip_number)"),+ action: z.enum(["update", "delete"]).optional().default("update").describe("Action: 'update' (default) or 'delete'"),+ updates: z.object({+ title: z.string().optional(),+ start_second: z.number().optional(),+ end_second: z.number().optional(),+ reasoning: z.string().optional(),+ preview_text: z.string().optional(),+ suggested_caption_style: z.enum(["hormozi", "karaoke", "subtle", "branded"]).optional(),+ }).optional().describe("Partial fields to update on the clip (ignored when action='delete')"),+ },+ async ({ clip_number, clip_id, index, action, updates }) => {+ try {+ // 1. Read current UI state+ const res = await fetch("http://localhost:3847/api/ui-state");+ if (!res.ok) throw new Error(`HTTP ${res.status}`);+ const state = await res.json() as any;+ const suggestions: any[] = state.suggestions || [];++ if (!suggestions.length) {+ return { content: [{ type: "text" as const, text: "No suggestions in UI state." }] };+ }++ // 2. Find target clip (clip_number is 1-based)+ let targetIdx = -1;+ if (clip_number !== undefined) {+ targetIdx = clip_number - 1;+ } else if (clip_id) {+ targetIdx = suggestions.findIndex((s: any) => s.clip_id === clip_id);+ } else if (index !== undefined) {+ targetIdx = index;+ }++ if (targetIdx < 0 || targetIdx >= suggestions.length) {+ return { content: [{ type: "text" as const, text: `Clip not found. Use get_ui_state to see available clips.` }] };+ }++ // --- DELETE ---+ if (action === "delete") {+ const removed = suggestions[targetIdx];+ suggestions.splice(targetIdx, 1);+ // Adjust deselectedIndices: remove the deleted index, shift higher ones down+ const deselected: number[] = state.deselectedIndices || [];+ const adjusted = deselected+ .filter((i: number) => i !== targetIdx)+ .map((i: number) => (i > targetIdx ? i - 1 : i));++ await fetch("http://localhost:3847/api/ui-state", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ suggestions, deselectedIndices: adjusted }),+ });++ return {+ content: [{+ type: "text" as const,+ text: `Deleted clip #${targetIdx + 1}: "${removed.title}". ${suggestions.length} clips remaining.`,+ }],+ };+ }++ // --- UPDATE ---+ const upd = updates || {};+ if (Object.keys(upd).length === 0) {+ return { content: [{ type: "text" as const, text: "No updates provided. Specify at least one field: title, start_second, end_second, reasoning, preview_text, or suggested_caption_style." }] };+ }+ const clip = suggestions[targetIdx];+ if (upd.title !== undefined) clip.title = upd.title;+ if (upd.start_second !== undefined) clip.start_second = upd.start_second;+ if (upd.end_second !== undefined) clip.end_second = upd.end_second;+ if (upd.reasoning !== undefined) clip.reasoning = upd.reasoning;+ if (upd.preview_text !== undefined) clip.preview_text = upd.preview_text;+ if (upd.suggested_caption_style !== undefined) clip.suggested_caption_style = upd.suggested_caption_style;++ // Recalculate derived fields+ clip.duration = Math.round((clip.end_second - clip.start_second) * 10) / 10;+ const fmtTime = (s: number) => `${Math.floor(s / 60)}:${Math.floor(s % 60).toString().padStart(2, "0")}`;+ clip.timestamp_display = `${fmtTime(clip.start_second)} → ${fmtTime(clip.end_second)}`;++ suggestions[targetIdx] = clip;+ await fetch("http://localhost:3847/api/ui-state", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ suggestions }),+ });++ return {+ content: [{+ type: "text" as const,+ text: `Updated clip #${targetIdx + 1}: "${clip.title}" (${clip.start_second}s–${clip.end_second}s, ${clip.duration}s)`,+ }],+ };+ } catch (err: unknown) {+ const msg = err instanceof Error ? err.message : String(err);+ if (msg.includes("ECONNREFUSED") || msg.includes("fetch failed")) {+ return { content: [{ type: "text" as const, text: "Web UI is not running. Start with: npm run ui" }] };+ }+ return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };+ }+ }+ );++ // =============================================+ // Tool: toggle_clip+ // =============================================+ server.tool(+ "toggle_clip",+ "Select or deselect a suggested clip by clip_number. Selected clips are exported with export_selected.",+ {+ clip_number: z.number().optional().describe("Clip number (1-based, from get_ui_state)"),+ clip_id: z.string().optional().describe("UUID of the clip (alternative to clip_number)"),+ index: z.number().optional().describe("0-based index (deprecated, use clip_number)"),+ selected: z.boolean().describe("true = select, false = deselect"),+ },+ async ({ clip_number, clip_id, index, selected }) => {+ try {+ const res = await fetch("http://localhost:3847/api/ui-state");+ if (!res.ok) throw new Error(`HTTP ${res.status}`);+ const state = await res.json() as any;+ const suggestions: any[] = state.suggestions || [];+ const deselected: number[] = state.deselectedIndices || [];++ // Find target index (clip_number is 1-based)+ let targetIdx = -1;+ if (clip_number !== undefined) {+ targetIdx = clip_number - 1;+ } else if (clip_id) {+ targetIdx = suggestions.findIndex((s: any) => s.clip_id === clip_id);+ } else if (index !== undefined) {+ targetIdx = index;+ }++ if (targetIdx < 0 || targetIdx >= suggestions.length) {+ return { content: [{ type: "text" as const, text: "Clip not found. Use get_ui_state to see available clips." }] };+ }++ let updated: number[];+ if (selected) {+ updated = deselected.filter((i: number) => i !== targetIdx);+ } else {+ updated = deselected.includes(targetIdx) ? deselected : [...deselected, targetIdx];+ }++ await fetch("http://localhost:3847/api/ui-state", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ deselectedIndices: updated }),+ });++ const clip = suggestions[targetIdx];+ return {+ content: [{+ type: "text" as const,+ text: `Clip #${targetIdx + 1} "${clip.title}" is now ${selected ? "selected" : "deselected"}. (${suggestions.length - updated.length}/${suggestions.length} selected)`,+ }],+ };+ } catch (err: unknown) {+ const msg = err instanceof Error ? err.message : String(err);+ if (msg.includes("ECONNREFUSED") || msg.includes("fetch failed")) {+ return { content: [{ type: "text" as const, text: "Web UI is not running. Start with: npm run ui" }] };+ }+ return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };+ }+ }+ );++ // =============================================+ // Tool: update_settings+ // =============================================+ server.tool(+ "update_settings",+ "Update rendering settings (caption style, crop strategy, logo, outro) in the Web UI.",+ {+ caption_style: z.enum(["hormozi", "karaoke", "subtle", "branded"]).optional().describe("Caption style"),+ crop_strategy: z.enum(["center", "face"]).optional().describe("Cropping strategy"),+ logo_path: z.string().optional().describe("Path or registered asset name for PNG logo"),+ outro_path: z.string().optional().describe("Path or registered asset name for outro video"),+ },+ async ({ caption_style, crop_strategy, logo_path, outro_path }) => {+ try {+ const settings: Record<string, string> = {};+ if (caption_style) settings.captionStyle = caption_style;+ if (crop_strategy) settings.cropStrategy = crop_strategy;+ if (logo_path) {+ const resolved = await assets.resolve(logo_path);+ settings.logoPath = resolved || logo_path;+ }+ if (outro_path) {+ const resolved = await assets.resolve(outro_path);+ settings.outroPath = resolved || outro_path;+ }++ if (Object.keys(settings).length === 0) {+ return { content: [{ type: "text" as const, text: "No settings provided. Specify at least one of: caption_style, crop_strategy, logo_path, outro_path." }] };+ }++ await fetch("http://localhost:3847/api/ui-state", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ settings }),+ });++ const parts = Object.entries(settings).map(([k, v]) => `${k}=${v}`);+ return { content: [{ type: "text" as const, text: `Settings updated: ${parts.join(", ")}` }] };+ } catch (err: unknown) {+ const msg = err instanceof Error ? err.message : String(err);+ if (msg.includes("ECONNREFUSED") || msg.includes("fetch failed")) {+ return { content: [{ type: "text" as const, text: "Web UI is not running. Start with: npm run ui" }] };+ }+ return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };+ }+ }+ );++ // =============================================+ // Tool: list_outputs+ // =============================================+ server.tool(+ "list_outputs",+ "List all rendered clip files in the output directory with file sizes and dates.",+ {},+ async () => {+ try {+ const res = await fetch("http://localhost:3847/api/outputs");+ if (!res.ok) throw new Error(`HTTP ${res.status}`);+ const clips = await res.json() as any[];++ if (!clips.length) {+ return { content: [{ type: "text" as const, text: "No rendered clips found." }] };+ }++ const lines = clips.map((c: any) => {+ const date = c.created ? c.created.split("T")[0] : "unknown";+ return ` - ${c.filename} (${c.size_mb} MB, ${date})`;+ });++ return {+ content: [{ type: "text" as const, text: `${clips.length} rendered clip${clips.length === 1 ? "" : "s"}:\n${lines.join("\n")}` }],+ };+ } catch (err: unknown) {+ const msg = err instanceof Error ? err.message : String(err);+ if (msg.includes("ECONNREFUSED") || msg.includes("fetch failed")) {+ return { content: [{ type: "text" as const, text: "Web UI is not running. Start with: npm run ui" }] };+ }+ return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };+ }+ }+ );++ // =============================================+ // Tool: manage_presets+ // =============================================+ server.tool(+ "manage_presets",+ "Save, load, list, or delete rendering presets. Presets store caption_style, crop_strategy, logo_path, and outro_path for quick reuse.",+ {+ action: z.enum(["list", "save", "load", "delete"]).describe("Preset action"),+ name: z.string().optional().describe("Preset name (required for save/load/delete)"),+ config: z.object({+ caption_style: z.enum(["hormozi", "karaoke", "subtle", "branded"]).optional(),+ crop_strategy: z.enum(["center", "face"]).optional(),+ logo_path: z.string().optional(),+ outro_path: z.string().optional(),+ }).optional().describe("Preset config (for save action)"),+ },+ async ({ action, name, config }) => {+ try {+ if (["save", "load", "delete"].includes(action) && !name) {+ return { content: [{ type: "text" as const, text: `Error: 'name' is required for action '${action}'.` }], isError: true };+ }++ const res = await fetch("http://localhost:3847/api/presets", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ action, name, config }),+ });+ if (!res.ok) throw new Error(`HTTP ${res.status}`);+ const data = await res.json() as any;++ if (data.error) {+ return { content: [{ type: "text" as const, text: `Error: ${data.error}` }], isError: true };+ }++ if (action === "list") {+ const presets: string[] = data.presets || [];+ if (!presets.length) return { content: [{ type: "text" as const, text: "No presets saved." }] };+ return { content: [{ type: "text" as const, text: `Presets:\n${presets.map((p: any) => ` - ${typeof p === "string" ? p : p.name || JSON.stringify(p)}`).join("\n")}` }] };+ }++ if (action === "load" && data.config) {+ // Push loaded config to UI settings+ const settings: Record<string, string> = {};+ if (data.config.caption_style) settings.captionStyle = data.config.caption_style;+ if (data.config.crop_strategy) settings.cropStrategy = data.config.crop_strategy;+ if (data.config.logo_path) settings.logoPath = data.config.logo_path;+ if (data.config.outro_path) settings.outroPath = data.config.outro_path;+ if (Object.keys(settings).length) {+ await fetch("http://localhost:3847/api/ui-state", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ settings }),+ });+ }+ return { content: [{ type: "text" as const, text: `Loaded preset "${name}" and applied to UI settings.` }] };+ }++ if (action === "save") {+ return { content: [{ type: "text" as const, text: `Saved preset "${name}".` }] };+ }++ if (action === "delete") {+ return { content: [{ type: "text" as const, text: `Deleted preset "${name}".` }] };+ }++ return { content: [{ type: "text" as const, text: JSON.stringify(data) }] };+ } catch (err: unknown) {+ const msg = err instanceof Error ? err.message : String(err);+ if (msg.includes("ECONNREFUSED") || msg.includes("fetch failed")) {+ return { content: [{ type: "text" as const, text: "Web UI is not running. Start with: npm run ui" }] };+ }+ return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };+ }+ }+ );++ // =============================================+ // Tool: analyze_energy+ // =============================================+ server.tool(+ "analyze_energy",+ "Analyze audio energy levels for a video or specific segments. Useful for finding high-energy moments. Defaults to the current UI video and suggestions if not specified.",+ {+ video_path: z.string().optional().describe("Path to video file (defaults to current UI video)"),+ segments: z.array(z.object({+ start: z.number(),+ end: z.number(),+ })).optional().describe("Specific segments to analyze (defaults to current suggestions)"),+ },+ async ({ video_path, segments }) => {+ try {+ let vPath = video_path;+ let segs = segments;++ // If no video_path, read from UI state+ if (!vPath || !segs) {+ const stateRes = await fetch("http://localhost:3847/api/ui-state");+ if (stateRes.ok) {+ const state = await stateRes.json() as any;+ if (!vPath) vPath = state.videoPath || state.filePath;+ if (!segs) {+ const suggestions: any[] = state.suggestions || [];+ segs = suggestions.map((s: any) => ({ start: s.start_second, end: s.end_second }));+ }+ }+ }++ if (!vPath) {+ return { content: [{ type: "text" as const, text: "No video path. Set a video first or provide video_path." }] };+ }++ if (!segs || segs.length === 0) {+ return { content: [{ type: "text" as const, text: "No segments to analyze. Provide segments explicitly or suggest clips first." }] };+ }++ const res = await fetch("http://localhost:3847/api/analyze-energy", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ video_path: vPath, segments: segs }),+ });+ if (!res.ok) throw new Error(`HTTP ${res.status}`);+ const data = await res.json() as any;++ if (data.error) {+ return { content: [{ type: "text" as const, text: `Error: ${data.error}` }], isError: true };+ }++ return { content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }] };+ } catch (err: unknown) {+ const msg = err instanceof Error ? err.message : String(err);+ if (msg.includes("ECONNREFUSED") || msg.includes("fetch failed")) {+ return { content: [{ type: "text" as const, text: "Web UI is not running. Start with: npm run ui" }] };+ }+ return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };+ }+ }+ );++ // =============================================+ // Tool: set_video+ // =============================================+ server.tool(+ "set_video",+ "Set the working video file in the UI without transcribing it. Validates the file exists and updates the UI state.",+ {+ file_path: z.string().describe("Absolute path to the video file"),+ },+ async ({ file_path }) => {+ try {+ // Validate via select-file endpoint+ const selectRes = await fetch("http://localhost:3847/api/select-file", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ file_path }),+ });+ if (!selectRes.ok) {+ const err = await selectRes.json() as any;+ return { content: [{ type: "text" as const, text: `Error: ${err.error || "File not found"}` }], isError: true };+ }+ const fileInfo = await selectRes.json() as any;++ // Push to UI state+ await fetch("http://localhost:3847/api/ui-state", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ videoPath: file_path, filePath: file_path }),+ });++ return {+ content: [{+ type: "text" as const,+ text: `Video set: ${fileInfo.filename} (${fileInfo.size_mb} MB). Use transcribe_podcast to transcribe it.`,+ }],+ };+ } catch (err: unknown) {+ const msg = err instanceof Error ? err.message : String(err);+ if (msg.includes("ECONNREFUSED") || msg.includes("fetch failed")) {+ return { content: [{ type: "text" as const, text: "Web UI is not running. Start with: npm run ui" }] };+ }+ return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };+ }+ }+ );++ // =============================================+ // Tool: import_transcript+ // =============================================+ server.tool(+ "import_transcript",+ "Import an external transcript (e.g. from a transcription service) into the UI. Skips Whisper entirely. The transcript must include word-level timestamps.",+ {+ file_path: z.string().describe("Path to the video file the transcript belongs to"),+ transcript: z.object({+ words: z.array(z.object({+ word: z.string(),+ start: z.number(),+ end: z.number(),+ speaker: z.string().optional(),+ })).describe("Word-level timestamps"),+ segments: z.array(z.object({+ text: z.string(),+ start: z.number(),+ end: z.number(),+ speaker: z.string().optional(),+ })).optional().describe("Segment-level transcript"),+ duration: z.number().optional().describe("Total duration in seconds"),+ language: z.string().optional().describe("ISO language code"),+ text: z.string().optional().describe("Full transcript text"),+ }).describe("Transcript data with word-level timestamps"),+ },+ async ({ file_path, transcript }) => {+ try {+ const res = await fetch("http://localhost:3847/api/import-transcript", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ file_path, transcript }),+ });+ if (!res.ok) {+ const err = await res.json() as any;+ return { content: [{ type: "text" as const, text: `Error: ${err.error || "Import failed"}` }], isError: true };+ }+ const result = await res.json() as any;++ // Push transcript to UI state+ await fetch("http://localhost:3847/api/ui-state", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({+ transcript: result.data,+ rawTranscriptText: result.data?.transcript || "",+ phase: "review",+ videoPath: file_path,+ filePath: file_path,+ }),+ });++ const wordCount = result.data?.words?.length || 0;+ const duration = result.data?.duration || 0;+ return {+ content: [{+ type: "text" as const,+ text: `Transcript imported: ${wordCount} words, ${Math.round(duration)}s duration. UI updated to review phase.`,+ }],+ };+ } catch (err: unknown) {+ const msg = err instanceof Error ? err.message : String(err);+ if (msg.includes("ECONNREFUSED") || msg.includes("fetch failed")) {+ return { content: [{ type: "text" as const, text: "Web UI is not running. Start with: npm run ui" }] };+ }+ return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };+ }+ }+ );++ // =============================================+ // Tool: parse_transcript+ // =============================================+ server.tool(+ "parse_transcript",+ "Parse a raw speaker-labeled plain text transcript into word-level timestamps. Input format: 'Speaker (MM:SS)\\ntext...\\n\\nSpeaker2 (MM:SS)\\ntext...'. Uses the Python backend to generate accurate word timings.",+ {+ file_path: z.string().describe("Path to the video file the transcript belongs to"),+ raw_text: z.string().describe("Raw speaker-labeled transcript text"),+ total_duration: z.number().optional().describe("Total video duration in seconds (helps accuracy)"),+ time_adjust: z.number().optional().default(0).describe("Offset in seconds to add to all timestamps"),+ },+ async ({ file_path, raw_text, total_duration, time_adjust }) => {+ try {+ const res = await fetch("http://localhost:3847/api/parse-transcript", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ file_path, raw_text, total_duration, time_adjust }),+ });+ if (!res.ok) {+ const err = await res.json() as any;+ return { content: [{ type: "text" as const, text: `Error: ${err.error || "Parse failed"}` }], isError: true };+ }+ const result = await res.json() as any;++ // Push parsed transcript to UI state+ await fetch("http://localhost:3847/api/ui-state", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({+ transcript: result.data,+ rawTranscriptText: result.data?.transcript || "",+ phase: "review",+ videoPath: file_path,+ filePath: file_path,+ }),+ });++ const wordCount = result.data?.words?.length || 0;+ const segCount = result.data?.segments?.length || 0;+ return {+ content: [{+ type: "text" as const,+ text: `Transcript parsed: ${wordCount} words, ${segCount} segments. UI updated to review phase.`,+ }],+ };+ } catch (err: unknown) {+ const msg = err instanceof Error ? err.message : String(err);+ if (msg.includes("ECONNREFUSED") || msg.includes("fetch failed")) {+ return { content: [{ type: "text" as const, text: "Web UI is not running. Start with: npm run ui" }] };+ }+ return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };+ }+ }+ );+return server;}diff --git a/src/ui/public/index.html b/src/ui/public/index.htmlindex cffa1c9..fe25735 100644--- a/src/ui/public/index.html+++ b/src/ui/public/index.html@@ -232,6 +232,51 @@.row { display: flex; gap: 10px; align-items: flex-end; }.row > * { flex: 1; }+ /* ─── Asset pills ─── */+ .asset-pill {+ display: inline-flex; align-items: center; gap: 6px;+ padding: 5px 8px 5px 10px; background: var(--surface2);+ border: 1px solid var(--border); border-radius: 20px;+ font-size: 12px; color: var(--text); max-width: 200px;+ }+ .asset-pill-icon { display: flex; align-items: center; flex-shrink: 0; color: var(--text2); }+ .asset-pill-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }+ .asset-pill-x {+ display: flex; align-items: center; justify-content: center;+ width: 18px; height: 18px; border-radius: 50%; border: none;+ background: transparent; color: var(--text3); font-size: 14px;+ cursor: pointer; flex-shrink: 0; padding: 0; line-height: 1;+ transition: all 0.15s var(--ease);+ }+ .asset-pill-x:hover { background: var(--surface); color: var(--red); }+ .asset-add {+ display: inline-flex; align-items: center; gap: 4px;+ padding: 5px 12px; background: transparent;+ border: 1px dashed var(--border); border-radius: 20px;+ font-size: 12px; color: var(--text3); cursor: pointer;+ font-family: inherit; font-weight: 500;+ transition: all 0.15s var(--ease);+ }+ .asset-add:hover:not(:disabled) { border-color: var(--border-hover); color: var(--text2); }++ /* ─── Overflow menu ─── */+ .overflow-menu {+ position: absolute; right: 0; top: calc(100% + 4px); z-index: 50;+ background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);+ padding: 4px; min-width: 120px; box-shadow: 0 8px 24px rgba(0,0,0,0.4);+ }+ .overflow-menu-label {+ padding: 6px 10px 4px; font-size: 10px; font-weight: 600;+ color: var(--text3); text-transform: uppercase; letter-spacing: 0.5px;+ }+ .overflow-menu-item {+ display: block; width: 100%; text-align: left; padding: 6px 10px;+ background: none; border: none; color: var(--text2); font-size: 12px;+ font-family: inherit; cursor: pointer; border-radius: 6px;+ transition: all 0.1s var(--ease);+ }+ .overflow-menu-item:hover { background: var(--surface2); color: var(--text); }+/* ─── Progress ─── */.progress-track { background: var(--surface2); border-radius: 4px; height: 4px; overflow: hidden; margin: 8px 0; }.progress-fill { height: 100%; background: var(--accent); border-radius: 4px; transition: width 0.5s var(--ease); }@@ -475,9 +520,13 @@const [timeAdjust, setTimeAdjust] = useState(-1);const [whisperModel, setWhisperModel] = useState('base');const [captionStyle, setCaptionStyle] = useState('branded');- const [cropStrategy, setCropStrategy] = useState('center');+ const [cropStrategy, setCropStrategy] = useState('face');const [logoPath, setLogoPath] = useState('');const logoRef = useRef();+ const [outroPath, setOutroPath] = useState('');+ const initializedRef = useRef(false);+ const outroRef = useRef();+ const [outroUploading, setOutroUploading] = useState(false);const [transcriptDragOver, setTranscriptDragOver] = useState(false);const [transcriptFileName, setTranscriptFileName] = useState('');@@ -500,34 +549,44 @@const [logoUploading, setLogoUploading] = useState(false);const [browsing, setBrowsing] = useState(false);+ const [clipHistory, setClipHistory] = useState([]);+ const [historyOpen, setHistoryOpen] = useState(false);const [encoderInfo, setEncoderInfo] = useState(null);useEffect(() => { api('/encoder-info').then(d => { if(d.best) setEncoderInfo(d); }).catch(()=>{}); }, []);+ // Fetch clip history on mount and after exports+ const fetchHistory = () => { fetch('/api/history?limit=50').then(r=>r.json()).then(d=>{ if(Array.isArray(d)) setClipHistory(d); }).catch(()=>{}); };+ useEffect(fetchHistory, []);+ useEffect(() => { if (phase === 'done') fetchHistory(); }, [phase]);+// --- MCP ↔ UI Bridge: connect SSE + sync state ---const { lastEvent: sseEvent, connected: mcpConnected } = useSSE();// Sync UI state to server on changes (fire-and-forget)+ // Guard: don't sync until initial SSE state has been received to avoid overwriting persisted state with defaultsconst prevSyncRef = useRef('');useEffect(() => {+ if (!initializedRef.current) return;const state = {_source: 'ui',videoPath,filePath: file?.file_path || '',suggestions,deselectedIndices: Array.from(deselected),- settings: { captionStyle, cropStrategy, logoPath },+ settings: { captionStyle, cropStrategy, logoPath, outroPath },phase,};const key = JSON.stringify(state);if (key === prevSyncRef.current) return;prevSyncRef.current = key;fetch('/api/ui-state', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: key }).catch(() => {});- }, [videoPath, file, suggestions, deselected, captionStyle, cropStrategy, logoPath, phase]);+ }, [videoPath, file, suggestions, deselected, captionStyle, cropStrategy, logoPath, outroPath, phase]);// Sync transcript separately (large payload)const prevTranscriptRef = useRef(null);useEffect(() => {+ if (!initializedRef.current) return;if (!transcript || transcript === prevTranscriptRef.current) return;prevTranscriptRef.current = transcript;fetch('/api/ui-state', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ _source: 'ui', transcript }) }).catch(() => {});@@ -536,6 +595,7 @@// Sync raw transcript text so MCP can read it before pipeline runsconst prevRawRef = useRef('');useEffect(() => {+ if (!initializedRef.current) return;if (transcriptText === prevRawRef.current) return;prevRawRef.current = transcriptText;if (transcriptText.trim()) {@@ -549,16 +609,24 @@if (!sseEvent || sseEvent.ts === lastHandledTsRef.current) return;lastHandledTsRef.current = sseEvent.ts;- if (sseEvent.type === 'state-sync') {+ if (sseEvent.type === 'state-sync' || sseEvent.type === 'state') {const d = sseEvent.data;- if (d.suggestions) { setSuggestions(d.suggestions); setDeselected(new Set()); }+ if (d.suggestions) setSuggestions(d.suggestions);+ if (d.deselectedIndices !== undefined) setDeselected(new Set(d.deselectedIndices));+ else if (d.suggestions && !d.deselectedIndices) setDeselected(new Set());if (d.phase) setPhase(d.phase);if (d.videoPath !== undefined) setVideoPath(d.videoPath);if (d.transcript) setTranscript(d.transcript);+ if (d.rawTranscriptText && !transcript) setTranscriptText(d.rawTranscriptText);if (d.settings) {if (d.settings.captionStyle) setCaptionStyle(d.settings.captionStyle);if (d.settings.cropStrategy) setCropStrategy(d.settings.cropStrategy);if (d.settings.logoPath !== undefined) setLogoPath(d.settings.logoPath);+ if (d.settings.outroPath !== undefined) setOutroPath(d.settings.outroPath);+ }+ // Mark initialized after first state restoration so sync useEffects don't overwrite with defaults+ if (sseEvent.type === 'state') {+ initializedRef.current = true;}} else if (sseEvent.type === 'export-started') {setBatchJobId(sseEvent.data.jobId);@@ -689,7 +757,7 @@const data = await api('/batch-clips', { method:'POST', body: JSON.stringify({video_path: file.file_path,clips: sc.map(c => ({ start_second: c.start_second, end_second: c.end_second, title: c.title.slice(0,40), caption_style: captionStyle, crop_strategy: cropStrategy })),- transcript_words: transcript?.words || [], logo_path: logoPath || undefined,+ transcript_words: transcript?.words || [], logo_path: logoPath || undefined, outro_path: outroPath || undefined,})});setBatchJobId(data.job_id);};@@ -705,7 +773,7 @@const data = await api('/create-clip', { method:'POST', body: JSON.stringify({video_path: file.file_path, start_second: c.start_second, end_second: c.end_second,title: c.title.slice(0,40), caption_style: captionStyle, crop_strategy: cropStrategy,- transcript_words: transcript?.words || [], logo_path: logoPath || undefined,+ transcript_words: transcript?.words || [], logo_path: logoPath || undefined, outro_path: outroPath || undefined,})});setRetryJobId(data.job_id);};@@ -725,6 +793,12 @@catch(e) { setError('Logo upload failed'); }finally { setLogoUploading(false); }};+ const handleOutroUpload = async (f) => {+ setOutroUploading(true);+ try { const d = await uploadFile(f, ()=>{}); if(d.file_path) setOutroPath(d.file_path); }+ catch(e) { setError('Outro upload failed'); }+ finally { setOutroUploading(false); }+ };const isProcessing = phase === 'parsing' || phase === 'suggesting' || phase === 'exporting' || whisperJobId;const selectedClips = suggestions.filter((_,i) => !deselected.has(i));@@ -871,17 +945,39 @@</select></div></div>- {captionStyle === 'branded' && (- <div className="fade-in" style={{display:'flex',gap:8,alignItems:'center'}}>- <input type="text" placeholder="Logo image path (optional)" value={logoPath} onChange={e=>setLogoPath(e.target.value)}- style={{flex:1}} disabled={isProcessing||logoUploading} />- <button className="btn btn-ghost btn-sm" onClick={()=>logoRef.current?.click()} disabled={isProcessing||logoUploading} style={{minWidth:72}}>- {logoUploading ? <><div className="spinner sm"/><span style={{fontSize:11}}>wait</span></> : 'Upload'}+ <div style={{display:'flex',gap:8,flexWrap:'wrap'}}>+ {captionStyle === 'branded' && (+ logoPath ? (+ <div className="asset-pill fade-in">+ <span className="asset-pill-icon">+ <img src={`/api/stream-source?path=${encodeURIComponent(logoPath)}`}+ style={{width:16,height:16,objectFit:'contain',borderRadius:3}} onError={e=>{e.target.style.display='none'}} />+ </span>+ <span className="asset-pill-name">{logoPath.split('/').pop()}</span>+ <button className="asset-pill-x" onClick={()=>setLogoPath('')} disabled={isProcessing}>{'\u00D7'}</button>+ </div>+ ) : (+ <button className="asset-add fade-in" onClick={()=>logoRef.current?.click()} disabled={isProcessing||logoUploading}>+ {logoUploading ? <div className="spinner sm"/> : '+'} Logo+ </button>+ )+ )}+ {outroPath ? (+ <div className="asset-pill">+ <span className="asset-pill-icon" style={{fontSize:11}}>{'▶'}</span>+ <span className="asset-pill-name">{outroPath.split('/').pop()}</span>+ <button className="asset-pill-x" onClick={()=>setOutroPath('')} disabled={isProcessing}>{'\u00D7'}</button>+ </div>+ ) : (+ <button className="asset-add" onClick={()=>outroRef.current?.click()} disabled={isProcessing||outroUploading}>+ {outroUploading ? <div className="spinner sm"/> : '+'} Outro</button>- <input ref={logoRef} type="file" accept=".png,.jpg,.jpeg,.svg" style={{display:'none'}}- onChange={e=>e.target.files[0]&&handleLogoUpload(e.target.files[0])} />- </div>- )}+ )}+ <input ref={logoRef} type="file" accept=".png,.jpg,.jpeg,.svg" style={{display:'none'}}+ onChange={e=>e.target.files[0]&&handleLogoUpload(e.target.files[0])} />+ <input ref={outroRef} type="file" accept=".mp4,.mov,.mkv,.webm" style={{display:'none'}}+ onChange={e=>e.target.files[0]&&handleOutroUpload(e.target.files[0])} />+ </div></div>{/* Error */}@@ -944,11 +1040,28 @@{phase==='suggesting' ? `Found ${suggestions.length} clip${suggestions.length!==1?'s':''}`: phase==='review' ? `Clips \u00B7 ${selectedCount} selected` : 'Clips'}</div>- {phase === 'review' && (- <button className="btn btn-primary btn-sm" disabled={selectedCount===0} onClick={startExport}>- Export {selectedCount} clip{selectedCount!==1?'s':''}- </button>- )}+ <div style={{display:'flex',gap:6,alignItems:'center'}}>+ {phase === 'review' && (+ <button className="btn btn-primary btn-sm" disabled={selectedCount===0} onClick={startExport}>+ Export {selectedCount} clip{selectedCount!==1?'s':''}+ </button>+ )}+ {transcript && (phase === 'review' || phase === 'done') && (+ <div style={{position:'relative'}}>+ <button className="btn btn-ghost btn-sm overflow-menu-btn" onClick={e=>{const m=e.currentTarget.nextElementSibling;m.style.display=m.style.display==='block'?'none':'block';}} style={{padding:'4px 8px',fontSize:14,color:'var(--text3)',lineHeight:1}}>+ {'\u22EF'}+ </button>+ <div className="overflow-menu" style={{display:'none'}}>+ <div className="overflow-menu-label">Export transcript</div>+ {['SRT','VTT','JSON'].map(fmt => (+ <button key={fmt} className="overflow-menu-item" onClick={e=>{window.open(`/api/export-transcript?format=${fmt.toLowerCase()}`,'_blank');e.currentTarget.closest('.overflow-menu').style.display='none';}}>+ {fmt}+ </button>+ ))}+ </div>+ </div>+ )}+ </div></div>{suggestions.map((clip, i) => {@@ -1034,6 +1147,38 @@)}</div>)}+ {/* ═══════════ CLIP HISTORY ═══════════ */}+ {clipHistory.length > 0 && (+ <div className="section" style={{marginTop:16}}>+ <div className="section-label" style={{cursor:'pointer',userSelect:'none',display:'flex',justifyContent:'space-between',alignItems:'center'}}+ onClick={()=>setHistoryOpen(!historyOpen)}>+ <span>History ({clipHistory.length})</span>+ <span style={{fontSize:10,color:'var(--text3)',transition:'transform 0.2s',transform:historyOpen?'rotate(180deg)':'rotate(0)'}}>{'\u25BC'}</span>+ </div>+ {historyOpen && (+ <div className="fade-in" style={{display:'flex',flexDirection:'column',gap:4,maxHeight:300,overflowY:'auto',marginTop:8}}>+ {clipHistory.map((c,i) => {+ const date = new Date(c.created_at);+ const ago = ((Date.now() - date.getTime()) / 3600000);+ const timeStr = ago < 1 ? `${Math.round(ago*60)}m ago` : ago < 24 ? `${Math.round(ago)}h ago` : date.toLocaleDateString();+ const fname = c.output_path?.split('/').pop() || c.title;+ return (+ <div key={c.id||i} style={{display:'flex',alignItems:'center',gap:8,padding:'8px 10px',background:'var(--surface)',borderRadius:'var(--radius-sm)',fontSize:12,cursor:'pointer'}}+ onClick={()=>{ const f=c.output_path?.split('/').pop(); if(f) setPreviewSrc(f); }}>+ <div style={{width:6,height:6,borderRadius:3,background:'var(--green)',flexShrink:0}}/>+ <div style={{flex:1,minWidth:0}}>+ <div style={{fontWeight:600,overflow:'hidden',textOverflow:'ellipsis',whiteSpace:'nowrap'}}>{c.title || fname}</div>+ <div style={{color:'var(--text3)',fontSize:11,marginTop:2}}>+ {c.duration}s {'\u00B7'} {c.file_size_mb?.toFixed(1)}MB {'\u00B7'} {c.caption_style} {'\u00B7'} {timeStr}+ </div>+ </div>+ </div>+ );+ })}+ </div>+ )}+ </div>+ )}</div>{/* ═══════════ RIGHT COLUMN — PREVIEW ═══════════ */}@@ -1066,8 +1211,8 @@</div>)}- {/* Style preview mockup — always visible */}- <StylePreview style={captionStyle} logoPath={logoPath} />+ {/* Style preview mockup — hidden when rendered clip is playing */}+ {!previewSrc && <StylePreview style={captionStyle} logoPath={logoPath} />}{/* Settings tags */}<div className="preview-settings">@@ -1084,6 +1229,11 @@Logo {'\u00B7'} <strong>{logoPath.split('/').pop()}</strong></div>)}+ {outroPath && (+ <div className="preview-tag">+ Outro {'\u00B7'} <strong>{outroPath.split('/').pop()}</strong>+ </div>+ )}</div></div></div>diff --git a/src/ui/web-server.ts b/src/ui/web-server.tsindex e0e9212..46adc76 100644--- a/src/ui/web-server.ts+++ b/src/ui/web-server.ts@@ -11,7 +11,7 @@import express from "express";import multer from "multer";-import { createReadStream, existsSync, statSync } from "fs";+import { createReadStream, existsSync, statSync, readFileSync, writeFileSync } from "fs";import { mkdir, readdir, unlink } from "fs/promises";import { join, dirname, basename, extname } from "path";import { execSync } from "child_process";@@ -67,22 +67,68 @@ interface UIState {captionStyle: string;cropStrategy: string;logoPath: string;+ outroPath: string;};phase: string;lastUpdated: number;}-const uiState: UIState = {- videoPath: "",- filePath: "",- transcript: null,- rawTranscriptText: "",- suggestions: [],- deselectedIndices: [],- settings: { captionStyle: "branded", cropStrategy: "center", logoPath: "" },- phase: "idle",- lastUpdated: 0,-};+// Load persisted state or use defaults+function loadPersistedState(): UIState {+ try {+ if (existsSync(paths.uiState)) {+ const raw = readFileSync(paths.uiState, "utf-8");+ const saved = JSON.parse(raw);+ // Validate video still exists+ if (saved.videoPath && !existsSync(saved.videoPath)) {+ saved.videoPath = "";+ saved.filePath = "";+ saved.phase = "idle";+ }+ return {+ videoPath: saved.videoPath || "",+ filePath: saved.filePath || "",+ transcript: saved.transcript || null,+ rawTranscriptText: saved.rawTranscriptText || "",+ suggestions: saved.suggestions || [],+ deselectedIndices: saved.deselectedIndices || [],+ settings: {+ captionStyle: saved.settings?.captionStyle || "branded",+ cropStrategy: saved.settings?.cropStrategy || "face",+ logoPath: saved.settings?.logoPath || "",+ outroPath: saved.settings?.outroPath || "",+ },+ // Never restore mid-export phases+ phase: ["exporting", "parsing", "suggesting"].includes(saved.phase) ? "idle" : (saved.phase || "idle"),+ lastUpdated: saved.lastUpdated || 0,+ };+ }+ } catch {}+ return {+ videoPath: "",+ filePath: "",+ transcript: null,+ rawTranscriptText: "",+ suggestions: [],+ deselectedIndices: [],+ settings: { captionStyle: "branded", cropStrategy: "face", logoPath: "", outroPath: "" },+ phase: "idle",+ lastUpdated: 0,+ };+}++const uiState: UIState = loadPersistedState();++// Debounced save to disk+let saveTimer: ReturnType<typeof setTimeout> | null = null;+function persistState() {+ if (saveTimer) clearTimeout(saveTimer);+ saveTimer = setTimeout(() => {+ try {+ writeFileSync(paths.uiState, JSON.stringify(uiState, null, 2));+ } catch {}+ }, 500);+}// SSE clients for the global event busimport type { Response } from "express";@@ -357,10 +403,11 @@ app.post("/api/create-clip", async (req, res) => {start_second,end_second,caption_style = "hormozi",- crop_strategy = "center",+ crop_strategy = "face",transcript_words = [],title = "clip",logo_path = null,+ outro_path = null,} = req.body;if (!video_path || !existsSync(video_path)) {@@ -368,6 +415,39 @@ app.post("/api/create-clip", async (req, res) => {return;}+ // Validate clip params before spawning Python+ if (typeof start_second !== "number" || typeof end_second !== "number") {+ res.status(400).json({ error: "start_second and end_second must be numbers" });+ return;+ }+ if (end_second <= start_second) {+ res.status(400).json({ error: "end_second must be greater than start_second" });+ return;+ }+ const duration = end_second - start_second;+ if (duration > 180) {+ res.status(400).json({ error: `Clip too long (${Math.round(duration)}s). Max 180 seconds.` });+ return;+ }+ if (logo_path && !existsSync(logo_path)) {+ res.status(400).json({ error: `Logo file not found: ${logo_path}` });+ return;+ }+ if (outro_path && !existsSync(outro_path)) {+ res.status(400).json({ error: `Outro file not found: ${outro_path}` });+ return;+ }+ const validStyles = ["hormozi", "karaoke", "subtle", "branded"];+ if (!validStyles.includes(caption_style)) {+ res.status(400).json({ error: `Invalid caption style. Use: ${validStyles.join(", ")}` });+ return;+ }+ const validCrops = ["center", "face"];+ if (!validCrops.includes(crop_strategy)) {+ res.status(400).json({ error: `Invalid crop strategy. Use: ${validCrops.join(", ")}` });+ return;+ }+await fileManager.ensureDirectories();const jobId = uuidv4();@@ -396,6 +476,7 @@ app.post("/api/create-clip", async (req, res) => {title,output_dir: paths.output,logo_path,+ outro_path,},(event) => {job.progress = event.percent;@@ -434,12 +515,37 @@ app.post("/api/create-clip", async (req, res) => {* POST /api/batch-clips — Create multiple clips*/app.post("/api/batch-clips", async (req, res) => {- const { video_path, clips, transcript_words = [], logo_path = null } = req.body;+ const { video_path, clips, transcript_words = [], logo_path = null, outro_path = null } = req.body;if (!video_path || !existsSync(video_path)) {res.status(400).json({ error: "Video file not found" });return;}+ if (!clips || !Array.isArray(clips) || clips.length === 0) {+ res.status(400).json({ error: "No clips provided" });+ return;+ }+ // Validate each clip's timing+ for (let i = 0; i < clips.length; i++) {+ const c = clips[i];+ const dur = (c.end_second || 0) - (c.start_second || 0);+ if (dur <= 0) {+ res.status(400).json({ error: `Clip ${i + 1}: end must be after start` });+ return;+ }+ if (dur > 180) {+ res.status(400).json({ error: `Clip ${i + 1}: too long (${Math.round(dur)}s). Max 180s.` });+ return;+ }+ }+ if (logo_path && !existsSync(logo_path)) {+ res.status(400).json({ error: `Logo file not found: ${logo_path}` });+ return;+ }+ if (outro_path && !existsSync(outro_path)) {+ res.status(400).json({ error: `Outro file not found: ${outro_path}` });+ return;+ }await fileManager.ensureDirectories();@@ -459,7 +565,7 @@ app.post("/api/batch-clips", async (req, res) => {executor.execute("batch_clips",- { video_path, clips, transcript_words, output_dir: paths.output, logo_path },+ { video_path, clips, transcript_words, output_dir: paths.output, logo_path, outro_path },(event) => {job.progress = event.percent;job.message = event.message;@@ -482,7 +588,7 @@ app.post("/api/batch-clips", async (req, res) => {start_second: r.start_second || 0,end_second: r.end_second || 0,caption_style: r.caption_style || "hormozi",- crop_strategy: r.crop_strategy || "center",+ crop_strategy: r.crop_strategy || "face",title: r.title || "clip",output_path: r.output_path,file_size_mb: r.file_size_mb || 0,@@ -693,6 +799,63 @@ app.get("/api/integration-info", (_req, res) => {});});+// --- Transcript export (SRT/VTT) ---+app.get("/api/export-transcript", (_req, res) => {+ const format = (_req.query.format as string) || "srt";+ const transcript = uiState.transcript as any;++ if (!transcript?.words?.length) {+ res.status(400).json({ error: "No transcript available" });+ return;+ }++ const words = transcript.words;++ // Group words into subtitle lines (~8 words each)+ const lineSize = 8;+ const lines: Array<{ text: string; start: number; end: number }> = [];+ for (let i = 0; i < words.length; i += lineSize) {+ const chunk = words.slice(i, i + lineSize);+ lines.push({+ text: chunk.map((w: any) => w.word).join(" "),+ start: chunk[0].start,+ end: chunk[chunk.length - 1].end,+ });+ }++ const fmtSrt = (s: number) => {+ const h = Math.floor(s / 3600);+ const m = Math.floor((s % 3600) / 60);+ const sec = Math.floor(s % 60);+ const ms = Math.round((s % 1) * 1000);+ return `${String(h).padStart(2, "0")}:${String(m).padStart(2, "0")}:${String(sec).padStart(2, "0")},${String(ms).padStart(3, "0")}`;+ };++ const fmtVtt = (s: number) => fmtSrt(s).replace(",", ".");++ if (format === "vtt") {+ let vtt = "WEBVTT\n\n";+ lines.forEach((line, i) => {+ vtt += `${i + 1}\n${fmtVtt(line.start)} --> ${fmtVtt(line.end)}\n${line.text}\n\n`;+ });+ res.setHeader("Content-Type", "text/vtt");+ res.setHeader("Content-Disposition", "attachment; filename=transcript.vtt");+ res.send(vtt);+ } else if (format === "json") {+ res.setHeader("Content-Disposition", "attachment; filename=transcript.json");+ res.json(transcript);+ } else {+ // SRT+ let srt = "";+ lines.forEach((line, i) => {+ srt += `${i + 1}\n${fmtSrt(line.start)} --> ${fmtSrt(line.end)}\n${line.text}\n\n`;+ });+ res.setHeader("Content-Type", "application/x-subrip");+ res.setHeader("Content-Disposition", "attachment; filename=transcript.srt");+ res.send(srt);+ }+});+// --- Analyze audio energy ---app.post("/api/analyze-energy", async (req, res) => {const { video_path, segments } = req.body;@@ -780,7 +943,7 @@ app.get("/api/history", async (req, res) => {app.get("/api/history/check", async (req, res) => {try {- const { source, start, end, style = "hormozi", crop = "center" } = req.query;+ const { source, start, end, style = "hormozi", crop = "face" } = req.query;if (!source || !start || !end) {res.json({ duplicate: null });return;@@ -912,6 +1075,8 @@ app.get("/api/ui-state", (_req, res) => {phase: uiState.phase,settings: uiState.settings,selectedClips: selected,+ suggestions: uiState.suggestions,+ deselectedIndices: uiState.deselectedIndices,totalSuggestions: uiState.suggestions.length,deselectedCount: uiState.deselectedIndices.length,transcriptWordCount: Array.isArray((uiState.transcript as any)?.words)@@ -942,8 +1107,10 @@ app.post("/api/ui-state", (req, res) => {if (body.settings.captionStyle !== undefined) uiState.settings.captionStyle = body.settings.captionStyle;if (body.settings.cropStrategy !== undefined) uiState.settings.cropStrategy = body.settings.cropStrategy;if (body.settings.logoPath !== undefined) uiState.settings.logoPath = body.settings.logoPath;+ if (body.settings.outroPath !== undefined) uiState.settings.outroPath = body.settings.outroPath;}uiState.lastUpdated = Date.now();+ persistState();// Broadcast to UI when changes come from MCP (not from UI itself)if (source !== "ui") {@@ -951,6 +1118,7 @@ app.post("/api/ui-state", (req, res) => {...(body.videoPath !== undefined && { videoPath: body.videoPath }),...(body.filePath !== undefined && { filePath: body.filePath }),...(body.suggestions !== undefined && { suggestions: body.suggestions }),+ ...(body.deselectedIndices !== undefined && { deselectedIndices: body.deselectedIndices }),...(body.phase !== undefined && { phase: body.phase }),...(body.transcript !== undefined && { transcript: body.transcript }),...(body.settings && { settings: body.settings }),@@ -972,8 +1140,9 @@ app.post("/api/mcp/export", async (req, res) => {const transcriptWords = req.body.transcript_words ||(Array.isArray((uiState.transcript as any)?.words) ? (uiState.transcript as any).words : []);const logoPath = req.body.logo_path || uiState.settings.logoPath || null;+ const outroPath = req.body.outro_path || (uiState.settings as any).outroPath || null;const captionStyle = req.body.caption_style || uiState.settings.captionStyle || "branded";- const cropStrategy = req.body.crop_strategy || uiState.settings.cropStrategy || "center";+ const cropStrategy = req.body.crop_strategy || uiState.settings.cropStrategy || "face";if (!videoPath || !existsSync(videoPath)) {res.status(400).json({ error: "Video file not found" });@@ -1010,13 +1179,14 @@ app.post("/api/mcp/export", async (req, res) => {broadcastSSE("export-started", { jobId, clipCount: styledClips.length });uiState.phase = "exporting";uiState.lastUpdated = Date.now();+ persistState();res.json({ job_id: jobId, status: "running", clipCount: styledClips.length });executor.execute("batch_clips",- { video_path: videoPath, clips: styledClips, transcript_words: transcriptWords, output_dir: paths.output, logo_path: logoPath },+ { video_path: videoPath, clips: styledClips, transcript_words: transcriptWords, output_dir: paths.output, logo_path: logoPath, outro_path: outroPath },(event) => {job.progress = event.percent;job.message = event.message;@@ -1065,6 +1235,12 @@ async function main() {await knowledgeBase.ensureDir();await mkdir(uploadDir, { recursive: true });+ // Cleanup old temp files on startup (>48h)+ try {+ const cleaned = await fileManager.cleanupOldTasks(48);+ if (cleaned > 0) console.log(` Cleaned up ${cleaned} old temp files`);+ } catch {}+app.listen(PORT, () => {console.log(`\n 🎬 podcli running at:`);console.log(` ➜ http://localhost:${PORT}\n`);