Skip to content

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

feat: multi-segment clip cutting — trim dead weight from shorts

Nika Siradze1160c6f49501
diff --git a/backend/cli.py b/backend/cli.py
index dc26206..3902965 100644
--- a/backend/cli.py
+++ b/backend/cli.py
@@ -371,7 +371,9 @@ def cmd_process(args):
score_val = c.get("score", 0)
score_str = f"({score_val}/20)" if isinstance(score_val, int) and score_val <= 20 else f"({score_val:.0f}pts)"
type_tag = f" [{ctype}]" if ctype and ctype != "unknown" else ""
- print(f" {i+1}. [{m_s}:{s_s:02d} → +{c['duration']}s] {score_str}{type_tag} {c['title'][:50]}")
+ n_segs = len(c.get("segments", [])) if c.get("segments") else 1
+ cuts_tag = f" ({n_segs} cuts)" if n_segs > 1 else ""
+ print(f" {i+1}. [{m_s}:{s_s:02d} → +{c['duration']}s] {score_str}{type_tag}{cuts_tag} {c['title'][:50]}")
if c.get("why"):
print(f" {c['why'][:70]}")
@@ -426,6 +428,7 @@ def cmd_process(args):
output_dir=output_dir,
logo_path=config.get("logo_path") or None,
outro_path=config.get("outro_path") or None,
+ keep_segments=clip.get("segments"),
)
results.append(result)
print(f" ✓ {result['file_size_mb']}MB")
diff --git a/backend/services/claude_suggest.py b/backend/services/claude_suggest.py
index 06b739c..220f2fc 100644
--- a/backend/services/claude_suggest.py
+++ b/backend/services/claude_suggest.py
@@ -103,7 +103,11 @@ Return this exact JSON structure:
"title": "First strong sentence from the moment",
"start_second": 123.4,
"end_second": 168.4,
- "duration": 45,
+ "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,
@@ -113,11 +117,21 @@ Return this exact JSON structure:
]
}}
+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
+- Each segment must start and end on sentence boundaries
+- The rendered video will stitch these segments together seamlessly
+- "duration" = total kept time (sum of all segment lengths), NOT end - start
+- "start_second" / "end_second" = outer bounds (first segment start, last segment end)
+- Example: speaker makes great point (10s), rambles (8s), delivers punchline (12s) → 2 segments, 22s total
+
Rules:
-- Clips MUST be 20-60 seconds (target 30-45s)
-- Must start and end on sentence boundaries
-- Must make sense standalone
-- Sort by timestamp order
+- Final clip duration (sum of segments) MUST be 20-60 seconds (target 30-45s)
+- Each segment must start and end on sentence boundaries
+- Must make sense standalone when stitched together
+- Sort clips by timestamp order
Transcript ({segment_count} segments, ~{duration_min:.0f} min):
@@ -215,11 +229,32 @@ def suggest_with_claude(
for c in clips:
scores = c.get("scores", {})
total = sum(scores.values()) if scores else c.get("total_score", 0)
+
+ # Parse segments (multi-cut ranges) or fall back to single range
+ raw_segments = c.get("segments", [])
+ keep_segments = []
+ for seg in raw_segments:
+ s = round(float(seg.get("start", 0)), 1)
+ e = round(float(seg.get("end", 0)), 1)
+ if e > s:
+ keep_segments.append({"start": s, "end": e})
+
+ start_sec = round(float(c.get("start_second", 0)), 1)
+ end_sec = round(float(c.get("end_second", 0)), 1)
+
+ # Fall back to single segment if none provided
+ if not keep_segments and end_sec > start_sec:
+ keep_segments = [{"start": start_sec, "end": end_sec}]
+
+ # Compute actual kept duration
+ kept_duration = sum(seg["end"] - seg["start"] for seg in keep_segments)
+
normalized.append({
"title": c.get("title", "Untitled")[:55],
- "start_second": round(float(c.get("start_second", 0)), 1),
- "end_second": round(float(c.get("end_second", 0)), 1),
- "duration": round(float(c.get("duration", 0))),
+ "start_second": keep_segments[0]["start"] if keep_segments else start_sec,
+ "end_second": keep_segments[-1]["end"] if keep_segments else end_sec,
+ "segments": keep_segments,
+ "duration": round(kept_duration),
"score": total,
"content_type": c.get("content_type", "unknown"),
"quote": c.get("quote", ""),
diff --git a/backend/services/clip_generator.py b/backend/services/clip_generator.py
index 6315637..e46f98e 100644
--- a/backend/services/clip_generator.py
+++ b/backend/services/clip_generator.py
@@ -16,6 +16,7 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from services.caption_renderer import render_captions
from services.video_processor import (
cut_segment,
+ cut_multi_segment,
crop_to_vertical,
burn_captions,
normalize_audio,
@@ -103,6 +104,7 @@ def generate_clip(
logo_path: Optional[str] = None,
outro_path: Optional[str] = None,
clean_fillers: bool = True,
+ keep_segments: list[dict] = None,
progress_callback: Optional[Callable[[int, str], None]] = None,
) -> dict:
"""
@@ -134,12 +136,23 @@ def generate_clip(
if end_second <= start_second:
raise ValueError("end_second must be greater than start_second")
- # Snap clip boundaries to sentence endings if transcript is available.
- # Extends up to 3s forward to find a sentence boundary (. ! ?).
- if transcript_words:
- end_second = _snap_to_sentence_end(transcript_words, start_second, end_second)
+ # Multi-segment cutting: if keep_segments provided, use those ranges.
+ # Otherwise fall back to single start→end range with sentence snapping.
+ if keep_segments and len(keep_segments) > 0:
+ # Validate segments
+ keep_segments = [s for s in keep_segments if s["end"] > s["start"]]
+ keep_segments.sort(key=lambda s: s["start"])
+ # Override start/end to match segments
+ start_second = keep_segments[0]["start"]
+ end_second = keep_segments[-1]["end"]
+ duration = sum(s["end"] - s["start"] for s in keep_segments)
+ else:
+ # Snap clip boundaries to sentence endings if transcript is available.
+ if transcript_words:
+ end_second = _snap_to_sentence_end(transcript_words, start_second, end_second)
+ keep_segments = None
+ duration = end_second - start_second
- duration = end_second - start_second
if duration > 180:
raise ValueError(f"Clip too long ({duration:.0f}s). Max 180 seconds for shorts.")
@@ -152,12 +165,42 @@ def generate_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 video
+ # Step 1: Cut the segment(s) from the source video
if progress_callback:
- progress_callback(10, f"Trimming to selected time range (1/{total_steps})")
+ n_segs = len(keep_segments) if keep_segments else 1
+ msg = f"Cutting {n_segs} segment{'s' if n_segs > 1 else ''} (1/{total_steps})"
+ progress_callback(10, msg)
segment_path = os.path.join(work_dir, "segment.mp4")
- cut_segment(video_path, segment_path, start_second, end_second)
+ if keep_segments and len(keep_segments) > 1:
+ cut_multi_segment(video_path, segment_path, keep_segments)
+ else:
+ cut_segment(video_path, segment_path, start_second, end_second)
+
+ # Remap transcript words for multi-segment clips.
+ # Needed before crop (speaker detection) and captions.
+ if keep_segments and len(keep_segments) > 1 and transcript_words:
+ remapped_words = []
+ cumulative_t = 0.0
+ for seg in keep_segments:
+ seg_words = [
+ w for w in transcript_words
+ if w["end"] > seg["start"] and w["start"] < seg["end"]
+ ]
+ for w in seg_words:
+ remapped_words.append({
+ **w,
+ "start": cumulative_t + (w["start"] - seg["start"]),
+ "end": cumulative_t + (w["end"] - seg["start"]),
+ })
+ cumulative_t += seg["end"] - seg["start"]
+ crop_words = remapped_words
+ crop_clip_start = 0
+ caption_time_offset = 0
+ else:
+ crop_words = transcript_words
+ crop_clip_start = start_second
+ caption_time_offset = start_second
# Step 2: Crop to vertical 9:16
if progress_callback:
@@ -167,8 +210,8 @@ def generate_clip(
crop_to_vertical(
segment_path, cropped_path,
strategy=crop_strategy,
- transcript_words=transcript_words,
- clip_start=start_second,
+ transcript_words=crop_words,
+ clip_start=crop_clip_start,
face_map=face_map,
)
@@ -177,15 +220,15 @@ def generate_clip(
if progress_callback:
progress_callback(50, f"Adding {caption_style} captions (3/{total_steps})")
- # Filter words that overlap with our clip's time range
- # Use overlap check instead of strict containment to avoid
- # dropping words at boundaries
- clip_words = [
- w for w in transcript_words
- if w["end"] > start_second and w["start"] < end_second
- ]
+ if keep_segments and len(keep_segments) > 1:
+ clip_words = remapped_words
+ else:
+ clip_words = [
+ w for w in transcript_words
+ if w["end"] > start_second and w["start"] < end_second
+ ]
- # Clean filler words and compress large gaps (opt-out via clean_fillers=false)
+ # Clean filler words
if clean_fillers:
clip_words = _clean_transcript_words(clip_words)
@@ -196,7 +239,7 @@ def generate_clip(
words=clip_words,
caption_style=caption_style,
output_path=ass_path,
- time_offset=start_second,
+ time_offset=caption_time_offset,
)
# Burn captions into video
diff --git a/backend/services/video_processor.py b/backend/services/video_processor.py
index 15695cf..e9c2863 100644
--- a/backend/services/video_processor.py
+++ b/backend/services/video_processor.py
@@ -85,17 +85,9 @@ def cut_segment(
"""
Extract a time segment from a video file.
Uses -ss before -i with re-encoding for frame-accurate timestamps.
- Stream-copy (-c copy) snaps to keyframes which can be seconds off,
- causing caption-audio desync. Re-encoding with ultrafast is still
- fast and guarantees the output starts at exactly the requested time.
"""
duration = end_second - start_second
- # Use lossless copy for this intermediate step — the crop/caption
- # pipeline will re-encode with the proper encoder anyway.
- # -ss before -i seeks to nearest keyframe, -noaccurate_seek is fast.
- # We still re-encode video to get frame-accurate start, but use
- # high quality CRF to minimize generation loss.
cmd = [
"ffmpeg", "-y",
"-ss", str(start_second),
@@ -112,6 +104,64 @@ def cut_segment(
return output_path
+def cut_multi_segment(
+ input_path: str,
+ output_path: str,
+ segments: list[dict],
+) -> str:
+ """
+ Cut multiple time ranges from a video and concatenate them seamlessly.
+
+ segments: [{"start": 10.5, "end": 25.0}, {"start": 30.2, "end": 45.0}]
+
+ Each segment is cut individually with frame-accurate encoding, then
+ concatenated with matching codec settings for gapless playback.
+ """
+ if len(segments) == 1:
+ return cut_segment(input_path, output_path, segments[0]["start"], segments[0]["end"])
+
+ work_dir = os.path.dirname(output_path) or "."
+ part_paths = []
+
+ try:
+ # Cut each segment
+ for i, seg in enumerate(segments):
+ part_path = os.path.join(work_dir, f"_part_{i}.mp4")
+ cut_segment(input_path, part_path, seg["start"], seg["end"])
+ part_paths.append(part_path)
+
+ # Build concat file
+ concat_file = os.path.join(work_dir, "_concat_parts.txt")
+ with open(concat_file, "w") as f:
+ for p in part_paths:
+ f.write(f"file '{os.path.abspath(p)}'\n")
+
+ # Concatenate — re-encode to ensure seamless joins
+ cmd = [
+ "ffmpeg", "-y",
+ "-f", "concat", "-safe", "0",
+ "-i", concat_file,
+ "-c:v", "libx264", "-crf", "18", "-preset", "fast", "-profile:v", "high",
+ "-c:a", "aac", "-b:a", "192k",
+ "-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:
+ # Clean up temp parts
+ for p in part_paths:
+ if os.path.exists(p):
+ os.remove(p)
+ concat_file = os.path.join(work_dir, "_concat_parts.txt")
+ if os.path.exists(concat_file):
+ os.remove(concat_file)
+
+
def crop_to_vertical(
input_path: str,
output_path: str,