commit 7bef35c3f6b86db061a2e86013bf72a886ab0840
parent 534d2ab62c86693be1a09342b4208082f5e373b9
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Tue, 27 Oct 2026 12:05:02 -0400
plans: live pre-clip — transcribe and mine a live stream while it records
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
| A | plans/live-preclip.md | | | 150 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 150 insertions(+), 0 deletions(-)
diff --git a/plans/live-preclip.md b/plans/live-preclip.md
@@ -0,0 +1,150 @@
+# Live pre-clip — transcribe and mine a live stream while it records (lane `lane/live-preclip`)
+
+Asked by the operator on 2026-10-27: "I want to add the ability to start pre-clipping a live stream by
+periodically copying the partial file, transcribing it, and mining clip windows." Two additions the same day:
+"since streams download first-to-last naturally the partial mined clips will still be relevant on the full
+video" (the partial's clock IS the final video's clock: no re-anchoring, a cheap check, a flag when it fails),
+and "it'd also be cool to get shareable clips from a partial in case something really interesting happens on a
+long stream" (a captioned, X-sized clip of a window while the stream is still live).
+
+## What was measured (ffmpeg n9.0.2, 2026-10-27)
+
+- A growing MPEG-TS (a 20-minute TS cut at an arbitrary byte, mid-packet): `ffprobe` reads its duration from the
+ last PTS it finds (234.76 s for 7 MB of 35.8 MB — exact); `ffmpeg -ss 200 -i grow.ts -vn …wav` takes 0.18 s
+ and gives the 34.6 s from 200 s to the end of what is written. The trailing partial packet is dropped.
+- A growing fragmented MP4 (audio, 1-hour, 2 s fragments, cut mid-fragment at 10 MB of 14.9 MB): `ffprobe` gives
+ 2420.7 s; `ffmpeg -ss 2300 -i grow.m4a` takes 0.34 s. The partial last fragment logs a decoder error and the
+ exit is still 0.
+- `-ss` on an input is relative to the input's start time, and the landing's remux (`-c copy`, no `-copyts`)
+ subtracts the same start time: a TS whose audio starts at PTS 1001.336 lands as an mp4 whose audio starts at
+ 0. So **second S of the growing file, read with `-ss S`, is second S of the landed video**, for the muxed
+ recordings; for separate streams the merge subtracts each input's own start, so second S of the growing
+ audio stream is second S of the landed video's audio.
+
+## Design
+
+### The per-tick snapshot: a byte prefix, read by time
+
+A recording is append-only. The bytes a recorder has written never change, so the "stable, readable copy" of a
+tick is the prefix of the file already written — nothing is copied. Each tick:
+
+1. finds the part being recorded (`live-recording.json`'s parts — each now records its `stem` — and the files
+ named for it, `lib/liveCapture.ts livePartFiles`) and the AUDIO-BEARING file in it:
+ - muxed (`<stem>.ts`: yt-dlp `--hls-use-mpegts`, the browser path's ffmpeg, Kick's VOD poller): the file;
+ - separate (`<stem>.f<id>.<ext>`, YouTube): the stream ffprobe says holds audio (cached per file); the video
+ stream is never read by a tick;
+ - Kick records two parts at once (the VOD from segment 0, the channel from now): the from-start part wins.
+2. probes how many seconds it holds (`ffprobe` format duration — the TS tail's last PTS, the fMP4's fragments;
+ cheap, never proportional to the stream) and keeps a 2 s margin from the live edge;
+3. cuts ONLY the new span's audio — `ffmpeg -ss <from> -i <file> -t <span> -vn -ac 1 -ar 16000` — into a 16 kHz
+ WAV in the pre-clip's work dir on the store's disk (`<store>/<slug>/.<id>.preclip/`, never /tmp, removed
+ after each tick and at the end). The cost of a tick is the new span: ffmpeg seeks a TS by its timestamps and
+ an fMP4 by its fragments.
+
+The span starts `overlapSeconds` (15) before the end of what is transcribed, so a word cut by the boundary is
+heard whole once; the splice keeps the old cues before the overlap's middle and the new ones from it.
+
+### The job: `live-preclip`, one per stream
+
+Its own job kind and queue (`live-preclip:<slug>:<id>`), separate from the recorder's: starting or stopping
+pre-clip never touches the recording. `record-live {"preclip": true | {everyMinutes, maxChunkMinutes, scan}}`
+starts it once the first part begins (and implies `transcribe: true` for the landing); `pnpm ops live-preclip
+{slug, id, action: "start"}` starts it on a recording already running; `"stop"` cancels it; `"status"` prints
+its state; `"finalize"` runs the supersede by hand. The default period and caps are settings
+(`settings.livePreclip`).
+
+Ticks never pile up: the job IS the queue. One transcription is in flight at a time, on a worker lease from the
+pool (`transcribeFile.ts runTranscribeFile`, the path `pnpm ops transcribe` and find-clips' word timings take —
+the "foreground" tier: ahead of the lane's background units, behind a manual batch queued first). While it waits
+for a worker or runs, no further tick is asked for; when it returns, the next tick covers everything since,
+capped at `maxChunkMinutes` (20) — and a tick that leaves the transcript behind by more than a period starts
+the next at once (catch-up) instead of sleeping.
+
+Skipped, and said in the log, never silently: a tick with less than 30 s new ("no new data"); MemAvailable
+under the heavy slot's floor (`HEAVY_MIN_FREE_MB`, 6000 — re-checked every minute, one line per due tick); the
+store's disk under 2 GB free; a part that is not readable yet. A recorder restart or fallback (a new part) is a
+new BASIS: when the new part's opening audio matches the old one's (the fingerprint below) the transcript goes
+on; else it starts again on the new part's clock, and umtool is told (the project's windows are flagged).
+
+### The live transcript: `data/<id>/live-transcript.json`
+
+A per-video sidecar (`sidecar()`; not `transcript.<x>.<y>`, so it is never a caption track, and not
+`transcript.json`, so the index, find-clips and the transcription lane never read it as THE transcript):
+`{version, provisional: true, status: "live" | "landed" | "superseded", basis {stem, file, audioFile},
+transcribedTo, cues [{start, end, text}], words [{w, start, end}], scan {hop, window, energy[], laugh[] | null,
+model}, chunks [{from, to, cues, words, worker, at}], fingerprint {from, hop, env[]}, updatedAt}`, every time on
+the stream's (= the landed video's) clock. `live-preclip.json` beside it is the job's state: status, options,
+ticks, skips by reason, the last ten tick lines, transcribed seconds, what was mined, the landing check.
+
+The laugh scan runs on each tick's WAV (`lib/clips/scan.mjs runAudioScan`, the same CED tagger umtool's
+`clips analyze` uses, energy only when no model) and is spliced into the doc by window index: the scan is
+incremental too.
+
+### Mining: umtool `POST /api/report/clips {mode: "live"}`
+
+Synchronous (a proposal pass is arithmetic; it does not wait behind a render on umtool's one-job runner). umtool
+reads the editor's `live-transcript.json` from the corpus and keeps the project's own copy of the recording in
+its `.shadow-channels/<slug>/data/<id>/` (a project with a shadow dir reads its recording from there —
+`report-to-video/sources.mjs channelsDirFor`): `transcript.cues.json` (the live cues), a synthetic
+`diarization.json` (one speaker, turns from the cues — the clip finder needs turns; the real diarization comes
+with the landing), `metadata.info.json` (the record's), `clips -> <corpus>/data/<id>/clips` (the editor's clip
+cache, where a live share's window lands), plus `<project>/words.json` (the live words) and the scan cache
+`scans/<slug>--<id>.audio.json` (the live scan). The first call scaffolds the project with the reactions seed
+(`<id>-live` unless named); every call runs `proposeProject` over `[0, transcribedTo − 20 s]` (the live edge is
+left for the next tick, so a proposal is never cut mid-thought). Ids are stable (`stableIds`: a proposal that
+overlaps an earlier one keeps its id), ruled and edited clips are protected (`rulingsGuard`), so the windows
+grow without re-proposing what was proposed. No model step: when the CED model is missing the laughs are the
+transcript's markers and the energy bursts (the clip finder's own fallback), and the phrase finder and the clip
+editor's strip work over the shadow cues as over any recording.
+
+### When the stream ends: the supersede
+
+The recording lands as before (the longest part). The pre-clip job then:
+
+1. **checks the clock**: the landed part is the basis part → "same recording" (append-only, nothing to check);
+ else the RMS envelope of 30 s of audio (100 ms frames, from 60 s in) of the landed video is compared with
+ the one taken from the basis when it was recorded: correlation ≥ 0.75 at a lag ≤ 0.3 s → "same"; a
+ correlation at a larger lag → "shifted by N s" (flagged, not moved); none → "mismatch"; silence → "unverified";
+2. marks the live transcript `landed`, then waits (a stat every 2 minutes, up to 24 h) for the final
+ `transcript.json`; then marks it `superseded` and tells umtool `{mode: "live", final: true, check}`: the
+ shadow dir goes (the project now reads the corpus — the final cues and, once diarized, the speakers), the
+ project's `words.json` becomes the final words (data/<id>/words.json when find-clips made it, else it is
+ removed so captions come from transcript.json), and the windows stay as they are, by source second — flagged
+ in the clip editor when the check was not "same". A wait that times out leaves `landed` and `pnpm ops
+ live-preclip {action: "finalize"}` does the rest.
+
+The provisional and the final transcripts are never mixed: different files, and every reader that prefers the
+live one does so only while `transcript.json` is absent.
+
+### Shareable clips from a partial
+
+`pnpm ops live-clip {slug, id, start, end | clip, project?}` and the clip editor's **Share now** (a live
+project): umtool `POST /api/report/live-share {project, clip | start, end}`:
+
+1. asks the editor `POST /api/media/live-window {slug, id, from, to}` (WORKER_TOKEN): the window (padded 3 s) is
+ cut from the growing recording NOW — `ffmpeg -ss -i <part> [-ss -i <audio>] -t … -c copy` — into the clip
+ cache, `data/<id>/clips/<from>-<to>.mp4` with its provenance (`via: "live-recording"`). This is the
+ snapshot-on-demand: it reads the bytes written so far, so it is never older than the request; a window past
+ what is recorded is refused with how much is. After the landing the same call cuts from the saved video. No
+ network request, and the recorder is only read;
+2. adds the clip to the project when given by seconds (the clip editor's `add`);
+3. makes its short (`build-video.mjs --short`, `--skip-fetch --no-network`: the window above is the source, the
+ live words the captions), refusing a clip over 2:20; a short over 512 MiB (it is not, at 2:20) is refused
+ with the size rather than posted.
+
+## What is incremental, and what is not
+
+| Step | Per tick | Notes |
+|---|---|---|
+| audio cut | the new span | ffmpeg seeks by time; nothing is copied |
+| transcription | the new span (+15 s overlap) | one lease at a time, coalesced, capped |
+| laugh / energy scan | the new span | spliced by window index |
+| proposal pass | the whole transcript | arithmetic over cues/words/scan, milliseconds to seconds |
+| shadow files | rewritten whole | JSON of the transcript so far (MBs at 4 h) |
+
+## Owed / not built
+
+- Speakers during the stream (a live diarization): one synthetic speaker until the landing.
+- Chat during the stream: the replay is fetched after (fetch-chat), not live.
+- MPEG-TS PTS wrap (26.5 h) and a discontinuity inside one part are not handled: a part that resets its clock is
+ read as ffmpeg reads it.