Skip to content

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

refactor: single source of truth for suggestion and content generation

Nika Siradze75d5f4ae7f82
diff --git a/backend/cli.py b/backend/cli.py
index 3269bd9..7a08639 100644
--- a/backend/cli.py
+++ b/backend/cli.py
@@ -231,7 +231,7 @@ def cmd_process(args):
pass
if args.transcript:
- print(" [1/5] Loading transcript...")
+ print(" [1/4] Loading transcript...")
with open(args.transcript, "r") as f:
raw_text = f.read()
@@ -259,13 +259,13 @@ def cmd_process(args):
# Check cache first
cached = _load_cache(video_path)
if cached and not config.get("no_cache", False):
- print(" [1/5] Loaded from cache (instant)")
+ print(" [1/4] Loaded from cache (instant)")
words = cached["words"]
segments = cached["segments"]
result = cached
print(f" {len(segments)} segments, {len(words)} words")
else:
- print(" [1/5] Transcribing with Whisper...")
+ print(" [1/4] Transcribing with Whisper...")
_ensure_ssl_certs()
import warnings
warnings.filterwarnings("ignore", message="FP16 is not supported on CPU")
@@ -328,7 +328,7 @@ def cmd_process(args):
# ── Step 2: Analyze audio energy ──
energy_scores = None
if config.get("energy_boost", True):
- print(" [2/5] Analyzing audio energy...")
+ print(" [2/4] Analyzing audio energy...")
try:
profile = get_energy_profile(video_path, segments)
energy_scores = profile["segment_scores"]
@@ -336,31 +336,31 @@ def cmd_process(args):
except Exception as e:
print(f" Skipped (error: {e})")
else:
- print(" [2/5] Audio analysis skipped (--no-energy)")
+ print(" [2/4] Audio analysis skipped (--no-energy)")
# ── Step 3: Score and select clips ──
top_n = config.get("top_clips", 5)
clips = None
# Try Claude first (uses PodStack knowledge base for intelligent selection)
- from services.claude_suggest import suggest_with_claude, _find_claude
+ from services.claude_suggest import suggest_with_claude, _find_ai_cli
- claude_path = _find_claude()
- if claude_path:
- print(f" [3/5] Selecting moments with Claude (PodStack)...")
+ ai_path, ai_engine = _find_ai_cli()
+ if ai_path:
+ ai_label = "Claude" if ai_engine == "claude" else "Codex"
+ print(f" [3/4] Selecting moments with {ai_label} (PodStack)...")
clips = suggest_with_claude(
segments=segments,
top_n=top_n,
progress_callback=lambda pct, msg: print(f" {msg}") if msg else None,
)
if clips:
- print(f" ✓ Claude selected {len(clips)} clips")
+ print(f" ✓ {ai_label} selected {len(clips)} clips")
else:
- print(f" ⚠ Claude unavailable, falling back to heuristics")
+ print(f" ⚠ {ai_label} unavailable, falling back to heuristics")
else:
- print(f" [3/5] Scoring clips (heuristic mode)...")
- print(f" ℹ Install Claude Code for smarter selection:")
- print(f" https://docs.anthropic.com/en/docs/claude-code")
+ print(f" [3/4] Scoring clips (heuristic mode)...")
+ print(f" ℹ Install Claude Code or Codex for smarter selection")
# Fallback to heuristic algorithm
if not clips:
@@ -402,6 +402,10 @@ def cmd_process(args):
except Exception:
pass
+ # Check if AI CLI is available for per-clip content generation
+ from services.claude_suggest import _find_ai_cli
+ _ai_cli_path, _ai_engine = _find_ai_cli()
+
# Pre-load thumbnail tools if enabled
_thumb_gen = None
_thumb_to_video = None
@@ -423,7 +427,8 @@ def cmd_process(args):
except Exception:
_thumb_enabled = False
- print(f"\n [4/5] Exporting {len(clips)} clips to {output_dir}/")
+ _ai_label = f" (+ titles via {'Claude' if _ai_engine == 'claude' else 'Codex'})" if _ai_cli_path else ""
+ print(f"\n [4/4] Exporting {len(clips)} clips{_ai_label} to {output_dir}/")
results = []
t0 = time.time()
@@ -473,6 +478,47 @@ def cmd_process(args):
except Exception as e:
print(f" ⚠ thumbnail: {e}")
+ # Generate titles, descriptions, tags for this clip immediately
+ if _ai_cli_path and result.get("output_path"):
+ try:
+ from services.content_generator import generate_clip_content
+ content_result = generate_clip_content(
+ clip=clip,
+ transcript_segments=segments,
+ )
+ if content_result and content_result.get("raw_text"):
+ # Save per-clip content to file
+ _content_path = result["output_path"].replace(".mp4", "_content.md")
+ with open(_content_path, "w") as _cf:
+ _cf.write(f"# {clip.get('title', 'Clip')}\n\n{content_result['raw_text']}")
+
+ # Pretty-print in terminal
+ _accent = "\033[38;2;212;135;74m"
+ _bold = "\033[1m"
+ _dim = "\033[2m"
+ _yellow = "\033[33m"
+ _reset = "\033[0m"
+
+ print(f"\n {'─' * 45}")
+ print(f" {_bold}📋 Clip {i+1}: {clip['title'][:45]}{_reset}")
+ print(f" {'─' * 45}")
+ for line in content_result["raw_text"].split("\n"):
+ stripped = line.strip()
+ if not stripped:
+ continue
+ if stripped.startswith("TITLES") or stripped.startswith("DESCRIPTION") or stripped.startswith("TAGS") or stripped.startswith("HASHTAGS") or stripped.startswith("TOP PICK"):
+ print(f" {_bold}{stripped}{_reset}")
+ elif stripped[0:1].isdigit() and ". " in stripped[:4]:
+ print(f" {_accent}{stripped}{_reset}")
+ elif stripped.startswith("#"):
+ print(f" {_yellow}{stripped}{_reset}")
+ else:
+ print(f" {_dim}{stripped}{_reset}")
+ print(f" {_dim}Saved: {os.path.basename(_content_path)}{_reset}")
+ print()
+ except Exception as e:
+ print(f" ⚠ content: {e}")
+
elapsed = time.time() - t0
success = sum(1 for r in results if "output_path" in r)
print(f"\n {success}/{len(clips)} clips exported in {elapsed:.1f}s")
@@ -481,175 +527,13 @@ def cmd_process(args):
print(f"\n Output: {output_dir}/")
- # ── Step 5: Content generation via Claude (with PodStack knowledge base) ──
- from services.claude_suggest import _find_claude
- claude_path = _find_claude()
-
accent = "\033[38;2;212;135;74m"
gray = "\033[38;5;245m"
- green = "\033[38;2;74;222;128m"
bold = "\033[1m"
reset = "\033[0m"
- if claude_path:
- print(f"\n {bold}[5/5] Generating content package...{reset}")
-
- # Load PodStack knowledge base files inline
- kb_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", ".podcli", "knowledge")
- kb_sections = {}
- for fname in [
- "02-voice-and-tone.md",
- "05-title-formulas.md",
- "06-descriptions-template.md",
- "07-thumbnail-guide.md",
- "12-quick-reference.md",
- ]:
- fpath = os.path.join(kb_dir, fname)
- if os.path.exists(fpath):
- try:
- with open(fpath) as kf:
- kb_sections[fname] = kf.read()
- except Exception:
- pass
-
- kb_context = ""
- for fname, content in kb_sections.items():
- kb_context += f"\n\n--- {fname} ---\n{content[:3000]}"
-
- # Build transcript text for Claude
- transcript_lines = []
- for seg in segments:
- sp = seg.get("speaker", "")
- sp_label = f"[{sp}] " if sp else ""
- start = seg.get("start", 0)
- mins = int(start) // 60
- secs = int(start) % 60
- text = seg.get("text", "").strip()
- if text:
- transcript_lines.append(f"({mins}:{secs:02d}) {sp_label}{text}")
-
- # Build clip summary for context
- clip_summary = "\n".join(
- f"- Short {i+1}: \"{c['title']}\" ({c['start_second']}s-{c['end_second']}s, {c['end_second']-c['start_second']:.0f}s) [{c.get('content_type', '')}]"
- for i, c in enumerate(clips)
- )
-
- prompt = f"""You are the content production team for a podcast. Generate a complete publish-ready content package for these clips.
-
-KNOWLEDGE BASE (follow these specs exactly):
-{kb_context}
-
----
-
-VIDEO: {os.path.basename(video_path)}
-CLIPS RENDERED: {success}
-OUTPUT DIR: {output_dir}/
-
-SELECTED CLIPS:
-{clip_summary}
-
-TRANSCRIPT (for context):
-{chr(10).join(transcript_lines[:500])}
-
----
-
-GENERATE FOR EACH CLIP:
-
-## 1. TITLES (8 per clip)
-Follow the title spec exactly:
-- 40-60 chars target, 70 max
-- 5-11 words, keyword in first 3 words
-- 4 shapes: topic+tension, result+cause, X vs Y, number+noun
-- Generate: 2 guest/decision, 2 insight/technical, 1-2 market/hot take, 1-2 safe/descriptive
-- Verify each: Coffee Test, banned words check, hook alignment
-- Flag top 2 picks with reasoning
-
-## 2. DESCRIPTIONS (per clip)
-- Short description: hook line + guest attribution + hashtags (under 150 chars)
-- Use the description template from the knowledge base
-
-## 3. THUMBNAIL TEXT (per clip)
-- Line 1 (top): 2-3 words, setup/topic
-- Line 2 (bottom): 2-3 words, payoff/hook (appears on teal highlight)
-- Format as: "LINE 1 / LINE 2"
-
-## 4. POSTING SCHEDULE
-- Recommend optimal posting times and order
-
-## 5. PUBLISH CHECKLIST
-- Pre-publish, post-publish, and day 3-4 optimization steps
-
-Output as clean markdown. No code fences around the whole thing."""
-
- import tempfile, subprocess as sp
- project_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
-
- with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False, dir=project_dir) as f:
- f.write(prompt)
- prompt_file = f.name
-
- try:
- print(f" {gray}Claude is generating titles, descriptions, thumbnails...{reset}")
- claude_result = sp.run(
- f'cat "{prompt_file}" | "{claude_path}" --print -p -',
- capture_output=True, text=True, cwd=project_dir,
- timeout=300, shell=True,
- )
- if claude_result.returncode == 0 and claude_result.stdout.strip():
- # Save content package
- import re
- guest = "episode"
- for seg in segments[:10]:
- text = seg.get("text", "")
- if text and len(text) > 20:
- guest = re.sub(r'[^a-zA-Z0-9]', '-', text[:30]).strip('-').lower()
- break
-
- episodes_dir = os.path.join(project_dir, "episodes")
- os.makedirs(episodes_dir, exist_ok=True)
- pkg_path = os.path.join(episodes_dir, f"content-package-{guest[:20]}.md")
- with open(pkg_path, "w") as f:
- f.write(claude_result.stdout)
-
- print(f" {green}✓{reset} Saved to {accent}episodes/{os.path.basename(pkg_path)}{reset}")
- print()
-
- # Print content package in terminal
- dim = "\033[2m"
- cyan = "\033[36m"
- yellow = "\033[33m"
- pkg_text = claude_result.stdout.strip()
- print(f" {'━' * 50}")
- print(f" {bold}📋 CONTENT PACKAGE{reset}")
- print(f" {'━' * 50}")
- for line in pkg_text.split("\n"):
- stripped = line.strip()
- if stripped.startswith("# "):
- print(f"\n {bold}{cyan}{stripped}{reset}")
- elif stripped.startswith("## "):
- print(f"\n {bold}{stripped[3:]}{reset}")
- elif stripped.startswith("### "):
- print(f" {yellow}{stripped[4:]}{reset}")
- elif stripped.startswith("- ⭐") or stripped.startswith("- **⭐") or "TOP PICK" in stripped.upper():
- print(f" {green}{stripped}{reset}")
- elif stripped.startswith("- "):
- print(f" {stripped}")
- elif stripped:
- print(f" {dim}{stripped}{reset}")
- print(f"\n {'━' * 50}")
- else:
- print(f" {gray}⚠ Claude returned no content. Run {accent}/produce-shorts{reset} {gray}in Claude Code manually.{reset}")
- except Exception as e:
- print(f" {gray}⚠ Content generation skipped: {e}{reset}")
- print(f" {gray}Run {accent}/produce-shorts{reset} {gray}in Claude Code for titles + descriptions.{reset}")
- finally:
- try:
- os.unlink(prompt_file)
- except Exception:
- pass
- else:
- print(f"\n {gray}Clips rendered. For titles, descriptions & thumbnails:{reset}")
- print(f" {gray}Open Claude Code and run {accent}/produce-shorts{reset}")
+ if not _ai_cli_path:
+ print(f"\n {gray}For titles, descriptions & tags: install Claude Code or Codex CLI{reset}")
# ── What next? ──
print(f" {'─' * 45}")
@@ -1160,13 +1044,36 @@ def cmd_cache(args):
def cmd_info(args):
"""Show system info."""
from services.encoder import get_encoder_info
+ from services.claude_suggest import _find_ai_cli
+
+ green = "\033[38;2;74;222;128m"
+ yellow = "\033[38;2;250;204;21m"
+ gray = "\033[38;5;245m"
+ reset = "\033[0m"
info = get_encoder_info()
+ ai_path, ai_engine = _find_ai_cli()
+
+ # Check HF_TOKEN
+ hf_token = os.environ.get("HF_TOKEN", "")
+ if not hf_token:
+ env_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", ".env")
+ if os.path.exists(env_path):
+ try:
+ with open(env_path) as f:
+ for line in f:
+ if line.strip().startswith("HF_TOKEN=") and line.strip().split("=", 1)[1].strip():
+ hf_token = line.strip().split("=", 1)[1].strip()
+ break
+ except Exception:
+ pass
+
print(f"\n podcli system info\n")
- print(f" Platform: {info['system']}")
- print(f" Encoder: {info['best']}")
- print(f" Available: {', '.join(info['available'])}")
- print(f" Flags: {' '.join(info['best_flags'])}")
+ print(f" Platform: {info['system']}")
+ print(f" Encoder: {info['best']}")
+ print(f" Available: {', '.join(info['available'])}")
+ print(f" AI CLI: {green}{('Claude' if ai_engine == 'claude' else 'Codex') + ' (' + ai_path + ')' if ai_path else f'{yellow}not found — install Claude Code or Codex'}{reset}")
+ print(f" Speakers: {green + '✓ configured' if hf_token else yellow + '✗ set HF_TOKEN in .env'}{reset}")
print()
@@ -1227,9 +1134,9 @@ def print_banner():
speakers_ok = bool(hf_token)
- # Check Claude Code
- from services.claude_suggest import _find_claude
- claude_path = _find_claude()
+ # Check AI CLI (Claude Code or Codex)
+ from services.claude_suggest import _find_ai_cli
+ ai_path, ai_engine = _find_ai_cli()
print(f" {bold}podcli{reset} v{VERSION}")
@@ -1240,10 +1147,11 @@ def print_banner():
cache_count = len([f for f in os.listdir(cache_dir) if f.endswith(".json")])
# Status — one line
- claude_tag = f"{green}✓{reset}" if claude_path else f"{yellow}✗{reset}"
+ ai_label = ("Claude" if ai_engine == "claude" else "Codex") if ai_path else "AI CLI"
+ ai_tag = f"{green}✓ {ai_label}{reset}" if ai_path else f"{yellow}✗{reset}"
speaker_tag = f"{green}✓{reset}" if speakers_ok else f"{yellow}✗{reset}"
cache_tag = f"{green}{cache_count}{reset}" if cache_count else f"{gray}0{reset}"
- print(f" {gray}Encoder {green}{encoder_label}{reset} {gray}· Claude {claude_tag} {gray}· Speakers {speaker_tag} {gray}· Cache {cache_tag}{reset}")
+ print(f" {gray}Encoder {green}{encoder_label}{reset} {gray}· {ai_tag} {gray}· Speakers {speaker_tag} {gray}· Cache {cache_tag}{reset}")
# Assets — one line if any
try:
diff --git a/backend/main.py b/backend/main.py
index be43e42..eec9044 100644
--- a/backend/main.py
+++ b/backend/main.py
@@ -216,6 +216,54 @@ def handle_presets(task_id: str, params: dict):
emit_result(task_id, "error", error=f"Unknown presets action: {action}")
+def handle_suggest_clips(task_id: str, params: dict):
+ """AI-powered clip suggestion using Claude/Codex and PodStack knowledge base."""
+ from services.claude_suggest import suggest_with_claude
+
+ segments = params.get("segments", [])
+ top_n = params.get("top_n", 5)
+
+ if not segments:
+ emit_result(task_id, "error", error="segments is required")
+ return
+
+ clips = suggest_with_claude(
+ segments=segments,
+ top_n=top_n,
+ progress_callback=lambda pct, msg: emit_progress(task_id, "suggesting", pct, msg),
+ )
+
+ if clips is None:
+ emit_result(task_id, "error", error="No AI CLI available (install Claude Code or Codex)")
+ return
+
+ emit_result(task_id, "success", data={"clips": clips})
+
+
+def handle_generate_content(task_id: str, params: dict):
+ """Generate titles, descriptions, tags for a clip using PodStack knowledge base."""
+ from services.content_generator import generate_clip_content
+
+ clip = params.get("clip", {})
+ transcript_segments = params.get("transcript_segments", [])
+
+ if not clip:
+ emit_result(task_id, "error", error="clip is required")
+ return
+
+ result = generate_clip_content(
+ clip=clip,
+ transcript_segments=transcript_segments,
+ progress_callback=lambda pct, msg: emit_progress(task_id, "generating", pct, msg),
+ )
+
+ if result is None:
+ emit_result(task_id, "error", error="No AI CLI available (install Claude Code or Codex)")
+ return
+
+ emit_result(task_id, "success", data=result)
+
+
TASK_HANDLERS = {
"ping": handle_ping,
"transcribe": handle_transcribe,
@@ -225,6 +273,8 @@ TASK_HANDLERS = {
"analyze_energy": handle_analyze_energy,
"detect_encoder": handle_detect_encoder,
"presets": handle_presets,
+ "suggest_clips": handle_suggest_clips,
+ "generate_content": handle_generate_content,
}
diff --git a/backend/services/content_generator.py b/backend/services/content_generator.py
new file mode 100644
index 0000000..cccf120
--- /dev/null
+++ b/backend/services/content_generator.py
@@ -0,0 +1,215 @@
+"""
+Per-clip content generation (titles, descriptions, tags) via AI CLI.
+
+Single source of truth used by CLI, Web UI, and MCP.
+"""
+
+import json
+import os
+import subprocess
+import tempfile
+from typing import Optional, Callable
+
+from services.claude_suggest import _find_ai_cli
+
+
+def _load_kb_context() -> str:
+ """Load PodStack knowledge base files for content generation."""
+ kb_dir = os.path.join(
+ os.path.dirname(os.path.abspath(__file__)), "..", "..", ".podcli", "knowledge"
+ )
+ kb_context = ""
+ for fname, max_chars in [
+ ("05-title-formulas.md", 3000),
+ ("06-descriptions-template.md", 2000),
+ ("02-voice-and-tone.md", 2000),
+ ("01-brand-identity.md", 1000),
+ ("12-quick-reference.md", 1500),
+ ]:
+ fpath = os.path.join(kb_dir, fname)
+ if os.path.exists(fpath):
+ try:
+ with open(fpath) as kf:
+ content = kf.read().strip()
+ # Skip uncustomized templates
+ if content.count("[Your Show Name]") > 2 and len(content) < 500:
+ continue
+ kb_context += f"\n--- {fname} ---\n{content[:max_chars]}\n"
+ except Exception:
+ pass
+ return kb_context
+
+
+def generate_clip_content(
+ clip: dict,
+ transcript_segments: list[dict],
+ progress_callback: Optional[Callable[[int, str], None]] = None,
+) -> Optional[dict]:
+ """
+ Generate titles, description, tags, and hashtags for a single clip.
+
+ Args:
+ clip: dict with title, start_second, end_second, content_type
+ transcript_segments: full transcript segments list
+ progress_callback: optional (percent, message) callback
+
+ Returns:
+ dict with raw_text, titles, description, tags, hashtags, or None if AI unavailable
+ """
+ cli_path, engine = _find_ai_cli()
+ if not cli_path:
+ return None
+
+ label = "Claude" if engine == "claude" else "Codex"
+ if progress_callback:
+ progress_callback(0, f"Generating content via {label}...")
+
+ kb_context = _load_kb_context()
+
+ # Extract transcript text for this clip's time range
+ clip_start = clip.get("start_second", 0)
+ clip_end = clip.get("end_second", 0)
+ clip_transcript = []
+ for seg in transcript_segments:
+ seg_start = seg.get("start", 0)
+ if clip_start - 5 <= seg_start <= clip_end + 5:
+ sp = seg.get("speaker", "")
+ sp_label = f"[{sp}] " if sp else ""
+ clip_transcript.append(f"{sp_label}{seg.get('text', '').strip()}")
+
+ prompt = f"""Generate a YouTube Shorts content package for this clip. Return ONLY the content below, no preamble.
+
+KNOWLEDGE BASE:
+{kb_context}
+
+CLIP: "{clip.get('title', '')}"
+Duration: {clip_end - clip_start:.0f}s
+Content type: {clip.get('content_type', 'unknown')}
+
+TRANSCRIPT EXCERPT:
+{chr(10).join(clip_transcript[:30])}
+
+Generate exactly this (no other text):
+
+TITLES (8 options, 40-60 chars, keyword-first, follow title spec):
+1. [title]
+2. [title]
+3. [title]
+4. [title]
+5. [title]
+6. [title]
+7. [title]
+8. [title]
+TOP PICK: [number] — [why]
+
+DESCRIPTION:
+[hook line under 100 chars]
+[guest attribution line]
+
+TAGS:
+[comma-separated, 8-12 tags for YouTube]
+
+HASHTAGS:
+[5 hashtags for description]"""
+
+ project_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..")
+
+ with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False, dir=project_dir) as f:
+ f.write(prompt)
+ prompt_file = f.name
+
+ try:
+ if progress_callback:
+ progress_callback(30, f"Asking {label} for titles & descriptions...")
+
+ if engine == "codex":
+ out_file = prompt_file + ".out"
+ cr = subprocess.run(
+ [cli_path, "exec", "--full-auto", "-o", out_file, prompt[:4000]],
+ capture_output=True, text=True, cwd=project_dir, timeout=120,
+ )
+ if os.path.exists(out_file):
+ with open(out_file) as of:
+ cr = subprocess.CompletedProcess(
+ args=cr.args, returncode=cr.returncode,
+ stdout=of.read(), stderr=cr.stderr,
+ )
+ try:
+ os.unlink(out_file)
+ except Exception:
+ pass
+ else:
+ cr = subprocess.run(
+ f'cat "{prompt_file}" | "{cli_path}" --print -p -',
+ capture_output=True, text=True, cwd=project_dir,
+ timeout=120, shell=True,
+ )
+
+ if cr.returncode != 0 or not cr.stdout.strip():
+ return None
+
+ raw_text = cr.stdout.strip()
+
+ if progress_callback:
+ progress_callback(90, "Parsing content...")
+
+ # Parse structured sections from the response
+ result = {
+ "raw_text": raw_text,
+ "titles": [],
+ "top_pick": "",
+ "description": "",
+ "tags": "",
+ "hashtags": "",
+ "engine": engine,
+ }
+
+ lines = raw_text.split("\n")
+ section = ""
+ for line in lines:
+ stripped = line.strip()
+ if not stripped:
+ continue
+ upper = stripped.upper()
+ if upper.startswith("TITLES"):
+ section = "titles"
+ continue
+ elif upper.startswith("TOP PICK"):
+ result["top_pick"] = stripped
+ section = ""
+ continue
+ elif upper.startswith("DESCRIPTION"):
+ section = "description"
+ continue
+ elif upper.startswith("TAGS"):
+ section = "tags"
+ continue
+ elif upper.startswith("HASHTAGS"):
+ section = "hashtags"
+ continue
+
+ if section == "titles" and stripped[0:1].isdigit() and ". " in stripped[:4]:
+ result["titles"].append(stripped)
+ elif section == "description":
+ result["description"] += stripped + "\n"
+ elif section == "tags":
+ result["tags"] = stripped
+ elif section == "hashtags":
+ result["hashtags"] = stripped
+
+ result["description"] = result["description"].strip()
+
+ if progress_callback:
+ progress_callback(100, f"Content ready ({len(result['titles'])} titles)")
+
+ return result
+
+ except subprocess.TimeoutExpired:
+ return None
+ except Exception:
+ return None
+ finally:
+ try:
+ os.unlink(prompt_file)
+ except Exception:
+ pass
diff --git a/backend/services/thumbnail_ai.py b/backend/services/thumbnail_ai.py
index 4ec0139..8aa8a91 100644
--- a/backend/services/thumbnail_ai.py
+++ b/backend/services/thumbnail_ai.py
@@ -371,8 +371,8 @@ def ask_claude_for_layout(
Returns dict with line1, line2, box_y, photo_object_position, etc.
"""
- from services.claude_suggest import _find_claude
- claude_path = _find_claude()
+ from services.claude_suggest import _find_ai_cli
+ claude_path, _engine = _find_ai_cli()
if not claude_path:
return None
diff --git a/src/models/index.ts b/src/models/index.ts
index c1f3775..bc5e8f2 100644
--- a/src/models/index.ts
+++ b/src/models/index.ts
@@ -2,7 +2,7 @@
export interface TaskRequest {
task_id: string;
- task_type: "transcribe" | "parse_transcript" | "create_clip" | "batch_clips" | "analyze_energy" | "detect_encoder" | "presets" | "ping";
+ task_type: "transcribe" | "parse_transcript" | "create_clip" | "batch_clips" | "analyze_energy" | "detect_encoder" | "presets" | "ping" | "suggest_clips" | "generate_content";
params: Record<string, unknown>;
}
diff --git a/src/ui/web-server.ts b/src/ui/web-server.ts
index 39bb136..605d9f5 100644
--- a/src/ui/web-server.ts
+++ b/src/ui/web-server.ts
@@ -1203,200 +1203,31 @@ app.post("/api/generate-prompt", (req, res) => {
res.json({ prompt, action, context: ctx });
});
-// --- AI-powered clip suggestion (Claude Code or Codex) ---
+// --- AI-powered clip suggestion (delegates to Python backend) ---
app.post("/api/claude-suggest", async (req, res) => {
const { top_n = 5 } = req.body;
- // Find AI CLI: prefer Claude, fall back to Codex
- const homedir = require("os").homedir();
- const claudeCandidates = [
- join(homedir, ".local", "bin", "claude"),
- "/usr/local/bin/claude",
- "/opt/homebrew/bin/claude",
- ];
- const codexCandidates = [
- "/usr/local/bin/codex",
- "/opt/homebrew/bin/codex",
- ];
- const claudePath = claudeCandidates.find((p) => existsSync(p));
- const codexPath = !claudePath ? codexCandidates.find((p) => existsSync(p)) : undefined;
- const aiPath = claudePath || codexPath;
- const engine: "claude" | "codex" = claudePath ? "claude" : "codex";
-
- if (!aiPath) {
- res.json({
- error: "No AI CLI found (Claude Code or Codex)",
- message: "Install Claude Code (https://docs.anthropic.com/en/docs/claude-code) or Codex (https://openai.com/index/introducing-codex/)",
- fallback: "clipboard",
- clips: [],
- });
- return;
- }
-
// Need transcript in state
if (!uiState.transcript && !uiState.rawTranscriptText) {
res.status(400).json({ error: "No transcript loaded. Transcribe or import one first." });
return;
}
- // Build transcript text from state
- let transcriptText = "";
const segs = (uiState.transcript as any)?.segments;
- if (segs && Array.isArray(segs)) {
- transcriptText = segs
- .map((s: any) => {
- const sp = s.speaker ? `[${s.speaker}] ` : "";
- const mins = Math.floor(s.start / 60);
- const secs = Math.floor(s.start % 60);
- return `(${mins}:${String(secs).padStart(2, "0")}) ${sp}${s.text}`;
- })
- .join("\n");
- } else if (uiState.rawTranscriptText) {
- transcriptText = uiState.rawTranscriptText as string;
- }
-
- const segCount = segs?.length || 0;
- const dur = (uiState.transcript as any)?.duration || 0;
- const durationMin = Math.round(dur / 60);
-
- // Load knowledge base files for context
- const kbDir = join(process.cwd(), ".podcli", "knowledge");
- let kbContext = "";
- const kbFiles: [string, number][] = [
- ["04-shorts-creation-guide.md", 4000],
- ["05-title-formulas.md", 3000],
- ["02-voice-and-tone.md", 3000],
- ["01-brand-identity.md", 1500],
- ["11-inspiration-channels.md", 2000],
- ["12-quick-reference.md", 2000],
- ];
- for (const [fname, maxChars] of kbFiles) {
- const fpath = join(kbDir, fname);
- if (existsSync(fpath)) {
- try {
- const content = readFileSync(fpath, "utf-8").trim();
- if (content.split("[Your Show Name]").length > 3 && content.length < 500) continue;
- kbContext += `\n--- ${fname} ---\n${content.slice(0, maxChars)}\n`;
- } catch {}
- }
+ if (!segs || !Array.isArray(segs) || segs.length === 0) {
+ res.status(400).json({ error: "No transcript segments available." });
+ return;
}
- const prompt = `You are a viral clip editor for TikTok and YouTube Shorts. Find the ${top_n} most scroll-stopping moments in this podcast transcript.
-
-IMPORTANT: Return ONLY valid JSON. No markdown, no explanation, no code fences.
-
-TIMESTAMP FORMAT: All timestamps in the transcript are in SECONDS (e.g., [123.4s]).
-All timestamps you return MUST be in SECONDS as numbers (e.g., 123.4), NOT minutes:seconds.
-
-DURATION RULES (CRITICAL):
-- Target: 25-40 seconds (this is the viral sweet spot)
-- Maximum: 50 seconds (absolute hard limit — anything longer loses viewers)
-- Minimum: 15 seconds (too short = no payoff)
-- SHORTER IS BETTER. A punchy 25s clip outperforms a 50s clip every time.
-- If a thought takes longer than 40s, use segments to cut the filler in the middle
-
-CUTTING RULES (CRITICAL):
-- Cut TIGHT. Every second must earn its place.
-- Start at the exact moment the hook hits — no preamble, no "so", no "well"
-- End the MOMENT the point lands with a complete thought — don't trail off
-- NEVER cut mid-sentence or mid-thought. The viewer must feel closure.
-- If there's filler/tangent in the middle, use multiple segments to skip it
-
-MOMENT SELECTION (think like a TikTok editor):
-- Would YOU stop scrolling for this? If no, skip it.
-- First 3 seconds must HOOK — a bold claim, shocking number, or provocative question
-- Must make complete sense standalone — no "as I mentioned" or "going back to"
-- Must end on a COMPLETE THOUGHT — sentence boundary, natural pause, or mic-drop moment
-- Single focused idea — one concept, fully delivered, no loose threads
-- Prioritize: controversial takes, surprising numbers, founder war stories, "wait what?" moments, emotional peaks
-- Skip: generic advice, obvious statements, context-dependent references
-
-${kbContext ? `KNOWLEDGE BASE:${kbContext}` : ""}
-
-Score each moment on 4 dimensions (1-5 each):
-- standalone: Makes sense without episode context?
-- hook: Grabs attention in first 3 seconds?
-- relevance: Matters to target audience?
-- quotability: Memorable, shareable phrasing?
-
-Classify each as: guest_story | technical_insight | market_landscape | business_strategy | hot_take
-
-Return this exact JSON structure:
-{ "clips": [{ "title": "First strong sentence from the moment", "start_second": 123.4, "end_second": 168.4, "segments": [{"start": 123.4, "end": 140.0}, {"start": 145.2, "end": 168.4}], "duration": 40, "content_type": "guest_story", "scores": {"standalone": 4, "hook": 5, "relevance": 4, "quotability": 3}, "total_score": 16, "quote": "The key quote from this moment", "why": "One sentence on why this works as a short" }] }
-
-SEGMENTS RULES:
-- "segments" is an array of keep-ranges within the clip. Use it to CUT OUT dead weight.
-- If the moment is clean with no filler, use a single segment: [{"start": X, "end": Y}]
-- If there's a ramble/tangent/filler in the middle, split into multiple segments that skip it
-- "duration" = total kept time (sum of all segment lengths), NOT end - start
-
-Rules:
-- Final clip duration (sum of segments) MUST be 15-50 seconds (target 25-40s)
-- Each segment must start and end on COMPLETE SENTENCES
-- Must make sense standalone when stitched together
-- Sort clips by timestamp order
-
-Transcript (${segCount} segments, ~${durationMin} min):
-
-${transcriptText}`;
-
- // Write prompt to temp file
- const tmpFile = join(process.cwd(), `.tmp-claude-prompt-${Date.now()}.txt`);
- writeFileSync(tmpFile, prompt);
-
try {
- const { execSync } = require("child_process");
- let result: string;
-
- if (engine === "codex") {
- const outFile = tmpFile + ".out";
- execSync(
- `"${aiPath}" exec --full-auto -o "${outFile}" "$(cat "${tmpFile}")"`,
- { cwd: process.cwd(), timeout: 300_000, maxBuffer: 10 * 1024 * 1024, encoding: "utf-8" }
- );
- result = existsSync(outFile) ? readFileSync(outFile, "utf-8") : "";
- try { require("fs").unlinkSync(outFile); } catch {}
- } else {
- result = execSync(
- `cat "${tmpFile}" | "${aiPath}" --print -p -`,
- { cwd: process.cwd(), timeout: 300_000, maxBuffer: 10 * 1024 * 1024, encoding: "utf-8" }
- );
- }
-
- // Parse JSON from response
- const jsonStart = result.indexOf("{");
- const jsonEnd = result.lastIndexOf("}") + 1;
- if (jsonStart < 0 || jsonEnd <= jsonStart) {
- res.status(500).json({ error: `${engine === "claude" ? "Claude" : "Codex"} did not return valid JSON` });
- return;
- }
-
- const data = JSON.parse(result.substring(jsonStart, jsonEnd));
- const clips = (data.clips || []).map((c: any) => {
- // Parse and validate segments
- const rawSegs = c.segments || [];
- const segments = rawSegs
- .filter((s: any) => s.end > s.start)
- .map((s: any) => ({ start: s.start, end: s.end }));
- const keptDuration = segments.length > 0
- ? segments.reduce((sum: number, s: any) => sum + (s.end - s.start), 0)
- : (c.end_second || 0) - (c.start_second || 0);
+ const result = await executor.execute(
+ "suggest_clips",
+ { segments: segs, top_n },
+ (event) => broadcastSSE("job-update", { progress: event.percent, message: event.message }),
+ );
- return {
- title: c.title || "Untitled",
- start_second: c.start_second || 0,
- end_second: c.end_second || 0,
- segments: segments.length > 0 ? segments : [{ start: c.start_second || 0, end: c.end_second || 0 }],
- duration: Math.round(keptDuration),
- reasoning: c.why || "",
- preview_text: (c.quote || "").slice(0, 120),
- content_type: c.content_type || "unknown",
- score: c.total_score || 0,
- scores: c.scores || {},
- suggested_caption_style: "hormozi",
- };
- }).filter((c: any) => c.duration >= 15 && c.duration <= 55);
+ const clips = (result.data as any)?.clips || [];
// Auto-push to UI state as suggestions
if (clips.length > 0) {
@@ -1410,18 +1241,41 @@ ${transcriptText}`;
preview_text: c.preview_text,
content_type: c.content_type,
score: c.score,
- suggested_caption_style: c.suggested_caption_style,
+ suggested_caption_style: c.suggested_caption_style || "hormozi",
}));
(uiState as any).phase = "review";
broadcastSSE("state-sync", uiState);
}
- res.json({ clips, source: engine });
+ res.json({ clips, source: "python" });
+ } catch (err: any) {
+ res.status(500).json({ error: `Suggestion failed: ${err.message?.substring(0, 200)}` });
+ }
+});
+
+// --- Per-clip content generation (titles, descriptions, tags) ---
+
+app.post("/api/generate-content", async (req, res) => {
+ const { clip, transcript_segments } = req.body;
+
+ if (!clip) {
+ res.status(400).json({ error: "clip is required" });
+ return;
+ }
+
+ // Use transcript segments from request or fall back to UI state
+ const segs = transcript_segments || (uiState.transcript as any)?.segments || [];
+
+ try {
+ const result = await executor.execute(
+ "generate_content",
+ { clip, transcript_segments: segs },
+ (event) => broadcastSSE("job-update", { progress: event.percent, message: event.message }),
+ );
+
+ res.json(result.data);
} catch (err: any) {
- const label = engine === "claude" ? "Claude" : "Codex";
- res.status(500).json({ error: `${label} failed: ${err.message?.substring(0, 200)}` });
- } finally {
- try { require("fs").unlinkSync(tmpFile); } catch {}
+ res.status(500).json({ error: `Content generation failed: ${err.message?.substring(0, 200)}` });
}
});