Skip to content

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

Add format, duration, downloads and auto detection to highlight reels

Nika Siradze96c1138b4c5b
diff --git a/backend/cli.py b/backend/cli.py
index d6a927e..10eab60 100644
--- a/backend/cli.py
+++ b/backend/cli.py
@@ -398,7 +398,9 @@ def cmd_studio(args):
def cmd_reel(args):
"""Create and iterate on a highlights reel — detect once, then edit moments fast."""
- from services.reel import ReelSession, seed_session, edit_moment, build_reel
+ from services.reel import (
+ ReelSession, seed_session, edit_moment, build_reel, list_sessions, delete_session,
+ )
from services.transcript_packer import compute_cache_hash, load_cached_transcript_for_video
def _mmss(s):
@@ -420,7 +422,9 @@ def cmd_reel(args):
words = cached.get("words") if cached else None
print(" Detecting moments (one-time)...")
session = seed_session(
- sid, video, out_dir, profile=args.profile or "party", top_n=args.top or 10,
+ sid, video, out_dir, profile=args.profile or "auto",
+ format=args.format or "horizontal", top_n=args.top or 10,
+ min_dur=args.min_dur, max_dur=args.max_dur,
words=words, progress_callback=lambda p, m: print(f" {m}") if m else None,
)
_show(session)
@@ -429,6 +433,13 @@ def cmd_reel(args):
print(f" ✓ {reel}")
print(f" session: {sid}")
print(f" edit with: podcli reel edit {sid} <N> <longer|shorter|earlier|later|shift|drop|toggle> [secs]")
+ elif action == "list":
+ for s in list_sessions():
+ print(f" {s['session_id']} {s['profile']}/{s['format']} "
+ f"{s['enabled_count']}/{s['moment_count']} moments {os.path.basename(s['source'])}")
+ elif action == "delete":
+ ok = delete_session(args.session)
+ print(f" {'✓ deleted' if ok else '✗ no such session'} {args.session}")
elif action == "show":
_show(ReelSession.load(args.session))
elif action == "edit":
@@ -440,7 +451,8 @@ def cmd_reel(args):
reel = build_reel(ReelSession.load(args.session))
print(f" ✓ {reel}")
else:
- print(" Usage: podcli reel new <video> | show <session> | edit <session> N <op> [secs] | build <session>")
+ print(" Usage: podcli reel new <video> | list | show <session> | "
+ "edit <session> N <op> [secs] | build <session> | delete <session>")
def cmd_process(args):
@@ -3468,9 +3480,18 @@ def main():
reel_sub = reel_p.add_subparsers(dest="reel_action")
rn = reel_sub.add_parser("new", help="Detect moments and build a reel")
rn.add_argument("video", help="Path to the source video")
- rn.add_argument("--profile", choices=["party", "action"], default="party")
+ rn.add_argument("--profile", choices=["auto", "party", "action"], default="auto")
+ rn.add_argument("--format", choices=["vertical", "horizontal", "square"], default="horizontal",
+ help="Reel aspect ratio (default horizontal 1920x1080)")
rn.add_argument("-n", "--top", type=int, help="Number of moments (default 10)")
+ rn.add_argument("--min-dur", type=float, default=15.0, dest="min_dur",
+ help="Shortest moment in seconds (default 15)")
+ rn.add_argument("--max-dur", type=float, default=60.0, dest="max_dur",
+ help="Longest moment in seconds (default 60)")
rn.add_argument("-o", "--output", help="Output directory")
+ reel_sub.add_parser("list", help="List saved reel sessions")
+ rdel = reel_sub.add_parser("delete", help="Delete a reel session")
+ rdel.add_argument("session")
rsh = reel_sub.add_parser("show", help="List the moments in a reel session")
rsh.add_argument("session", help="Session id (printed by 'reel new')")
red = reel_sub.add_parser("edit", help="Adjust one moment and rebuild")
diff --git a/backend/main.py b/backend/main.py
index e99832b..ddd2c87 100644
--- a/backend/main.py
+++ b/backend/main.py
@@ -362,19 +362,31 @@ def handle_detect_highlights(task_id: str, params: dict):
def handle_manage_reel(task_id: str, params: dict):
"""Create and iterate on a highlights reel — the MCP surface over the reel service.
- Actions: new (detect + build), show, edit (adjust one moment + rebuild), build.
+ Actions: new (detect + build), list, show, edit (adjust one moment + rebuild),
+ build, delete.
"""
from dataclasses import asdict
- from services.reel import ReelSession, seed_session, edit_moment, build_reel
+ from services.reel import (
+ ReelSession, seed_session, edit_moment, build_reel, list_sessions, delete_session,
+ )
action = params.get("action", "show")
def payload(session, reel=None):
+ moments = []
+ for i, m in enumerate(session.moments, 1):
+ d = asdict(m)
+ d["clip_path"] = os.path.join(session.out_dir, "clips", f"clip_{i:02d}.mp4")
+ d["clip_exists"] = os.path.exists(d["clip_path"])
+ moments.append(d)
+ reel_file = reel or os.path.join(session.out_dir, "highlights_reel.mp4")
return {
"session_id": session.session_id,
+ "source": session.source,
+ "format": session.format,
"out_dir": session.out_dir,
- "reel_path": reel,
- "moments": [asdict(m) for m in session.moments],
+ "reel_path": reel_file if os.path.exists(reel_file) else None,
+ "moments": moments,
}
try:
@@ -386,18 +398,28 @@ def handle_manage_reel(task_id: str, params: dict):
cached = load_cached_transcript_for_video(video)
words = cached.get("words") if cached else None
session = seed_session(
- sid, video, out_dir, profile=params.get("profile", "party"),
- top_n=int(params.get("top_n", 10)), words=words,
+ sid, video, out_dir, profile=params.get("profile", "auto"),
+ format=params.get("format", "horizontal"),
+ top_n=int(params.get("top_n", 10)),
+ min_dur=float(params.get("min_dur", 15.0)),
+ max_dur=float(params.get("max_dur", 60.0)),
+ words=words,
progress_callback=lambda p, m: emit_progress(task_id, "detecting", p, m),
)
reel = build_reel(session, progress_callback=lambda p, m: emit_progress(task_id, "building", p, m))
emit_result(task_id, "success", data=payload(session, reel))
+ elif action == "list":
+ emit_result(task_id, "success", data={"sessions": list_sessions()})
+ elif action == "delete":
+ ok = delete_session(params["session_id"])
+ emit_result(task_id, "success", data={"deleted": ok, "session_id": params["session_id"]})
elif action == "show":
emit_result(task_id, "success", data=payload(ReelSession.load(params["session_id"])))
elif action == "edit":
session = edit_moment(
ReelSession.load(params["session_id"]),
int(params["index"]), params["op"], float(params.get("seconds", 0.0)),
+ start=params.get("start"), end=params.get("end"),
)
reel = build_reel(session, progress_callback=lambda p, m: emit_progress(task_id, "building", p, m))
emit_result(task_id, "success", data=payload(session, reel))
diff --git a/backend/services/profiles.py b/backend/services/profiles.py
index c5450da..822bd35 100644
--- a/backend/services/profiles.py
+++ b/backend/services/profiles.py
@@ -35,6 +35,17 @@ def _weights(**kw) -> dict[str, float]:
PROFILES = {
+ "auto": ContentProfile(
+ name="auto",
+ candidate_source="saliency",
+ channel_weights=_weights(
+ audio_event=0.3, energy=0.25, motion=0.2, prosody=0.15, face_reaction=0.1
+ ),
+ reaction_lookback_sec=7.0,
+ reaction_payoff_sec=2.0,
+ peak_min_gap_sec=8.0,
+ peak_top_percentile=0.15,
+ ),
"podcast": ContentProfile(
name="podcast",
candidate_source="llm",
diff --git a/backend/services/reel.py b/backend/services/reel.py
index a066b9e..9914f3f 100644
--- a/backend/services/reel.py
+++ b/backend/services/reel.py
@@ -15,6 +15,7 @@ from typing import Optional, Callable
from utils.proc import run as proc_run
from config.paths import paths
+from services.formats import get_format
def _sessions_dir() -> str:
@@ -48,6 +49,7 @@ class ReelSession:
source: str
profile: str
out_dir: str
+ format: str = "horizontal"
moments: list[Moment] = field(default_factory=list)
def save(self) -> str:
@@ -77,7 +79,8 @@ def seed_session(
session_id: str,
source: str,
out_dir: str,
- profile: str = "party",
+ profile: str = "auto",
+ format: str = "horizontal",
top_n: int = 10,
min_dur: float = 15.0,
max_dur: float = 60.0,
@@ -100,7 +103,9 @@ def seed_session(
)
for c in clips
]
- session = ReelSession(session_id, source, profile, out_dir, moments)
+ session = ReelSession(
+ session_id, source, profile, out_dir, format=get_format(format).name, moments=moments
+ )
session.save()
return session
@@ -115,15 +120,34 @@ _EDITS = {
}
-def edit_moment(session: ReelSession, index: int, op: str, seconds: float = 0.0) -> ReelSession:
+def edit_moment(
+ session: ReelSession,
+ index: int,
+ op: str,
+ seconds: float = 0.0,
+ start: Optional[float] = None,
+ end: Optional[float] = None,
+) -> ReelSession:
"""Apply an edit to one moment (1-based index) and mark it for re-cut."""
if not (1 <= index <= len(session.moments)):
raise IndexError(f"no moment {index} (have {len(session.moments)})")
m = session.moments[index - 1]
if op == "drop":
session.moments.pop(index - 1)
+ # Clip files are keyed by position, so everything after the hole now maps
+ # to a stale neighbour's cut. Force those to re-cut on the next build.
+ for shifted in session.moments[index - 1:]:
+ shifted.dirty = True
elif op == "toggle":
m.enabled = not m.enabled
+ elif op == "set":
+ if start is not None:
+ m.start = round(max(0.0, float(start)), 1)
+ if end is not None:
+ m.end = round(float(end), 1)
+ if m.end <= m.start:
+ m.end = round(m.start + 1.0, 1)
+ m.dirty = True
elif op in _EDITS:
_EDITS[op](m, seconds)
m.dirty = True
@@ -133,14 +157,23 @@ def edit_moment(session: ReelSession, index: int, op: str, seconds: float = 0.0)
return session
-def _cut(source: str, out_dir: str, idx: int, m: Moment) -> str:
+def _scale_filter(format: str) -> str:
+ spec = get_format(format)
+ w, h = spec.width, spec.height
+ if spec.reframe:
+ # Vertical/square from a wider source: cover the frame, then centre-crop.
+ return f"scale={w}:{h}:force_original_aspect_ratio=increase,crop={w}:{h}"
+ return (f"scale={w}:{h}:force_original_aspect_ratio=decrease,"
+ f"pad={w}:{h}:(ow-iw)/2:(oh-ih)/2")
+
+
+def _cut(source: str, out_dir: str, idx: int, m: Moment, format: str) -> str:
clips_dir = os.path.join(out_dir, "clips")
os.makedirs(clips_dir, exist_ok=True)
out = os.path.join(clips_dir, f"clip_{idx:02d}.mp4")
proc_run([
"ffmpeg", "-y", "-ss", str(m.start), "-i", source, "-t", str(m.duration),
- "-vf", "scale=1920:1080:force_original_aspect_ratio=decrease,"
- "pad=1920:1080:(ow-iw)/2:(oh-ih)/2",
+ "-vf", _scale_filter(format),
"-c:v", "libx264", "-preset", "veryfast", "-crf", "20",
"-c:a", "aac", "-b:a", "192k", out, "-loglevel", "error",
], timeout=120, check=True)
@@ -157,7 +190,7 @@ def build_reel(session: ReelSession, progress_callback: Optional[Callable] = Non
if m.dirty or not os.path.exists(clip):
if progress_callback:
progress_callback(int(n / len(active) * 90), f"cutting moment {i}")
- clip = _cut(session.source, session.out_dir, i, m)
+ clip = _cut(session.source, session.out_dir, i, m, session.format)
m.dirty = False
files.append(clip)
session.save()
@@ -175,3 +208,40 @@ def build_reel(session: ReelSession, progress_callback: Optional[Callable] = Non
if progress_callback:
progress_callback(100, f"built reel with {len(files)} moments")
return reel
+
+
+def list_sessions() -> list[dict]:
+ """Summaries of every persisted reel session, newest first."""
+ out = []
+ d = _sessions_dir()
+ for name in os.listdir(d):
+ if not name.endswith(".json"):
+ continue
+ path = os.path.join(d, name)
+ try:
+ with open(path) as f:
+ data = json.load(f)
+ except (json.JSONDecodeError, OSError):
+ continue
+ moments = data.get("moments", [])
+ reel = os.path.join(data.get("out_dir", ""), "highlights_reel.mp4")
+ out.append({
+ "session_id": data.get("session_id", name[:-5]),
+ "source": data.get("source", ""),
+ "profile": data.get("profile", ""),
+ "format": data.get("format", "horizontal"),
+ "moment_count": len(moments),
+ "enabled_count": sum(1 for m in moments if m.get("enabled", True)),
+ "reel_path": reel if os.path.exists(reel) else None,
+ "mtime": os.path.getmtime(path),
+ })
+ out.sort(key=lambda s: s["mtime"], reverse=True)
+ return out
+
+
+def delete_session(session_id: str) -> bool:
+ path = session_path(session_id)
+ if not os.path.exists(path):
+ return False
+ os.remove(path)
+ return True
diff --git a/src/server.ts b/src/server.ts
index b335162..c78c35c 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -2077,10 +2077,10 @@ export function createServer(): McpServer {
// =============================================
server.tool(
"manage_reel",
- "Create and iterate on a highlights reel. Detection runs once with action 'new'; after that, edit individual moments fast (longer/shorter/earlier/later/shift/drop/toggle) and rebuild without re-detecting. Actions: 'new' (video_path, profile, top_n), 'show' (session_id), 'edit' (session_id, index, op, seconds), 'build' (session_id).",
+ "Create and iterate on a highlights reel. Detection runs once with action 'new'; after that, edit individual moments fast (longer/shorter/earlier/later/shift/drop/toggle) and rebuild without re-detecting. Actions: 'new' (video_path, profile, format, top_n, min_dur, max_dur), 'list', 'show' (session_id), 'edit' (session_id, index, op, seconds), 'build' (session_id), 'delete' (session_id).",
{
action: z
- .enum(["new", "show", "edit", "build"])
+ .enum(["new", "list", "show", "edit", "build", "delete"])
.describe("What to do with the reel"),
video_path: z
.string()
@@ -2089,24 +2089,38 @@ export function createServer(): McpServer {
session_id: z
.string()
.optional()
- .describe("For show/edit/build: the reel session id returned by 'new'"),
+ .describe("For show/edit/build/delete: the reel session id returned by 'new'"),
profile: z
- .enum(["party", "action"])
+ .enum(["auto", "party", "action"])
.optional()
- .describe("For 'new': detection profile (default party)"),
+ .describe("For 'new': detection profile (default auto)"),
+ format: z
+ .enum(["vertical", "horizontal", "square"])
+ .optional()
+ .describe("For 'new': reel aspect ratio (default horizontal)"),
top_n: z.number().optional().describe("For 'new': number of moments"),
+ min_dur: z.number().optional().describe("For 'new': shortest moment in seconds (default 15)"),
+ max_dur: z.number().optional().describe("For 'new': longest moment in seconds (default 60)"),
index: z
.number()
.optional()
.describe("For 'edit': 1-based moment number to adjust"),
op: z
- .enum(["longer", "shorter", "earlier", "later", "shift", "drop", "toggle"])
+ .enum(["longer", "shorter", "earlier", "later", "shift", "set", "drop", "toggle"])
.optional()
.describe("For 'edit': how to change the moment"),
seconds: z
.number()
.optional()
.describe("For 'edit': seconds for longer/shorter/earlier/later/shift"),
+ start: z
+ .number()
+ .optional()
+ .describe("For 'edit' with op 'set': absolute start time in seconds"),
+ end: z
+ .number()
+ .optional()
+ .describe("For 'edit' with op 'set': absolute end time in seconds"),
},
async (params) => {
try {
diff --git a/src/ui/web-server.ts b/src/ui/web-server.ts
index 3196909..99ba804 100644
--- a/src/ui/web-server.ts
+++ b/src/ui/web-server.ts
@@ -1565,12 +1565,40 @@ app.post("/api/analyze-energy", async (req, res) => {
app.post("/api/reel", async (req, res) => {
try {
const result = await executor.execute("manage_reel", req.body || {});
- res.json(result.data || {});
+ const data = (result.data || {}) as any;
+ if (typeof data.source === "string") registerSourcePath(data.source);
+ if (typeof data.reel_path === "string") registerSourcePath(data.reel_path);
+ if (Array.isArray(data.moments)) {
+ for (const m of data.moments) {
+ if (m && typeof m.clip_path === "string" && m.clip_exists) registerSourcePath(m.clip_path);
+ }
+ }
+ res.json(data);
} catch (err: any) {
res.status(500).json({ error: err.message });
}
});
+app.get("/api/reel-download", (req, res) => {
+ const filePath = req.query.path as string;
+ if (!filePath) {
+ res.status(400).json({ error: "path required" });
+ return;
+ }
+ let resolved: string;
+ try {
+ resolved = realpathSync(path.resolve(filePath));
+ } catch {
+ res.status(404).json({ error: "File not found" });
+ return;
+ }
+ if (extname(resolved).toLowerCase() !== ".mp4" || !allowedSourcePaths.has(resolved)) {
+ res.status(403).json({ error: "Access denied" });
+ return;
+ }
+ res.download(resolved);
+});
+
// --- Encoder info ---
app.get("/api/encoder-info", async (_req, res) => {
try {
diff --git a/tests/test_reel.py b/tests/test_reel.py
index 8c9c56d..9ad6663 100644
--- a/tests/test_reel.py
+++ b/tests/test_reel.py
@@ -60,6 +60,14 @@ class EditTests(unittest.TestCase):
self.assertEqual(len(self.s.moments), 1)
self.assertEqual(self.s.moments[0].text, "two")
+ def test_drop_marks_shifted_moments_dirty(self):
+ for m in self.s.moments:
+ m.dirty = False
+ R.edit_moment(self.s, 1, "drop")
+ # The moment that slid into position 1 must re-cut, else it reuses the
+ # dropped moment's clip file.
+ self.assertTrue(self.s.moments[0].dirty)
+
def test_toggle_disables_without_removing(self):
R.edit_moment(self.s, 1, "toggle")
self.assertFalse(self.s.moments[0].enabled)
@@ -89,6 +97,49 @@ class PersistenceTests(unittest.TestCase):
self.assertEqual(loaded.moments[1].why, "reaction")
self.assertIsInstance(loaded.moments[0], R.Moment)
+ def test_format_defaults_to_horizontal(self):
+ s = _session(sid="roundtrip_reel")
+ s.save()
+ self.assertEqual(R.ReelSession.load("roundtrip_reel").format, "horizontal")
+
+
+class FormatTests(unittest.TestCase):
+ def test_horizontal_pads_to_1920x1080(self):
+ vf = R._scale_filter("horizontal")
+ self.assertIn("1920:1080", vf)
+ self.assertIn("pad=", vf)
+
+ def test_vertical_crops_to_fill(self):
+ vf = R._scale_filter("vertical")
+ self.assertIn("1080:1920", vf)
+ self.assertIn("crop=1080:1920", vf)
+
+ def test_square_crops_to_fill(self):
+ vf = R._scale_filter("square")
+ self.assertIn("crop=1080:1080", vf)
+
+
+class SessionRegistryTests(unittest.TestCase):
+ _ids = ("registry_a", "registry_b")
+
+ def tearDown(self):
+ for sid in self._ids:
+ p = R.session_path(sid)
+ if os.path.exists(p):
+ os.remove(p)
+
+ def test_list_and_delete(self):
+ for sid in self._ids:
+ _session(sid=sid).save()
+ listed = {s["session_id"] for s in R.list_sessions()}
+ self.assertTrue(set(self._ids) <= listed)
+ summary = next(s for s in R.list_sessions() if s["session_id"] == "registry_a")
+ self.assertEqual(summary["moment_count"], 2)
+ self.assertEqual(summary["enabled_count"], 2)
+ self.assertTrue(R.delete_session("registry_a"))
+ self.assertFalse(R.delete_session("registry_a"))
+ self.assertNotIn("registry_a", {s["session_id"] for s in R.list_sessions()})
+
if __name__ == "__main__":
unittest.main()