import { test } from "node:test"; import assert from "node:assert/strict"; import { coerceLiveRecording, extractionRefused, ffmpegLiveArgs, liveFormatSelector, formatUnavailable, liveFromStartUnsupported, livePartFiles, mergeStreamsArgs, newSegments, separateStreamsDefault, parsePlaylist, pickVariant, playlistReach, ytdlpLiveArgs, type MediaPlaylist, } from "./liveCapture"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/liveCapture.test.ts // // The playlists below are the shapes measured on 2026-10-22 (plans/live-capture.md), // cut short. const RUMBLE_DVR = `#EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:2 #EXT-X-MEDIA-SEQUENCE:1 #EXT-X-DISCONTINUITY-SEQUENCE:0 #EXT-X-PROGRAM-DATE-TIME:2026-10-22T17:58:02.584Z #EXTINF:2, media_3m1qvn_360p_1.ts #EXTINF:2, media_3m1qvn_360p_2.ts #EXTINF:2, media_3m1qvn_360p_3.ts `; const RUMBLE_WINDOWED = `#EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:2 #EXT-X-MEDIA-SEQUENCE:5313 #EXT-X-PROGRAM-DATE-TIME:2026-10-22T14:17:06.315Z #EXTINF:2, media_nw0u1i_360p_5313.ts #EXTINF:2, media_nw0u1i_360p_5314.ts `; const KICK_VOD = `#EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:10 #EXT-X-PLAYLIST-TYPE:EVENT #EXT-X-MEDIA-SEQUENCE:0 #EXT-X-PROGRAM-DATE-TIME:2026-10-22T17:38:05.321Z #EXTINF:10.000, 0.ts #EXT-X-PROGRAM-DATE-TIME:2026-10-22T17:38:15.321Z #EXTINF:10.000, 1.ts #EXT-X-ENDLIST `; const MASTER = `#EXTM3U #EXT-X-VERSION:3 #EXT-X-STREAM-INF:BANDWIDTH=5212979,AVERAGE-BANDWIDTH=4739072,RESOLUTION=1920x1080 https://cdn.example/live/chunklist_i0_DVR.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=3581600,RESOLUTION=1280x720 https://cdn.example/live/chunklist_720p_i0_DVR.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=1460800,RESOLUTION=640x360 chunklist_360p_i0_DVR.m3u8 `; function media(text: string, base?: string): MediaPlaylist { const p = parsePlaylist(text, base); assert.equal(p?.kind, "media"); return p as MediaPlaylist; } test("a Rumble DVR playlist: sequence 1, resolved URIs, program date-times", () => { const p = media(RUMBLE_DVR, "https://cdn.example/live/chunklist_360p_i0_DVR.m3u8"); assert.equal(p.mediaSequence, 1); assert.equal(p.targetDuration, 2); assert.equal(p.playlistType, null); assert.equal(p.endList, false); assert.deepEqual( p.segments.map((s) => [s.seq, s.uri]), [ [1, "https://cdn.example/live/media_3m1qvn_360p_1.ts"], [2, "https://cdn.example/live/media_3m1qvn_360p_2.ts"], [3, "https://cdn.example/live/media_3m1qvn_360p_3.ts"], ], ); assert.equal(p.totalSeconds, 6); assert.equal(p.segments[1]!.programDateTimeMs, Date.parse("2026-10-22T17:58:04.584Z")); }); test("a Kick ongoing VOD: an EVENT playlist from 0 that ends every snapshot with ENDLIST", () => { const p = media(KICK_VOD, "https://stream.kick.com/x/360p30/playlist.m3u8"); assert.equal(p.playlistType, "EVENT"); assert.equal(p.endList, true); assert.equal(p.segments[0]!.seq, 0); assert.equal(p.segments[1]!.uri, "https://stream.kick.com/x/360p30/1.ts"); }); test("does the playlist reach the start: Rumble from 1, Kick from 0; a window past 4 h does not", () => { assert.equal(playlistReach(media(RUMBLE_DVR), "rumble").fromStart, true); assert.equal(playlistReach(media(KICK_VOD), "kick").fromStart, true); const late = playlistReach(media(RUMBLE_WINDOWED), "rumble", Date.parse("2026-10-22T11:19:42Z")); assert.equal(late.fromStart, false); assert.equal(late.firstSeq, 5313); assert.equal(late.missingSeconds, 10644); }); test("newSegments takes what is past the last sequence taken", () => { const p = media(RUMBLE_DVR); assert.equal(newSegments(p, null).length, 3); assert.deepEqual(newSegments(p, 2).map((s) => s.seq), [3]); assert.equal(newSegments(p, 3).length, 0); }); test("a master's variants, and the one ≤ maxHeight (else the shortest)", () => { const m = parsePlaylist(MASTER, "https://cdn.example/live/playlist.m3u8"); assert.equal(m?.kind, "master"); const variants = m!.kind === "master" ? m!.variants : []; assert.equal(variants.length, 3); assert.equal(variants[2]!.url, "https://cdn.example/live/chunklist_360p_i0_DVR.m3u8"); assert.equal(pickVariant(variants, 720)!.height, 720); assert.equal(pickVariant(variants, null)!.height, 1080); assert.equal(pickVariant(variants, 240)!.height, 360); assert.equal(parsePlaylist("not a playlist"), null); }); test("the yt-dlp argv: the channel's args first, the recording's after, MPEG-TS straight to the file", () => { const args = ytdlpLiveArgs({ url: "https://rumble.com/v1-x.html", output: "/store/s/.v1.live.ytdlp.ts", fromStart: true, maxHeight: 720, channelArgs: ["--impersonate", "firefox", "--no-live-from-start"], }); assert.ok(args.indexOf("--live-from-start") > args.indexOf("--no-live-from-start"), "after the channel's own"); for (const flag of ["--hls-use-mpegts", "--no-part", "--ignore-config", "--no-download-archive"]) assert.ok(args.includes(flag), flag); assert.deepEqual(args.slice(args.indexOf("--fixup"), args.indexOf("--fixup") + 2), ["--fixup", "never"]); assert.equal(args[args.indexOf("-f") + 1], "best[height<=720]/best"); assert.deepEqual(args.slice(-2), ["--", "https://rumble.com/v1-x.html"]); assert.ok(!args.includes("--wait-for-video")); const upcoming = ytdlpLiveArgs({ url: "u", output: "o", fromStart: false, maxHeight: null, channelArgs: [], waitForVideo: true }); assert.ok(upcoming.includes("--no-live-from-start")); assert.ok(upcoming.includes("--wait-for-video")); assert.equal(liveFormatSelector(null), "best"); }); test("a muxed recording's argv (Rumble, Kick) is exactly what it was: one `best`, MPEG-TS to the file", () => { const args = ytdlpLiveArgs({ url: "https://rumble.com/v1-x.html", output: "/store/s/.v1.live.ytdlp.ts", fromStart: true, maxHeight: 720, channelArgs: ["--impersonate", "chrome"], }); assert.deepEqual(args, [ "--ignore-config", "--no-playlist", "--impersonate", "chrome", "--live-from-start", "-f", "best[height<=720]/best", "--hls-use-mpegts", "--no-part", "--fixup", "never", "--no-write-info-json", "--no-write-subs", "--no-write-auto-subs", "--no-write-thumbnail", "--no-write-description", "--no-download-archive", "--no-mtime", "--newline", "-o", "/store/s/.v1.live.ytdlp.ts", "--", "https://rumble.com/v1-x.html", ]); assert.deepEqual(ytdlpLiveArgs({ url: "u", output: "o.ts", fromStart: true, maxHeight: 720, channelArgs: [], separate: false }), ytdlpLiveArgs({ url: "u", output: "o.ts", fromStart: true, maxHeight: 720, channelArgs: [] })); }); test("separate streams (YouTube): the video at the height plus the best audio, merged; the output a stem", () => { assert.equal(liveFormatSelector(720, true), "bv*[height<=720]+ba/b[height<=720]/bv*+ba/b"); assert.equal(liveFormatSelector(null, true), "bv*+ba/b"); assert.equal(liveFormatSelector(720, false), "best[height<=720]/best"); assert.equal(separateStreamsDefault("youtube"), true); for (const p of ["rumble", "kick", "twitch", "unknown", null]) assert.equal(separateStreamsDefault(p), false, String(p)); const args = ytdlpLiveArgs({ url: "https://www.youtube.com/watch?v=5-XlMxKT494", output: "/store/s/.5-XlMxKT494.live-T.ytdlp", fromStart: true, maxHeight: 720, channelArgs: [], separate: true, }); assert.equal(args[args.indexOf("-f") + 1], "bv*[height<=720]+ba/b[height<=720]/bv*+ba/b"); assert.equal(args[args.indexOf("--merge-output-format") + 1], "mp4/mkv"); assert.equal(args[args.indexOf("-o") + 1], "/store/s/.5-XlMxKT494.live-T.ytdlp.%(ext)s"); for (const flag of ["--live-from-start", "--no-part"]) assert.ok(args.includes(flag), flag); // DESIGN-4: the streams kept through the merge, every fragment kept, a // fragment that cannot be fetched ends the stream (retried with a back-off). for (const flag of ["-k", "--keep-fragments", "--abort-on-unavailable-fragments"]) assert.ok(args.includes(flag), flag); assert.equal(args[args.indexOf("--retry-sleep") + 1], "fragment:exp=1:20"); assert.ok(!args.includes("--skip-unavailable-fragments")); const last = ytdlpLiveArgs({ url: "u", output: "o", fromStart: true, maxHeight: 720, channelArgs: [], separate: true, allowSkip: true }); assert.ok(last.includes("--skip-unavailable-fragments") && !last.includes("--abort-on-unavailable-fragments"), "the last resort may skip"); const edge = ytdlpLiveArgs({ url: "u", output: "o", fromStart: false, maxHeight: 720, channelArgs: [], separate: true }); assert.ok(edge.includes("-k") && !edge.includes("--keep-fragments"), "the live edge keeps its streams through the merge, no fragments"); assert.ok(formatUnavailable("ERROR: [youtube] 5-XlMxKT494: Requested format is not available. Use --list-formats")); }); test("a part's files by stem: finished, unmerged streams, yt-dlp's scratch", () => { const id = "5-XlMxKT494"; const s1 = `.${id}.live-2026-10-26T12-00-00-000Z.ytdlp`; const s2 = `.${id}.live-2026-10-25T09-00-00-000Z.ytdlp-index0`; const groups = livePartFiles(id, [ `${s1}.f270.mp4`, `${s1}.f234.m4a`, `${s1}.f270.mp4-Frag12`, `${s1}.f234.m4a.ytdl`, `${s1}.temp.mp4`, `${s2}.ts`, `${s2}.merged.mp4`, `.${id}.land-1.part`, `.other.live-2026-10-26T12-00-00-000Z.ytdlp.ts`, "5-XlMxKT494", ]); assert.deepEqual(groups, [ { stem: s2, finished: [`${s2}.merged.mp4`, `${s2}.ts`], streams: [], scratch: [] }, { stem: s1, finished: [], streams: [`${s1}.f234.m4a`, `${s1}.f270.mp4`], scratch: [`${s1}.f234.m4a.ytdl`, `${s1}.f270.mp4-Frag12`, `${s1}.temp.mp4`], }, ]); // yt-dlp's DASH downloader names both streams with the merge's extension. assert.deepEqual(livePartFiles(id, [`${s1}.f270.mp4`, `${s1}.f234.mp4`, `${s1}.mp4`])[0], { stem: s1, finished: [`${s1}.mp4`], streams: [`${s1}.f234.mp4`, `${s1}.f270.mp4`], scratch: [], }); const m = mergeStreamsArgs({ video: "v.mp4", audio: "a.m4a", output: "o.mp4", mp4: true }); assert.deepEqual(m.slice(m.indexOf("-i")), ["-i", "v.mp4", "-i", "a.m4a", "-map", "0:v:0", "-map", "1:a:0", "-c", "copy", "-movflags", "+faststart", "-f", "mp4", "o.mp4"]); assert.deepEqual(mergeStreamsArgs({ video: "v", audio: "a", output: "o.mkv", mp4: false }).slice(-3), ["-f", "matroska", "o.mkv"]); }); test("the ffmpeg argv: from the first listed segment, stream copy, retries", () => { const args = ffmpegLiveArgs({ url: "https://cdn/x.m3u8", output: "/o.ts", fromStart: true, userAgent: "UA", referer: "https://rumble.com/" }); assert.equal(args[args.indexOf("-live_start_index") + 1], "0"); assert.ok(args.indexOf("-live_start_index") < args.indexOf("-i")); assert.deepEqual(args.slice(args.indexOf("-c"), args.indexOf("-c") + 2), ["-c", "copy"]); assert.equal(args[args.indexOf("-headers") + 1], "Referer: https://rumble.com/\r\n"); assert.equal(args.at(-1), "/o.ts"); assert.ok(!ffmpegLiveArgs({ url: "u", output: "o", fromStart: false }).includes("-live_start_index")); }); test("the ffmpeg argv: Rumble's .tar-packed segments are allowed, nothing looser", () => { const args = ffmpegLiveArgs({ url: "https://cdn/x.m3u8", output: "/o.ts", fromStart: true }); const i = args.indexOf("-allowed_segment_extensions"); assert.ok(i >= 0 && i < args.indexOf("-i")); assert.ok(args[i + 1].split(",").includes("tar")); assert.ok(args[i + 1].split(",").includes("ts")); assert.ok(!args.includes("-extension_picky")); }); test("yt-dlp's two answers a recording acts on", () => { assert.ok( liveFromStartUnsupported( "ERROR: [RumbleEmbed] v1: --live-from-start is passed, but there are no formats that can be downloaded from the start. If you want to download from the current time, use --no-live-from-start", ), ); assert.ok(extractionRefused("ERROR: [RumbleEmbed] v1: Unable to download JSON metadata: HTTP Error 403: Forbidden")); assert.ok(!extractionRefused("ERROR: This live event will begin in 3 hours")); }); test("live-recording.json reads back, and refuses what is not one", () => { const r = coerceLiveRecording({ status: "landed", url: "u", platform: "rumble", fromStart: true, startedAt: "2026-10-22T18:00:00Z", parts: [{ method: "ytdlp", url: "u", fromStart: true, startedAt: "x" }, { method: "bogus" }], }); assert.equal(r?.parts.length, 1); assert.equal(coerceLiveRecording({ status: "nope", url: "u", startedAt: "x" }), null); });