DEV Community

Catalina Vidal
Catalina Vidal

Posted on

Build a Video-Editing Agent That Ships

Automating video editing with an LLM usually stalls at the demo stage. The model returns plausible timestamps, a long recording blows past context, a render fails halfway, and letting the model write FFmpeg commands feels dangerous. The model is not the problem. State, validation, narrow tools, and verification are. This tutorial builds a rough-cut agent for talking-head or interview footage. If you want a browser-based handoff while you build, Halsion Video agentic editing platform streams a live editing session and lets an agent inspect the project, import media, arrange clips, trim, revise titles, and prepare outputs from the same timeline.

Pipeline diagram showing the five stages: transcribe, plan, validate, render, verify, with feedback from verify and validate back to plan
The model only does the plan step. Everything else is deterministic code.

The rule: the model makes editorial decisions, and code does everything else. Transcribe into numbered segments. Plan by choosing segment IDs. Validate hard rules. Render with FFmpeg through one narrow function. Verify with ffprobe and feed errors back. That retry loop makes it an agent, and the closed ID list prevents a whole class of hallucination.

You need Python 3.10+, FFmpeg on PATH, and faster-whisper. The LLM call is a stub, so any provider works.

Transcribe into addressable segments

Give every speech chunk a stable integer ID. The model later refers to IDs, not times. Use faster-whisper with VAD enabled to trim silence, then save transcript JSON as a checkpoint. Never re-transcribe an hour of audio because a later step crashed.

Keep job state on disk

A tiny job.json is the least glamorous and most important part. Every stage checks existing state before doing work. If the process dies, rerunning picks up where it stopped.

The planner, the only LLM call

The prompt gives a closed segment list, states the editorial goal, and demands strict JSON. For long recordings, chunk by time, shortlist each chunk, then plan over the combined shortlists. The output shape is simple:

{'keep': [3, 4, 9], 'notes': 'one sentence on what you cut and why'}
Enter fullscreen mode Exit fullscreen mode

Validate before rendering

Reject bad plans before expensive rendering. IDs must exist. Duplicates are rejected. Order must be chronological. Selected duration must fall within tolerance. The error message becomes feedback for the next planner attempt.

Turn IDs into ranges, then render

Add a few frames of handle around each segment so words are not clipped. Merge adjacent or nearly adjacent ranges so you do not get jittery micro-cuts. The render function takes a source path, numeric ranges, and an output path. It uses FFmpeg trim and atrim with concat, and the model never touches it. Re-encode for frame-accurate cuts; stream copy only cuts on keyframes and looks sloppy in a talking-head edit. With many ranges, use an FFmpeg filter script.

Verify the output

Do not trust an FFmpeg exit code of zero. Probe with ffprobe and compare duration to intended ranges. These cheap checks catch truncated renders, missing audio streams, and ranges that silently clamped to nothing. Add loudness checks or black-frame detection if needed.

Wire the loop together

The loop plans, validates, renders, verifies, and feeds errors back. It stops after a small number of attempts. It is about 150 lines total, and almost none of it is AI. That is the point.

Review it like an editor

An automatic rough cut is a starting point. Export ranges as an edit decision list or as FCPXML or OTIO instead of only a flattened MP4. OpenTimelineIO writes formats for Resolve, Premiere, and other editors, though test imports in your NLE. For a no-install review pass, Halsion Video browser timeline review lets its agent inspect the project, import media, arrange and trim clips, revise transitions and titles, and prepare preview or final outputs from the same timeline.

A timeline view with the agent's selected clips highlighted in color and the removed sections shown as gaps, next to the transcript with kept lines marked
The ideal handoff: the agent's choices appear as clips on a timeline, so you can restore any cut with a single drag.

Ask whether the agent saved the first hour of scrubbing, not whether it made the perfect edit. A cut you can undo is more forgiving than one baked into a flattened file.

What goes wrong in practice

  • Model picks IDs that do not exist: closed list plus validator.
  • Output length is off target: duration check with feedback.
  • First or last words are clipped: padding in the range builder.
  • Jittery micro-cuts: merge_gap merging.
  • Crash mid-job: job.json checkpoints.
  • Render succeeds but file is wrong: ffprobe verification.
  • Model tries shell creativity: it has no shell, only IDs.

Limits: transcript-only editing is blind to picture, transcription errors become editing errors, and hard cuts under music can pop. Spot-check names, and add short audio crossfades where needed.

Where to take it next

Export to OTIO or FCPXML so cuts open as editable timelines. Add b-roll slot detection. Add captions from transcript timings. Move rendering to a queue for batches with the same checkpoint approach. Log every plan and rejection; rejection messages become a free dataset about editorial taste.

The takeaway

Getting a model to have opinions about video is easy. The hard part is a system that survives long inputs, bad outputs, and crashes. Give the model a small closed vocabulary, put plain-code validators around it, expose one narrow renderer, and verify the result. That is how you ship a video-editing agent that works on real footage.

Top comments (0)