import { test } from "node:test"; import assert from "node:assert/strict"; import { chmod, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import type { Paths } from "../lib/paths"; import type { ChannelConfig } from "../lib/channelConfig"; import { downloadOneManaged, type ManagedDownloadOpts } from "./downloadOneManaged"; import { resolveDownloadFormatSelector } from "./downloadFormat"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test ytdlp/persistFormatPreset.test.ts // // THE PERSIST QUALITY. A pass that persists a source container takes // `persistFormatPreset`'s selector — unset is `bestvideo*+bestaudio/best`, as // every persist always was — prints the format yt-dlp actually took, and that // lands on the saved-video pointer. A "video_720" persist the last-resort rung // answered (a file above 720p) says so in the log. The yt-dlp here is a temp // node script; no network. const ID = "abc123def45"; const VIDEO = `https://www.youtube.com/watch?v=${ID}`; // The prefetch writes meta.json as the info json; the subtitle pass writes // nothing; the media pass (the `source-media` output) writes the container and // prints `format-line` (when present) the way `--print after_video:DLOM_FORMAT` // would. Every spawn appends its argv to `argv.log`. const FAKE_YTDLP = `#!/usr/bin/env node const fs = require("node:fs"); const path = require("node:path"); const argv = process.argv.slice(2); const root = process.env.FAKE_ROOT; fs.appendFileSync(path.join(root, "argv.log"), JSON.stringify(argv) + "\\n"); const has = (f) => argv.includes(f); const arg = (f) => { const i = argv.indexOf(f); return i < 0 ? undefined : argv[i + 1]; }; const info = arg("--load-info-json"); const id = info ? path.basename(path.dirname(info)) : "${ID}"; const dir = path.join("data", id); fs.mkdirSync(dir, { recursive: true }); if (has("--skip-download") && has("--write-info-json") && has("--no-write-subs")) { fs.copyFileSync(path.join(root, "meta.json"), path.join(dir, "metadata.info.json")); process.exit(0); } if (has("--skip-download") && has("--write-auto-subs")) { console.log("DLOM_ARCHIVE youtube " + id); process.exit(0); } if ((arg("-o") || "").includes("source-media")) { fs.writeFileSync(path.join(dir, "source-media.mp4"), "container bytes"); const fl = path.join(root, "format-line"); if (fs.existsSync(fl)) console.log("DLOM_FORMAT " + fs.readFileSync(fl, "utf8").trim()); console.log("DLOM_ARCHIVE youtube " + id); process.exit(0); } console.error("fake: unknown invocation " + argv.join(" ")); process.exit(2); `; const FAKE_FFMPEG = `#!/bin/sh for last; do :; done echo "extracted audio" > "$last" `; const META = { id: ID, title: "A video", duration: 60, webpage_url: VIDEO, subtitles: {}, automatic_captions: { en: [{ url: "https://example/cap" }] }, }; async function runWith(opts: { formatLine?: string; config?: Partial; managed?: Partial; }) { const root = await mkdtemp(path.join(tmpdir(), "persist-format-")); const ytdlp = path.join(root, "fake-ytdlp.cjs"); const ffmpeg = path.join(root, "fake-ffmpeg.sh"); await writeFile(ytdlp, FAKE_YTDLP); await writeFile(ffmpeg, FAKE_FFMPEG); await chmod(ytdlp, 0o755); await chmod(ffmpeg, 0o755); await writeFile(path.join(root, "meta.json"), JSON.stringify(META)); if (opts.formatLine) await writeFile(path.join(root, "format-line"), opts.formatLine); process.env.FAKE_ROOT = root; const paths = { transcriptsDir: root, channelsDir: path.join(root, "channels"), savedVideosDir: path.join(root, "saved-videos"), ytdlpBin: ytdlp, ffmpegBin: ffmpeg, ffprobeBin: path.join(root, "no-ffprobe"), } as Paths; const videoDir = path.join(paths.channelsDir, "demo-channel", "data", ID); await mkdir(videoDir, { recursive: true }); // A transcript on disk: the forced pass persists and does nothing else. await writeFile(path.join(videoDir, "transcript.json"), '{"segments":[]}'); let log = ""; const record = await downloadOneManaged({ channelSlug: "demo-channel", channelConfig: { handling: "youtube", url: "https://www.youtube.com/@demo/videos", ...opts.config, } as ChannelConfig, paths, videoUrl: VIDEO, onLog: (s) => { log += s; }, signal: new AbortController().signal, keepSourceVideoOverride: true, forceMedia: true, ...opts.managed, }); const argvs = (await readFile(path.join(root, "argv.log"), "utf8")) .split("\n") .filter(Boolean) .map((l) => JSON.parse(l) as string[]); const media = argvs.find((a) => (a[a.indexOf("-o") + 1] ?? "").includes("source-media")); const pointer = JSON.parse( await readFile(path.join(videoDir, "saved-video.json"), "utf8"), ) as Record; return { record, log, media: media ?? [], pointer, cleanup: () => rm(root, { recursive: true, force: true }), }; } const selectorOf = (argv: string[]) => argv[argv.indexOf("-f") + 1]; test("unset: the persist selector is bestvideo*+bestaudio/best, as it always was", async () => { const r = await runWith({ formatLine: "1080 avc1.640028 137+140" }); try { assert.equal(r.record.status, "ok"); assert.equal(selectorOf(r.media), "bestvideo*+bestaudio/best"); // The format yt-dlp took is asked for, and recorded under "original". assert.ok(r.media.some((a) => a.startsWith("after_video:DLOM_FORMAT ")), r.media.join(" ")); assert.deepEqual(r.pointer.format, { preset: "original", height: 1080, vcodec: "avc1.640028", }); assert.match(r.log, /Source format: 1080p avc1\.640028 \(format 137\+140\) \(quality original\)\.\n/); } finally { await r.cleanup(); } }); test("video_720: the persist takes the 720 selector and records what came back", async () => { const r = await runWith({ formatLine: "720 avc1.4d401f 136+140", managed: { persistFormatPreset: "video_720" }, }); try { assert.equal(selectorOf(r.media), resolveDownloadFormatSelector("video_720", null)); assert.deepEqual(r.pointer.format, { preset: "video_720", height: 720, vcodec: "avc1.4d401f", }); assert.doesNotMatch(r.log, /last resort/); } finally { await r.cleanup(); } }); test("video_720 answered by the last resort: a file above 720p is logged, not silent", async () => { const r = await runWith({ formatLine: "1080 vp9 248+251", managed: { persistFormatPreset: "video_720" }, }); try { assert.match( r.log, /Source quality video_720: no format at or under 720p matched any rung; the last resort \(bv\*\+ba\/b\) took 1080p vp9 \(format 248\+251\)\.\n/, ); assert.deepEqual(r.pointer.format, { preset: "video_720", height: 1080, vcodec: "vp9" }); } finally { await r.cleanup(); } }); test("original asked explicitly is the unset selector; no printed format → preset only", async () => { const r = await runWith({ managed: { persistFormatPreset: "original" } }); try { assert.equal(selectorOf(r.media), "bestvideo*+bestaudio/best"); assert.deepEqual(r.pointer.format, { preset: "original" }); assert.match(r.log, /Source format: not reported by yt-dlp \(quality original\)\.\n/); } finally { await r.cleanup(); } }); test("transcribe handling: a persisting primary takes the preset too; the audio selector is untouched", async () => { const r = await runWith({ formatLine: "480 avc1.4d401e 135+140", config: { handling: "transcribe", extractionMode: "app" }, managed: { persistFormatPreset: "video_720", forceMedia: false }, }); try { assert.equal(selectorOf(r.media), resolveDownloadFormatSelector("video_720", null)); assert.deepEqual(r.pointer.format, { preset: "video_720", height: 480, vcodec: "avc1.4d401e", }); } finally { await r.cleanup(); } });