// A LATER DOWNLOAD NEVER CLOBBERS A LONGER LANDED RECORDING (plans/live-capture.md). // // A video whose source was landed by import-media or record-live // (controller/landMedia.ts — `media-import.json` beside the record, the // pointer's `origin.requestedBy`) may be downloaded again by the ordinary // paths: a sync that finds the VOD, a "Persist source video", a refetch. The // VOD can be SHORTER than the recording (a streamer trims it, a platform cuts // the first hours), so: // // audio before the download the held audio is set aside // (`audio.mp3` → `audio.mp3.landed-kept`, through its tier link — // lib/mediaTier-server.ts renameMediaFile; a scratch name the tier // never moves). After it, the longer of the two stays // (KEEP_LONGER_TOLERANCE_SECONDS of slack; a tie keeps the landed // one) and the other is removed with removeMediaFile. No new file // (a failed or skipped download): the aside one goes back. // container NEITHER COPY IS DISCARDED (ytdlp/savedAlternates.ts, asked by // finalizeAppExtraction for any video with a container already // in the store): both are kept, one active, the other an // alternate flagged on the pointer for a later check. // // Every decision is appended to media-import.json's `challenges`. import path from "node:path"; import { readdir } from "node:fs/promises"; import { KEEP_LONGER_TOLERANCE_SECONDS, type MediaImportChallenge } from "../lib/mediaImport"; import { isRealAudioFile, pickPreferredAudio } from "../lib/mediaFiles"; import { mediaImportSidecar } from "../lib/mediaImport-server"; import { removeMediaFile, renameMediaFile } from "../lib/mediaTier-server"; import { loadSavedVideo } from "../lib/savedVideo-server"; export const LANDED_KEPT_SUFFIX = ".landed-kept"; export type LandedGuardDeps = { // Seconds of a media file (null: not measurable). measure: (file: string) => Promise; now?: () => Date; }; export type LandedGuardPlan = { videoDir: string; asides: string[] }; // attach-media too: a container attached from a local archive is a recording // somebody kept on purpose (keepReason "pin"), as an import is. export const LANDED_BY: ReadonlySet = new Set(["import-media", "record-live", "attach-media"]); // Was this video's source landed (rather than downloaded)? export async function isLandedVideo(videoDir: string): Promise { if ((await mediaImportSidecar.load(videoDir))?.entries.some((e) => e.kept === "new")) return true; const pointer = await loadSavedVideo(videoDir); return pointer?.origin?.requestedBy !== undefined && LANDED_BY.has(pointer.origin.requestedBy); } export async function recordChallenge(videoDir: string, c: MediaImportChallenge): Promise { const prev = await mediaImportSidecar.load(videoDir); if (!prev) return; await mediaImportSidecar.write(videoDir, { ...prev, challenges: [...(prev.challenges ?? []), c].slice(-50) }); } // Before a managed download of a landed video: set its audio aside. Null for // any other video (the download is then an ordinary one). A leftover aside // from a run cut short is put back first. export async function prepareLandedGuard(videoDir: string, onLog: (s: string) => void): Promise { let entries = await readdir(videoDir).catch(() => [] as string[]); for (const left of entries.filter((n) => n.endsWith(LANDED_KEPT_SUFFIX))) { const name = left.slice(0, -LANDED_KEPT_SUFFIX.length); if (entries.includes(name)) await removeMediaFile(videoDir, name); await renameMediaFile(videoDir, left, name); onLog(`Put back ${name}, set aside by an interrupted download.\n`); } if (!(await isLandedVideo(videoDir))) return null; entries = await readdir(videoDir).catch(() => [] as string[]); const asides: string[] = []; for (const name of entries.filter(isRealAudioFile)) { await renameMediaFile(videoDir, name, name + LANDED_KEPT_SUFFIX); asides.push(name); } if (asides.length) { onLog(`This video's source was landed (import or live recording): ${asides.join(", ")} is set aside; the longer recording stays.\n`); } return { videoDir, asides }; } // After the download: keep the longer audio. export async function settleLandedGuard( plan: LandedGuardPlan, deps: LandedGuardDeps, onLog: (s: string) => void, ): Promise { const { videoDir, asides } = plan; if (asides.length === 0) return; const entries = await readdir(videoDir).catch(() => [] as string[]); const fresh = pickPreferredAudio(entries.filter(isRealAudioFile)); const restore = async () => { for (const name of asides) { if (entries.includes(name)) await removeMediaFile(videoDir, name); await renameMediaFile(videoDir, name + LANDED_KEPT_SUFFIX, name); } }; if (!fresh) { await restore(); onLog(`No new audio: the landed ${asides.join(", ")} is back.\n`); return; } const kept = pickPreferredAudio(asides) ?? asides[0]!; const newSeconds = await deps.measure(path.join(videoDir, fresh)); const keptSeconds = await deps.measure(path.join(videoDir, kept + LANDED_KEPT_SUFFIX)); const newWins = newSeconds !== null && (keptSeconds === null || newSeconds > keptSeconds + KEEP_LONGER_TOLERANCE_SECONDS); if (newWins) { for (const name of asides) await removeMediaFile(videoDir, name + LANDED_KEPT_SUFFIX); } else { for (const name of entries.filter(isRealAudioFile)) await removeMediaFile(videoDir, name); await restore(); } const line = `The download's ${fresh} (${newSeconds ?? "?"} s) against the landed ${kept} (${keptSeconds ?? "?"} s): ` + `kept the ${newWins ? "download's" : "landed"} one.`; onLog(`${line}\n`); await recordChallenge(videoDir, { at: (deps.now?.() ?? new Date()).toISOString(), by: "download", what: "audio", newSeconds, landedSeconds: keptSeconds, kept: newWins ? "download" : "landed", }); }