How I clip a minute out of a two-hour live stream without downloading the two hours
I run CutThisX, a small web tool that cuts a part of an online video or live stream from its link and downloads only that part. The whole idea fits in one sentence, but getting it right took more ffmpeg reading than I ex
I run CutThisX, a small web tool that cuts a part of an online video or live stream from its link and downloads only that part. The whole idea fits in one sentence, but getting it right took more ffmpeg reading than I expected. Here are the parts that were not obvious.
The goal: never transfer what the user did not ask for
A typical downloader fetches the full file and lets you trim afterwards. For a one-minute clip from a one-hour 1080p video, that is 1–2 GB transferred to keep 20–40 MB. So the server has to reach into the source and pull only the selected range.
There are two shapes of source:
- Plain progressive files (one MP4, or separate video and audio files) served over HTTP with range requests.
-
HLS playlists (
.m3u8), used by live streams and some long recorded videos: a list of short segments, typically 2–6 seconds each.
Plain files: ffmpeg input seeking, stream copy, and keyframes
For HTTP files, ffmpeg can seek before opening the input and read only what it needs:
ffmpeg -ss 724 -t 60 -i "$VIDEO_URL" -ss 724 -t 60 -i "$AUDIO_URL" \
-map 0:v:0 -map 1:a:0 -c copy -movflags +faststart clip.mp4
-c copy makes this fast (no re-encoding), but a copied video stream can only start on a keyframe. If the nearest keyframe before 12:04 is at 12:01, the video starts at 12:01, and if the audio is cut at exactly 12:04 the two drift apart by three seconds.
The fix is to ask ffprobe where that keyframe is and start both streams there:
ffprobe -v error -read_intervals 724%+#1 -select_streams v:0 \
-skip_frame nokey -show_entries frame=pts_time -of csv=p=0 "$VIDEO_URL"
That keeps picture and sound in sync, at the cost of a clip that may start a second or two early. For most sources (keyframes every 1–4 seconds) nobody notices.
When keyframe snapping is not good enough
This week a test showed a short social video where the clip asked for 0:03–0:13 came back as 0:00–0:13: the source had one keyframe at 0:00 and none for a long while after. The same source was also VP9 in MP4, which plenty of phones will not play.
So for short clips there is now a second mode: if the copy would start more than about 1.5 s early, or the picture is not H.264, re-encode instead:
ffmpeg -ss 3 -t 10 -i "$VIDEO_URL" -ss 3 -t 10 -i "$AUDIO_URL" \
-map 0:v:0 -map 1:a:0 -c:v libx264 -preset veryfast -crf 20 \
-pix_fmt yuv420p -c:a aac -b:a 160k -movflags +faststart clip.mp4
With -ss before -i and re-encoding, ffmpeg decodes from the previous keyframe and discards frames until the exact start, so the cut is frame-accurate. Re-encoding costs CPU, so it only applies to clips up to 90 seconds; longer clips keep the copy path.
Lesson: test with real sources from each platform, not just one. Keyframe spacing varies a lot.
Live streams: playlists are a sliding window
A live HLS playlist only lists the most recent segments, the rewind window. Some platforms keep hours, others a few minutes. Each segment can carry #EXT-X-PROGRAM-DATE-TIME, so you can map "12:04 wall-clock" to a segment.
For a clip of what already aired:
- Load the media playlist, map segments to wall-clock times.
- Pick the segments overlapping the requested range.
- Download just those segments in parallel.
- Concatenate and trim the edges with ffmpeg's concat demuxer.
For "record the next five minutes", poll the playlist every target-duration seconds and collect new segments until the end time passes.
The tricky part is the window moving while the user is choosing. If someone picks the oldest visible moment and takes a while, that moment may have slid out of the playlist by the time the job starts. The honest answer is a clear error ("older than the rewind window") plus better logging. I am still collecting data on how often it happens.
Things I would tell myself on day one
- Validate every URL server-side. A tool that fetches user-supplied links is an SSRF risk: resolve DNS, refuse private ranges, re-check every redirect.
-
Probe the output before handing it over. A media request that fails halfway can leave a file without sound or cut short;
ffprobethe result and refuse clips under half the expected length. - Delete files quickly. Finished clips are removed 15 minutes after they are ready.
If you want to see it in action: paste a Facebook, X or TikTok link, or a live stream, and pick a start and an end. It is free, no account. Feedback on the approach is very welcome in the comments.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.