import path from "node:path"; import { readFile } from "node:fs/promises"; // THE CAPTION TRACKS A VIDEO'S metadata.info.json LISTS: yt-dlp's // `subtitles` (uploaded by the channel) and `automatic_captions` (YouTube's // speech recognition). `live_chat` is left out — yt-dlp files YouTube's chat // replay under `subtitles`, and it is not a transcript of speech. // // The one rule for "this video has captions to fetch": the managed download's // no-subs fallback (downloadOneManaged: audio + whisper only when none is // listed), and a Transcribe asked of a youtube-handling video with no audio // (videoActions.ts whisperVideoAction: its English captions are fetched // instead, unless `asr: true`). type CaptionMaps = { subtitles?: unknown; automatic_captions?: unknown; }; // Every listed track, null when the metadata cannot say (not an object, or a // map that is not one). export function listedCaptionTracks(meta: unknown): string[] | null { if (!meta || typeof meta !== "object") return null; const m = meta as CaptionMaps; const subs = m.subtitles ?? {}; const auto = m.automatic_captions ?? {}; if (typeof subs !== "object" || typeof auto !== "object") return null; const tracks = new Set(); for (const k of Object.keys(subs as object)) if (k !== "live_chat") tracks.add(k); for (const k of Object.keys(auto as object)) if (k !== "live_chat") tracks.add(k); return [...tracks]; } // The listed tracks of the video in `videoDir`; null with no readable // metadata.info.json. export async function readListedCaptionTracks(videoDir: string): Promise { let raw: string; try { raw = await readFile(path.join(videoDir, "metadata.info.json"), "utf8"); } catch { return null; } try { return listedCaptionTracks(JSON.parse(raw)); } catch { return null; } } // An English track key — `en`, `en-orig`, `en-US`, … (yt-dlp's language // codes; `es-en` is a Spanish track, not this). export function isEnglishCaptionTrack(track: string): boolean { return /^en(-|$)/i.test(track); } // The English keys of one caption map, sorted. export function englishCaptionTracks(map: unknown): string[] { if (!map || typeof map !== "object" || Array.isArray(map)) return []; return Object.keys(map).filter(isEnglishCaptionTrack).sort(); }