// THE YOUTUBE TEXT (release 28, M6): what a cut's upload says, composed by // CODE from `publish.youtube { title, description }` (drafted by the youtube // task or written by hand), the build's own chapter list and the cut's // citations. The build writes it to `out/youtube.txt` (`out/youtube-.txt` // for a variant other than `sourced`, as the cut's mp4 is named); umtool shows // and copies it, and previews it the same way, without chapters, before a build. // // // // <description> // // Chapters // 0:00 <chapter> // … // // Sources // <a moment link per cited clip, in cut order, each once> // // The chapters are YouTube's: the first at 0:00, at least three, each at // least ten seconds. A chapter shorter than that is folded into the one // before it (a first one shorter than that takes the next one's span). A list // that is still too short, or does not start at 0:00, is left out -- and the // result's `notes` say why. // // PURE: no fs. build-video.mjs writes the file. import { archiveMomentUrl } from "./site-links.mjs"; /** YouTube's rules for a description's chapters. */ export const YOUTUBE_CHAPTERS = Object.freeze({ min: 3, minSeconds: 10 }); /** `m:ss`, or `h:mm:ss` from an hour. */ export function chapterStamp(seconds) { const s = Math.max(0, Math.floor(seconds)); const h = Math.floor(s / 3600); const m = Math.floor((s % 3600) / 60); const ss = String(s % 60).padStart(2, "0"); return h ? `${h}:${String(m).padStart(2, "0")}:${ss}` : `${m}:${ss}`; } const oneLine = (s) => String(s ?? "").replace(/\s+/g, " ").trim(); /** * The chapters as YouTube takes them, from the build's list (`{ start, end?, * title }`, seconds, in order; `total` the cut's length for the last one's * span): `{ chapters, note }` -- `chapters` null when the block is left out, * `note` saying what was folded or why the block is left out. * * With the deck's `markers` (and the opening's title, `first`), the chapters * are its sections (markerChapters) when the list names each entry (`id`) and * where its segment starts in the cut (`cutStart`). * * @param {Array<{ start: number, end?: number, title: string, section?: boolean, id?: string, cutStart?: number }> | null | undefined} list * @param {number | null} [total] * @param {{ markers?: Array<{ after?: string, label?: string, level?: number }> | null, first?: string | null }} [opts] */ export function youtubeChapters(list, total = null, { markers = null, first = null } = {}) { if (!Array.isArray(list) || !list.length) return { chapters: null, note: "no chapters: the build made none" }; // A deck with markers: its sections are the chapters (markerChapters), at // the second each section's first segment starts in the cut (`cutStart`, // before the crossfade it lands after). The list must say which entry each // chapter is (`id`): the build's does. if (hasChapterMarkers(markers) && list.every((c) => typeof c?.id === "string" && Number.isFinite(c?.cutStart))) { const r = markerChapters({ markers, segments: list.map((c) => ({ id: c.id, start: c.cutStart })), total, first }); return { chapters: r.chapters, note: r.notes.join("; ") }; } // The build's list has a chapter per segment (a player's navigation). A // description's chapters are the cut's SECTIONS: when the list says which // entries open one (`section`: a title or chapter card), every other entry // folds into the section before it. if (list.some((c) => c?.section === true)) list = list.filter((c, i) => i === 0 || c?.section === true); const all = list.map((c) => ({ start: Math.floor(Number(c.start)), title: oneLine(c.title) || "Chapter" })); if (all[0].start !== 0) return { chapters: null, note: `no chapters: the first starts at ${chapterStamp(all[0].start)}, not 0:00` }; const end = Number.isFinite(Number(total)) && total !== null ? Math.floor(Number(total)) : Math.floor(Number(list.at(-1).end ?? all.at(-1).start + YOUTUBE_CHAPTERS.minSeconds)); const kept = [all[0]]; let folded = 0; for (const c of all.slice(1)) { if (c.start - kept.at(-1).start < YOUTUBE_CHAPTERS.minSeconds) { folded++; // A first chapter too short to stand takes the next one's span under its own 0:00. continue; } kept.push(c); } // The last must run ten seconds too. while (kept.length > 1 && end - kept.at(-1).start < YOUTUBE_CHAPTERS.minSeconds) { kept.pop(); folded++; } const foldNote = folded ? `${folded} chapter(s) under ${YOUTUBE_CHAPTERS.minSeconds} s folded into the one before` : ""; if (kept.length < YOUTUBE_CHAPTERS.min) { return { chapters: null, note: `no chapters: ${kept.length} of at least ${YOUTUBE_CHAPTERS.min}` + (foldNote ? ` (${foldNote})` : ""), }; } return { chapters: kept, note: foldNote }; } // ---- chapters from the deck's markers (2026-11-02) ------------------------ // // A cut whose deck carries markers (`render.chrome.deck.markers`) has its // sections named already: the rail's flags. Its chapters are those -- each // TOP-LEVEL marker (a `level: 2` subsection is a tick inside its section, not // a chapter) at the second the section after it starts in the master: the // start of the segment after the clip it follows (`after`), where the // dissolve into that section begins. The stretch before the first marker is // the opening, titled `deck.sectionTitle.first` (the section title's "first // section's title"); with none, the opening takes the first section's label // and that section starts at 0:00. Replaces the hand script that read // schedule.json and the markers (night1030 chapters.py), row for row. /** Does this deck have a marker a chapter can be made of (top-level, labelled)? */ export function hasChapterMarkers(markers) { return Array.isArray(markers) && markers.some((m) => m && m.level !== 2 && oneLine(m.label)); } /** * YouTube's rules over a list in order (`{ start, title }`, seconds), with * what had to change said by name: the first at 0:00 (a list that does not * start there is left out), each at least ten seconds (one that starts under * ten seconds after the chapter before it is merged into that one -- but an * OPENING under ten seconds gives 0:00 to the chapter after it), the last * ten seconds before `total` too, at least three. * * @param {Array<{ start: number, title: string }>} list * @param {number | null} total * @returns {{ chapters: Array<{ start: number, title: string }> | null, notes: string[] }} */ export function youtubeChapterRules(list, total = null) { const notes = []; const all = list.map((c) => ({ start: Math.floor(Number(c.start)), title: oneLine(c.title) || "Chapter" })); if (!all.length) return { chapters: null, notes: ["no chapters: none to make"] }; if (all[0].start !== 0) return { chapters: null, notes: [`no chapters: the first starts at ${chapterStamp(all[0].start)}, not 0:00`] }; const min = YOUTUBE_CHAPTERS.minSeconds; const kept = [all[0]]; for (const c of all.slice(1)) { const prev = kept.at(-1); const gap = c.start - prev.start; if (gap >= min) { kept.push(c); continue; } if (kept.length === 1 && prev.start === 0) { notes.push(`the opening “${prev.title}” runs ${gap} s, under ${min}: “${c.title}” starts at 0:00, not ${chapterStamp(c.start)}`); kept[0] = { start: 0, title: c.title }; continue; } notes.push(`“${c.title}” (${chapterStamp(c.start)}) is ${gap} s after “${prev.title}”, under ${min}: merged into it`); } const end = Number.isFinite(Number(total)) && total !== null ? Math.floor(Number(total)) : null; while (end !== null && kept.length > 1 && end - kept.at(-1).start < min) { const last = kept.pop(); notes.push(`“${last.title}” (${chapterStamp(last.start)}) runs ${end - last.start} s to the end, under ${min}: merged into “${kept.at(-1).title}”`); } if (kept.length < YOUTUBE_CHAPTERS.min) { notes.unshift(`no chapters: ${kept.length} of at least ${YOUTUBE_CHAPTERS.min}`); return { chapters: null, notes }; } return { chapters: kept, notes }; } /** * The chapters a cut's deck markers make, by YouTube's rules * (youtubeChapterRules): `{ chapters, notes }`, `chapters` null when the deck * has no top-level labelled marker in the cut, or too few survive. * * @param {object} o * @param {Array<{ after?: string, label?: string, level?: number }>} o.markers `deck.markers` * @param {Array<{ id: string, start: number }>} o.segments the master's segments in order, each where it starts (schedule.json's) * @param {number | null} [o.total] the master's length * @param {string | null} [o.first] the opening's title (`deck.sectionTitle.first`) */ export function markerChapters({ markers, segments, total = null, first = null }) { const notes = []; const index = new Map((segments ?? []).map((s, i) => [s.id, i])); const rows = []; for (const m of Array.isArray(markers) ? markers : []) { if (!m || m.level === 2) continue; const label = oneLine(m.label); const after = String(m.after ?? "").trim(); if (!label) continue; const i = index.get(after); if (i === undefined) { notes.push(`“${label}” follows ${after || "no clip"}, which is not in the cut: no chapter`); continue; } const next = segments[i + 1]; if (!next) { notes.push(`“${label}” follows the cut's last segment: no chapter`); continue; } rows.push({ start: Number(next.start), title: label }); } if (!rows.length) return { chapters: null, notes: [...notes, "no chapters: no labelled top-level marker in the cut"] }; rows.sort((a, b) => a.start - b.start); const opening = oneLine(first); if (opening) rows.unshift({ start: 0, title: opening }); else if (Math.floor(rows[0].start) !== 0) { notes.push(`the opening has no title (deck.sectionTitle.first): “${rows[0].title}” starts at 0:00, not ${chapterStamp(rows[0].start)}`); rows[0] = { ...rows[0], start: 0 }; } const r = youtubeChapterRules(rows, total); return { chapters: r.chapters, notes: [...notes, ...r.notes] }; } /** The chapter block's lines (`0:00 Title`), as out/youtube.txt and `umtool export --format chapters` print them. */ export function chapterLines(chapters) { return (chapters ?? []).map((c) => `${chapterStamp(c.start)} ${oneLine(c.title)}`); } /** * A clip's moment link: its `citeUrl` when it has one, else the archive's page * at the cited second (`cite`, else `start`) -- the link the citation makes * (`<origin>/?v=<channel>%2F<video>&t=<s>`). A clip from a file beside the * manifest (`src`) with no `citeUrl` has none; nor has any other entry. */ export function momentLinkOf(entry, provenance = {}) { if (entry?.type !== "clip") return null; if (entry.citeUrl) return entry.citeUrl; if (entry.src != null) return null; const origin = String(provenance?.siteOrigin ?? "").replace(/\/+$/, ""); const channel = entry.channel ?? provenance?.channelSlug; if (!origin || !channel || !entry.video) return null; const at = Number.isFinite(Number(entry.cite)) ? Number(entry.cite) : Number(entry.start); return archiveMomentUrl(entry, provenance, at, { origin }); } /** The cut's moment links, in cut order, each once. */ export function youtubeSources(entries, provenance = {}) { const out = []; const seen = new Set(); for (const e of entries ?? []) { const href = momentLinkOf(e, provenance); if (!href || seen.has(href)) continue; seen.add(href); out.push(href); } return out; } /** * The text. `chapters` is youtubeChapters' (null: no block); `sources` the * links. Null when the manifest has no `publish.youtube` title. * * @param {{ youtube?: { title?: string, description?: string } | null, chapters?: Array<{ start: number, title: string }> | null, sources?: string[] }} o * @returns {string | null} */ export function composeYoutubeText({ youtube, chapters = null, sources = [] }) { const title = oneLine(youtube?.title); if (!title) return null; const parts = [title]; const description = String(youtube?.description ?? "").trim(); if (description) parts.push(description); if (chapters?.length) parts.push(["Chapters", ...chapterLines(chapters)].join("\n")); if (sources.length) parts.push(["Sources", ...sources].join("\n")); return `${parts.join("\n\n")}\n`; } /** The file a variant's text is written to, in the project's `out/`. */ export function youtubeTextName(variant = "sourced") { return variant === "sourced" ? "youtube.txt" : `youtube-${variant}.txt`; } /** * The preview umtool shows before a build: composed the same way, without * chapters (the build makes them), over the default cut's entries -- an entry * tagged with another `variant` is left out, as build-video's selectVariant * leaves it out of `sourced`. * * @returns {{ text: string | null, note: string }} */ export function youtubePreview(manifest) { const entries = (Array.isArray(manifest?.timeline) ? manifest.timeline : []).filter((e) => !e?.variant || e.variant === "sourced"); const text = composeYoutubeText({ youtube: manifest?.publish?.youtube, chapters: null, sources: youtubeSources(entries, manifest?.provenance) }); return { text, note: "chapters are added by the build" }; }