// The words a measure slice times its rows by (measure-slice.mjs): for each // clip of the cut that shows a measure row, its words in SOURCE seconds and // where they came from. Reads only. Plain ESM. // // The first of, per channels tree given (the project's own first -- its // `provenance.channelsDir` or `.shadow-channels/` -- then the corpus): // // timed words clip-words.mjs clipWords: the word-timings sidecar, a // whisper transcript's tokens, or the English caption track's // word starts -> "words" // the cues the cue record (`transcript.cues.json`), or the build's own // cue reader (`cues(entry)`), each cue's words spread over it // by their characters (fx.mjs fxWordsOf) -- an ESTIMATE, a // second or so either way -> "cues" // // A clip with neither lands on its quoteStart (the slice's fallback). import { readFile } from "node:fs/promises"; import path from "node:path"; import { resolveVideoDir } from "yt-dlp-transcript-common/lib/videoDir-server.mjs"; import { clipWords } from "./clip-words.mjs"; import { fxWordsOf } from "./fx.mjs"; import { hasLocalMedia } from "./local-media.mjs"; /** * @param {{ entries: object[], ids: Set, provenance?: object, * channelsDirs?: Array, cues?: ((entry: object) => Promise>) | null }} a * `ids` the entries that show a measure row. * @returns {Promise, from: string }>>} */ export async function measureClipWords({ entries, ids, provenance = {}, channelsDirs = [], cues = null }) { const out = new Map(); const dirs = [...new Set(channelsDirs.filter(Boolean))]; for (const e of entries) { if (e?.type !== "clip" || !ids.has(e.id) || !Number.isFinite(e.start) || !Number.isFinite(e.end)) continue; let got = null; for (const dir of dirs) { const w = await clipWords(e, provenance, e.start - 1, e.end + 1, { channelsDir: dir }).catch(() => null); if (w?.words?.length) { got = { words: w.words.map((x) => ({ text: x.text, start: x.start, end: x.end ?? x.start })), from: "words" }; break; } } if (!got && !hasLocalMedia(e)) { for (const dir of dirs) { const spans = await cueFile(dir, e.channel ?? provenance.channelSlug, e.video); const words = spans ? fxWordsOf(spans, e.start - 1, e.end + 1) : []; if (words.length) { got = { words, from: "cues" }; break; } } } if (!got && cues) { const spans = await cues(e).catch(() => []); const words = fxWordsOf(spans ?? [], e.start - 1, e.end + 1); if (words.length) got = { words, from: "cues" }; } if (got) out.set(e.id, got); } return out; } /** A video's cue record in a channels tree, or null. */ async function cueFile(channelsDir, slug, video) { if (!channelsDir || !slug || !video) return null; try { const dir = await resolveVideoDir(channelsDir, slug, video); const doc = JSON.parse(await readFile(path.join(dir, "transcript.cues.json"), "utf8")); return Array.isArray(doc?.cues) ? doc.cues : null; } catch { return null; } }