Skip to content

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

fix: critical audit fixes — timestamps, seam margin, JSON parsing, encoding

Nika Siradzea065b734db3b
diff --git a/backend/services/claude_suggest.py b/backend/services/claude_suggest.py
index 220f2fc..5f6282c 100644
--- a/backend/services/claude_suggest.py
+++ b/backend/services/claude_suggest.py
@@ -209,14 +209,20 @@ def suggest_with_claude(
# Parse JSON from Claude's response
response = result.stdout.strip()
- # Try to find JSON in the response (Claude might add text around it)
+ # Strip markdown code fences if present
+ if "```" in response:
+ import re
+ fence_match = re.search(r"```(?:json)?\s*\n?(.*?)\n?\s*```", response, re.DOTALL)
+ if fence_match:
+ response = fence_match.group(1).strip()
+
+ # Use raw_decode to extract the first complete JSON object
+ # (safer than rfind which can grab a stray "}" after the JSON)
json_start = response.find("{")
- json_end = response.rfind("}") + 1
- if json_start >= 0 and json_end > json_start:
- json_str = response[json_start:json_end]
- data = json.loads(json_str)
+ if json_start >= 0:
+ decoder = json.JSONDecoder()
+ data, _ = decoder.raw_decode(response, json_start)
else:
- # Try parsing the whole thing
data = json.loads(response)
clips = data.get("clips", [])
@@ -226,6 +232,22 @@ def suggest_with_claude(
# Normalize to the format the CLI expects
normalized = []
+ def _parse_seconds(val) -> float:
+ """Parse a timestamp value — handles both 123.4 and '2:03' formats."""
+ if isinstance(val, (int, float)):
+ return float(val)
+ s = str(val).strip()
+ if ":" in s:
+ parts = s.split(":")
+ try:
+ return float(parts[0]) * 60 + float(parts[1])
+ except (ValueError, IndexError):
+ return 0.0
+ try:
+ return float(s)
+ except ValueError:
+ return 0.0
+
for c in clips:
scores = c.get("scores", {})
total = sum(scores.values()) if scores else c.get("total_score", 0)
@@ -234,13 +256,13 @@ def suggest_with_claude(
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)
+ s = round(_parse_seconds(seg.get("start", 0)), 1)
+ e = round(_parse_seconds(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)
+ start_sec = round(_parse_seconds(c.get("start_second", 0)), 1)
+ end_sec = round(_parse_seconds(c.get("end_second", 0)), 1)
# Fall back to single segment if none provided
if not keep_segments and end_sec > start_sec:
diff --git a/backend/services/clip_generator.py b/backend/services/clip_generator.py
index 2a06b5e..55fef71 100644
--- a/backend/services/clip_generator.py
+++ b/backend/services/clip_generator.py
@@ -267,13 +267,19 @@ def generate_clip(
w for w in transcript_words
if w["end"] > seg["start"] and w["start"] < seg["end"]
]
+ seg_duration = seg["end"] - seg["start"]
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"]
+ # Clamp to segment bounds to avoid negative/overflow timestamps
+ # for words that straddle a segment boundary
+ remapped_start = max(0, cumulative_t + (w["start"] - seg["start"]))
+ remapped_end = min(cumulative_t + seg_duration, cumulative_t + (w["end"] - seg["start"]))
+ if remapped_end > remapped_start:
+ remapped_words.append({
+ **w,
+ "start": round(remapped_start, 3),
+ "end": round(remapped_end, 3),
+ })
+ cumulative_t += seg_duration
crop_words = remapped_words
crop_clip_start = 0
caption_time_offset = 0
diff --git a/backend/services/thumbnail_html.py b/backend/services/thumbnail_html.py
index f75be79..dff1c78 100644
--- a/backend/services/thumbnail_html.py
+++ b/backend/services/thumbnail_html.py
@@ -215,11 +215,16 @@ def _build_html(
box_fill = cfg.get("box_fill_color", f"rgba(26,26,46,0.90)")
box_padding = cfg.get("box_padding", "28px 36px")
- # Base font sizes from config
- l1_base_size = int(cfg.get("line1_font_size", "64px").replace("px", ""))
- l2_base_size = int(cfg.get("line2_font_size", "60px").replace("px", ""))
- l1_base_spacing = int(cfg.get("line1_letter_spacing", "1px").replace("px", ""))
- l2_base_spacing = int(cfg.get("line2_letter_spacing", "1px").replace("px", ""))
+ # Base font sizes from config — extract int from "64px", "1.5rem", or bare "64"
+ def _px(val, default):
+ import re
+ m = re.search(r"(\d+)", str(val))
+ return int(m.group(1)) if m else default
+
+ l1_base_size = _px(cfg.get("line1_font_size", "56px"), 56)
+ l2_base_size = _px(cfg.get("line2_font_size", "52px"), 52)
+ l1_base_spacing = _px(cfg.get("line1_letter_spacing", "1px"), 1)
+ l2_base_spacing = _px(cfg.get("line2_letter_spacing", "1px"), 1)
# Auto-shrink: estimate text width vs box width, scale down if needed.
box_w_px = int(str(box_width).replace("px", "")) if "px" in str(box_width) else 860
diff --git a/backend/services/video_processor.py b/backend/services/video_processor.py
index 27eba6b..8c0b9d4 100644
--- a/backend/services/video_processor.py
+++ b/backend/services/video_processor.py
@@ -136,13 +136,13 @@ def cut_multi_segment(
for p in part_paths:
f.write(f"file '{os.path.abspath(p)}'\n")
- # Concatenate — re-encode to ensure seamless joins
+ # Concatenate with stream copy — parts already have matching codecs
+ # from cut_segment, so no re-encode needed
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",
+ "-c", "copy",
"-movflags", "+faststart",
output_path,
]
@@ -471,20 +471,23 @@ def _build_speaker_aware_crop(
left_pos = positions[positions < mid_x]
right_pos = positions[positions >= mid_x]
+ # Seam margin: keep crop at least 20px away from the center line
+ # to avoid showing the split-screen divider at the frame edge
+ seam_margin = 20
clusters = []
if len(left_pos) >= 3:
cx = int(np.median(left_pos))
clusters.append({
"center_x": cx,
"count": len(left_pos),
- "crop_x": max(0, min(cx - crop_w // 2, mid_x - crop_w)),
+ "crop_x": max(0, min(cx - crop_w // 2, mid_x - crop_w - seam_margin)),
})
if len(right_pos) >= 3:
cx = int(np.median(right_pos))
clusters.append({
"center_x": cx,
"count": len(right_pos),
- "crop_x": max(mid_x, min(cx - crop_w // 2, width - crop_w)),
+ "crop_x": max(mid_x + seam_margin, min(cx - crop_w // 2, width - crop_w)),
})
if len(clusters) < 2:
@@ -535,9 +538,10 @@ def _build_speaker_aware_crop(
speaker_to_cluster[top_2_by_first_word[0]] = clusters[0] # left
speaker_to_cluster[top_2_by_first_word[1]] = clusters[1] # right
- # Map any remaining speakers to the dominant speaker's cluster
+ # Map any remaining speakers to the dominant (most talk time) speaker's cluster
+ dominant = speakers_by_talk[0]
for sp in speakers_by_talk[2:]:
- speaker_to_cluster[sp] = speaker_to_cluster[top_2_by_first_word[0]]
+ speaker_to_cluster[sp] = speaker_to_cluster[dominant]
else:
# Non-split-screen: only one face visible at a time.
# Map by which face is visible when each speaker talks.
@@ -1057,15 +1061,16 @@ def _detect_face_center(
if left_count >= 3 and right_count >= 3:
# Split-screen — pick the side with the most detections,
- # clamp crop to that half so we never show the seam.
+ # clamp crop to that half with margin away from seam.
+ seam_margin = 20
if left_count >= right_count:
side_pos = positions[positions < mid_x]
face_x = int(np.median(side_pos))
- crop_x = max(0, min(face_x - crop_w // 2, mid_x - crop_w))
+ crop_x = max(0, min(face_x - crop_w // 2, mid_x - crop_w - seam_margin))
else:
side_pos = positions[positions >= mid_x]
face_x = int(np.median(side_pos))
- crop_x = max(mid_x, min(face_x - crop_w // 2, width - crop_w))
+ crop_x = max(mid_x + seam_margin, min(face_x - crop_w // 2, width - crop_w))
else:
face_x = int(np.median(face_positions))
crop_x = face_x - crop_w // 2