// A SHAREABLE CLIP OF A STREAM STILL LIVE (plans/live-preclip.md) — the clip // editor's Share now and `pnpm ops live-clip`: `POST /api/report/live-share // {project, clip | start, end, from?, replace?}`. // // 1. the clip: one of the project's, or a new one added at [start, end] (the // clip editor's `add`, with the clip finder's WINDOW_PAD_S either side); // at most 2:20 (X's limit); // 2. its source window — the build's own span (report-to-video/build-video.mjs // fetchSpan: the clip's extent plus render.fetchPad) — asked of the editor // (`POST /api/media/live-window`, WORKER_TOKEN): cut NOW from the part being // recorded, or from the saved video once it has landed, into the clip // cache the project's shadow recording links. Past the live edge the editor // answers 409 with how long to wait; // 3. its short (lib/report/shorts.mjs, `--skip-fetch --no-network`: the window // above is the source, the project's words.json — the live words — the // captions), as one job on the app's runner. // // No request leaves this machine. import path from "node:path"; import { WINDOW_PAD_S } from "yt-dlp-transcript-common/lib/clips/assist.mjs"; import { clipsOf, readManifest } from "../projects/report.mjs"; import { addClip } from "./manifest.mjs"; import { readLiveProject } from "./liveProject.mjs"; /** X's limit on a video's length, seconds (common/lib/livePreclip.ts LIVE_CLIP_MAX_SECONDS). */ export const LIVE_SHARE_MAX_SECONDS = 140; export class LiveShareError extends Error { /** @param {string} message @param {number} [status] */ constructor(message, status = 400) { super(message); this.status = status; } } const fin = (v) => typeof v === "number" && Number.isFinite(v); const r2 = (x) => Math.round(x * 100) / 100; /** The span the build reads for a clip (build-video.mjs fetchSpan, its default pad). */ export function shareSpan(entry, render) { const pad = fin(render?.fetchPad) ? render.fetchPad : 3; return { from: r2(Math.max(0, entry.start - pad)), to: r2(entry.end + pad) }; } /** * Steps 1 and 2: the clip (added when given by seconds) and its window, asked * of the editor through `askWindow({slug, id, from, to, clipId, manifest})`, * which resolves `{ ok, status, body }`. * @param {{ id: string, dir: string }} project * @param {Record} body * @param {{ askWindow: (req: Record) => Promise<{ ok: boolean, status: number, body: any }> }} o */ export async function prepareLiveShare(project, body, { askWindow }) { const manifest = await readManifest(project.dir); if (!manifest) throw new LiveShareError("no manifest", 404); const live = await readLiveProject(project.dir); let entry; let added = false; if (typeof body.clip === "string" && body.clip) { entry = clipsOf(manifest).find((e) => e.id === body.clip); if (!entry) throw new LiveShareError(`no clip ${body.clip} in ${project.id}`, 404); } else { if (!fin(body.start) || !fin(body.end) || body.end <= body.start) throw new LiveShareError('give "clip", or "start" and "end" (seconds of the stream)'); const from = typeof body.from === "string" ? /^([^/]+)\/([^/]+)$/.exec(body.from) : null; const channel = from?.[1] ?? live?.source?.channel ?? null; const video = from?.[2] ?? live?.source?.video ?? null; if (!video) throw new LiveShareError(`${project.id} is not a live project: say which recording ("from": "/")`); if (body.end - body.start > LIVE_SHARE_MAX_SECONDS + 1e-6) throw new LiveShareError(`a share is at most 2:20 (X's limit); this clip is ${Math.round(body.end - body.start)} s`); const res = await addClip(project.dir, { video, ...(channel && channel !== manifest.provenance?.channelSlug ? { channel } : {}), cutStart: body.start, cutEnd: body.end, start: Math.max(0, body.start - WINDOW_PAD_S), end: body.end + WINDOW_PAD_S, title: "live share", }); entry = res.entry; added = true; } const cutStart = fin(entry.cutStart) ? entry.cutStart : entry.start; const cutEnd = fin(entry.cutEnd) ? entry.cutEnd : entry.end; if (cutEnd - cutStart > LIVE_SHARE_MAX_SECONDS + 1e-6) { throw new LiveShareError(`${entry.id} is ${Math.round(cutEnd - cutStart)} s: a share is at most 2:20 (X's limit) — trim it first`); } const slug = entry.channel ?? manifest.provenance?.channelSlug ?? live?.source?.channel ?? null; if (!slug) throw new LiveShareError(`${entry.id} names no channel`); const span = shareSpan(entry, manifest.render); const w = await askWindow({ slug, id: entry.video, from: span.from, to: span.to, requestedBy: "umtool live-share", manifest: path.join(project.dir, "video.manifest.json"), clipId: entry.id, reason: `a live share of ${entry.id}`, }); if (!w.ok) throw new LiveShareError(`the editor did not cut the window: ${w.body?.error ?? `HTTP ${w.status}`}`, w.status === 409 ? 409 : 502); return { clip: entry.id, added, window: { from: w.body.from, to: w.body.to }, windowSource: w.body.source ?? null }; } /** The editor's live-window route (ARCHILYZER_EDITOR_URL, WORKER_TOKEN — fetch-via-editor.mjs's). */ export function editorWindowAsker({ editorUrl = process.env.ARCHILYZER_EDITOR_URL ?? "http://localhost:3001", token = process.env.WORKER_TOKEN ?? "", doFetch = fetch } = {}) { return async (req) => { if (!token) return { ok: false, status: 503, body: { error: "WORKER_TOKEN is not set on umtool: the editor cannot be asked for the window" } }; let res; try { res = await doFetch(`${editorUrl.replace(/\/+$/, "")}/api/media/live-window`, { method: "POST", headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, body: JSON.stringify(req), signal: AbortSignal.timeout(10 * 60_000), }); } catch (e) { return { ok: false, status: 502, body: { error: `the editor at ${editorUrl} did not answer (${e instanceof Error ? e.message : String(e)})` } }; } const body = await res.json().catch(() => ({})); return { ok: res.ok, status: res.status, body }; }; }