From 0abd4ee66b306f912443703c44f15b7d86d4a501 Mon Sep 17 00:00:00 2001 From: MajorLOL <56225967+MajorLOL@users.noreply.github.com> Date: Sat, 10 Oct 2026 15:13:39 +0200 Subject: [PATCH 01/14] Server: store recording quality per performer source - New sources.recording_max_height column (0 = automatic, highest available), added automatically to existing databases. - PATCH /api/sources/:id accepts recordingMaxHeight (0-4320). - The downloader passes the source's value to the plugin as recordingMaxHeight for that download only; the plugin's own settings are not changed. Automatic (0) keeps the plugin setting. - Recording quality is included in backup export and import. - Tests: server/recording-quality.test.ts (storage, migration, override and an end-to-end download check) and updated server/backup.test.ts. --- server/backup.test.ts | 6 +-- server/backup.ts | 4 +- server/database.ts | 9 +++- server/downloader.ts | 11 ++++- server/index.ts | 1 + server/recording-quality.test.ts | 78 ++++++++++++++++++++++++++++++++ 6 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 server/recording-quality.test.ts diff --git a/server/backup.test.ts b/server/backup.test.ts index 64dfa2b..2b5629d 100644 --- a/server/backup.test.ts +++ b/server/backup.test.ts @@ -29,7 +29,7 @@ async function filledLibrary() { lib.db.mergePerformerDetails(alice.id, { externalRefs: { "test.live": "alice" } }); lib.db.setPerformerPriority(alice.id, 1); const source = lib.db.addSource(alice.id, "test.live", { externalId: "https://live.test/alice", label: "live.test", profileUrl: "https://live.test/alice", domain: "live.test" }); - lib.db.updateSource(source.id, { autoDownload: true, scraperPluginId: "test.live", scrapeEnabled: true, syncIntervalSeconds: 30 }); + lib.db.updateSource(source.id, { autoDownload: true, scraperPluginId: "test.live", scrapeEnabled: true, syncIntervalSeconds: 30, recordingMaxHeight: 720 }); lib.db.createPerformer({ name: "Bob" }); return lib; } @@ -43,7 +43,7 @@ describe("backup export and import", () => { expect(backup.performers).toHaveLength(2); expect(backup.performers[0]).toMatchObject({ name: "Alice", aliases: ["Ally"], imageUrl: "https://img.test/alice.jpg", externalRefs: { "test.live": "alice" }, priority: 1, - sources: [{ pluginId: "test.live", profileUrl: "https://live.test/alice", autoDownload: true, scraperPluginId: "test.live", scrapeEnabled: true, syncIntervalSeconds: 30 }], + sources: [{ pluginId: "test.live", profileUrl: "https://live.test/alice", autoDownload: true, scraperPluginId: "test.live", scrapeEnabled: true, syncIntervalSeconds: 30, recordingMaxHeight: 720 }], }); expect(JSON.stringify(backup)).not.toContain("secret-value"); } finally { db.close(); } @@ -62,7 +62,7 @@ describe("backup export and import", () => { const alice = db.getPerformerByName("Alice")!; expect(alice).toMatchObject({ aliases: ["Ally"], imageUrl: "https://img.test/alice.jpg", externalRefs: { "test.live": "alice" }, priority: 1 }); expect(db.findPerformerByIdentity("test.live", "alice")?.id).toBe(alice.id); - expect(db.listSources(alice.id)).toMatchObject([{ profileUrl: "https://live.test/alice", autoDownload: true, scrapeEnabled: true, scraperPluginId: "test.live", syncIntervalSeconds: 30 }]); + expect(db.listSources(alice.id)).toMatchObject([{ profileUrl: "https://live.test/alice", autoDownload: true, scrapeEnabled: true, scraperPluginId: "test.live", syncIntervalSeconds: 30, recordingMaxHeight: 720 }]); } finally { db.close(); } }); diff --git a/server/backup.ts b/server/backup.ts index fe886b5..d6dff6c 100644 --- a/server/backup.ts +++ b/server/backup.ts @@ -24,6 +24,7 @@ const sourceSchema = z.object({ enabled: z.boolean().optional(), autoDownload: z.boolean().optional(), scraperPluginId: z.string().nullable().optional(), scrapeEnabled: z.boolean().optional(), syncIntervalSeconds: z.number().int().min(5).max(31_536_000).optional(), + recordingMaxHeight: z.number().int().min(0).max(4320).optional(), }); const performerSchema = z.object({ @@ -63,7 +64,7 @@ export function exportBackup(db: Database, plugins: PluginManager, appVersion?: sources: sources.filter((source) => source.performerId === performer.id).map((source) => ({ pluginId: source.pluginId, externalId: source.externalId, label: source.label, profileUrl: source.profileUrl, domain: source.domain, enabled: source.enabled, autoDownload: source.autoDownload, scraperPluginId: source.scraperPluginId ?? null, - scrapeEnabled: source.scrapeEnabled, syncIntervalSeconds: source.syncIntervalSeconds, + scrapeEnabled: source.scrapeEnabled, syncIntervalSeconds: source.syncIntervalSeconds, recordingMaxHeight: source.recordingMaxHeight, })), })), }; @@ -149,6 +150,7 @@ export function importBackup(db: Database, plugins: PluginManager, backup: Backu ...(source.enabled !== undefined ? { enabled: source.enabled } : {}), ...(source.autoDownload !== undefined ? { autoDownload: source.autoDownload } : {}), ...(source.scraperPluginId !== undefined ? { scraperPluginId: source.scraperPluginId } : {}), ...(scrapeEnabled !== undefined ? { scrapeEnabled } : {}), ...(interval !== undefined ? { syncIntervalSeconds: interval } : {}), + ...(source.recordingMaxHeight !== undefined ? { recordingMaxHeight: source.recordingMaxHeight } : {}), }); if (same) result.sources.updated += 1; else { result.sources.added += 1; existing.push(saved); } } catch (error) { diff --git a/server/database.ts b/server/database.ts index e4a2706..69a468a 100644 --- a/server/database.ts +++ b/server/database.ts @@ -23,6 +23,8 @@ export type Source = { profileUrl: string; domain: string; enabled: boolean; autoDownload: boolean; scraperPluginId?: string; scrapeEnabled: boolean; syncIntervalSeconds: number; lastSyncedAt?: string; nextSyncAt?: string; lastError?: string; + /** Maximum live recording height for this performer and plugin; 0 = automatic (highest available). */ + recordingMaxHeight: number; }; export type PerformerInput = { name: string; aliases?: string[]; imageUrl?: string | null }; @@ -147,6 +149,7 @@ export class Database { this.sqlite.exec("ALTER TABLE sources ADD COLUMN sync_interval_seconds INTEGER NOT NULL DEFAULT 21600"); this.sqlite.exec("UPDATE sources SET sync_interval_seconds=sync_interval_minutes*60"); } + if (!sourceColumns.has("recording_max_height")) this.sqlite.exec("ALTER TABLE sources ADD COLUMN recording_max_height INTEGER NOT NULL DEFAULT 0"); const itemColumns = new Set((this.sqlite.prepare("PRAGMA table_info(items)").all() as Array<{ name: string }>).map((column) => column.name)); if (!itemColumns.has("original_external_id")) this.sqlite.exec("ALTER TABLE items ADD COLUMN original_external_id TEXT"); if (!itemColumns.has("visual_hash")) this.sqlite.exec("ALTER TABLE items ADD COLUMN visual_hash TEXT"); @@ -486,7 +489,7 @@ export class Database { return row ? this.mapSource(row) : undefined; } - updateSource(sourceId: string, values: Partial> & { scraperPluginId?: string | null }) { + updateSource(sourceId: string, values: Partial> & { scraperPluginId?: string | null }) { const source = this.getSource(sourceId); if (!source) return undefined; // Refreshing an already stored account must not turn a legacy conflict into // a runtime failure. New associations still require an explicit merge. @@ -500,6 +503,7 @@ export class Database { (values.scrapeEnabled ?? source.scrapeEnabled) ? 1 : 0, values.syncIntervalSeconds ?? source.syncIntervalSeconds, Math.max(1, Math.round((values.syncIntervalSeconds ?? source.syncIntervalSeconds) / 60)), now(), sourceId); if (values.profileUrl !== undefined) this.sqlite.prepare("UPDATE sources SET profile_key=? WHERE id=?").run(profileIdentity(values.profileUrl) ?? null, sourceId); + if (values.recordingMaxHeight !== undefined) this.sqlite.prepare("UPDATE sources SET recording_max_height=? WHERE id=?").run(Math.max(0, Math.trunc(values.recordingMaxHeight)), sourceId); return this.getSource(sourceId); } @@ -525,7 +529,8 @@ export class Database { return { id: row.id, performerId: row.performer_id, pluginId: row.plugin_id, externalId: row.external_id, label: row.label, profileUrl: row.profile_url, domain: row.domain, enabled: !!row.enabled, autoDownload: !!row.auto_download, scraperPluginId: row.scraper_plugin_id ?? undefined, scrapeEnabled: !!row.scrape_enabled, - syncIntervalSeconds: row.sync_interval_seconds ?? row.sync_interval_minutes * 60, lastSyncedAt: row.last_synced_at ?? undefined, nextSyncAt: row.next_sync_at ?? undefined, lastError: row.last_error ?? undefined }; + syncIntervalSeconds: row.sync_interval_seconds ?? row.sync_interval_minutes * 60, lastSyncedAt: row.last_synced_at ?? undefined, nextSyncAt: row.next_sync_at ?? undefined, lastError: row.last_error ?? undefined, + recordingMaxHeight: Number(row.recording_max_height ?? 0) || 0 }; } /** Explicitly reconsider rediscovered missing media; ordinary scans keep deletion history. */ diff --git a/server/downloader.ts b/server/downloader.ts index f0833f1..eb2a628 100644 --- a/server/downloader.ts +++ b/server/downloader.ts @@ -13,6 +13,15 @@ import { filenameFromUrl, safeSegment } from "./utils.js"; import { downloadOutputPath, recordingEncodingArgs } from "./output-settings.js"; import { outputSettings } from "../packages/output-settings.js"; import { canMatchVideoExcerpt, fingerprintVideo, verifyVideoContainment, videoFileStamp, type VideoFingerprint } from "./video-matching.js"; +import type { Source } from "./database.js"; + +/** + * Plugin config for one download, with the source's recording quality applied. A + * source set to automatic (0) keeps the plugin's own setting (normally highest). + */ +export function recordingConfig(config: Record, source: Pick): Record { + return source.recordingMaxHeight > 0 ? { ...config, recordingMaxHeight: source.recordingMaxHeight } : config; +} type ActiveDownload = { /** Live recording of a performer with this priority; preempted when a higher one has to wait for a slot. */ @@ -166,7 +175,7 @@ export class DownloadQueue { if (!performer || !source) throw new Error("The performer or source no longer exists"); const settings = outputSettings(this.db.getSettings()); const resolutionController = new AbortController(); control.abort = resolutionController; - const request = await plugin.resolveDownload(this.plugins.context(item.pluginId, resolutionController.signal), { + const request = await plugin.resolveDownload(this.plugins.context(item.pluginId, resolutionController.signal, recordingConfig(this.db.getPluginState(item.pluginId).config, source)), { externalId: item.externalId, identityKey: item.identityKey, title: item.title, pageUrl: item.pageUrl, mediaType: item.mediaType as any, filename: item.filename, qualityScore: item.qualityScore, expectedBytes: item.expectedBytes, publishedAt: item.publishedAt, metadata: item.metadata, diff --git a/server/index.ts b/server/index.ts index 983506e..a3db819 100644 --- a/server/index.ts +++ b/server/index.ts @@ -541,6 +541,7 @@ app.patch<{ Params: { id: string }; Body: unknown }>("/api/sources/:id", async ( scraperPluginId: z.union([z.string().min(1), z.null()]).optional(), scrapeEnabled: z.boolean().optional(), pluginId: z.string().min(1).optional(), label: z.string().trim().min(1).max(160).optional(), profileUrl: z.string().url().optional(), enabled: z.boolean().optional(), autoDownload: z.boolean().optional(), syncIntervalSeconds: z.number().int().min(5).max(31_536_000).optional(), syncIntervalMinutes: z.number().int().min(1).max(525600).optional(), + recordingMaxHeight: z.number().int().min(0).max(4320).optional(), }).parse(request.body); const current = db.getSource(request.params.id); if (!current) throw Object.assign(new Error("Source not found"), { statusCode: 404 }); diff --git a/server/recording-quality.test.ts b/server/recording-quality.test.ts new file mode 100644 index 0000000..e10550d --- /dev/null +++ b/server/recording-quality.test.ts @@ -0,0 +1,78 @@ +import { afterEach, describe, expect, it } from "vitest"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { DatabaseSync } from "node:sqlite"; +import { Database } from "./database.js"; +import { PluginManager } from "./plugin-manager.js"; +import { DownloadQueue, recordingConfig } from "./downloader.js"; +import { LiveCamService } from "./live-cams.js"; + +const dirs: string[] = []; +afterEach(() => { for (const dir of dirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true }); }); +const temp = (name: string) => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), `${name}-`)); dirs.push(dir); return dir; }; +async function waitFor(check: () => boolean) { + const deadline = Date.now() + 5000; + while (!check()) { if (Date.now() > deadline) throw new Error("Timed out"); await new Promise((resolve) => setTimeout(resolve, 25)); } +} + +describe("per-source recording quality", () => { + it("defaults to automatic and stores a maximum height per source", () => { + const db = new Database(temp("easyx-quality-db")); + try { + const alice = db.createPerformer({ name: "Alice" }); + const one = db.addSource(alice.id, "test.a", { externalId: "a", label: "a.test", profileUrl: "https://a.test/alice", domain: "a.test" }); + const two = db.addSource(alice.id, "test.b", { externalId: "b", label: "b.test", profileUrl: "https://b.test/alice", domain: "b.test" }); + expect(one.recordingMaxHeight).toBe(0); + expect(db.updateSource(one.id, { recordingMaxHeight: 720 })?.recordingMaxHeight).toBe(720); + expect(db.updateSource(one.id, { autoDownload: true })?.recordingMaxHeight).toBe(720); + expect(db.getSource(two.id)?.recordingMaxHeight).toBe(0); + expect(db.updateSource(one.id, { recordingMaxHeight: 0 })?.recordingMaxHeight).toBe(0); + } finally { db.close(); } + }); + + it("adds the column to an existing database", () => { + const dir = temp("easyx-quality-migrate"); + new Database(dir).close(); + const raw = new DatabaseSync(path.join(dir, fs.readdirSync(dir).find((name) => name.endsWith(".sqlite"))!)); + raw.exec("ALTER TABLE sources DROP COLUMN recording_max_height"); raw.close(); + const db = new Database(dir); + try { + const alice = db.createPerformer({ name: "Alice" }); + expect(db.addSource(alice.id, "test.a", { externalId: "a", label: "a", profileUrl: "https://a.test/alice", domain: "a.test" }).recordingMaxHeight).toBe(0); + } finally { db.close(); } + }); + + it("only overrides the plugin setting when a maximum is chosen", () => { + const config = { cookiesFile: "/x", recordingMaxHeight: 1080 }; + expect(recordingConfig(config, { recordingMaxHeight: 0 })).toBe(config); + expect(recordingConfig(config, { recordingMaxHeight: 480 })).toEqual({ cookiesFile: "/x", recordingMaxHeight: 480 }); + expect(config.recordingMaxHeight).toBe(1080); + }); + + it("passes the source's quality to the plugin that records it", async () => { + const dataDir = temp("easyx-quality-data"); const mediaDir = temp("easyx-quality-media"); const pluginDir = temp("easyx-quality-plugins"); + const packageDir = path.join(pluginDir, "test"); fs.mkdirSync(packageDir); + const seen = path.join(pluginDir, "seen.json"); + fs.writeFileSync(path.join(packageDir, "index.mjs"), `import fs from "node:fs"; + export default { + manifest: { id: "test.quality", name: "Quality", version: "1", description: "Test", author: "Test", capabilities: ["live-cam", "download-resolver"], sourceUrlPatterns: ["https://live.test/*"], + settings: [{ key: "cookiesFile", label: "Session", type: "text" }] }, + resolveLiveStream: async () => ({ url: "https://cdn.test/live.m3u8" }), + resolveDownload: async (context) => { + fs.writeFileSync(${JSON.stringify(seen)}, JSON.stringify(context.config)); + return { kind: "command", command: process.execPath, args: ["-e", "require('node:fs').writeFileSync(process.argv[1], 'video')", "{output}"], filename: "recording.mp4" }; + } + };`); + const db = new Database(dataDir); const manager = new PluginManager(db, [pluginDir]); await manager.load(); manager.install("test.quality", { cookiesFile: "/session.txt" }); + const { itemId } = new LiveCamService(db, manager).record("test.quality", { id: "alice", username: "alice", pageUrl: "https://live.test/alice" }); + db.updateSource(db.getItem(itemId)!.sourceId, { recordingMaxHeight: 480 }); + const queue = new DownloadQueue(db, manager, mediaDir); queue.start(); + try { + await waitFor(() => fs.existsSync(seen)); + expect(JSON.parse(fs.readFileSync(seen, "utf8"))).toEqual({ cookiesFile: "/session.txt", recordingMaxHeight: 480 }); + expect(db.getPluginState("test.quality").config).toEqual({ cookiesFile: "/session.txt" }); + await waitFor(() => ["completed", "failed"].includes(db.getItem(itemId)?.status ?? "")); + } finally { queue.stop(); db.close(); } + }); +}); From 880097be9fa284d94cbc716a48ed885e7e48e759 Mon Sep 17 00:00:00 2001 From: MajorLOL <56225967+MajorLOL@users.noreply.github.com> Date: Sat, 10 Oct 2026 15:14:21 +0200 Subject: [PATCH 02/14] UI: recording quality selector in Manage profile - Each profile URL with a live plugin (Stripchat, CAM4, BongaCams, Chaturbate, Twitch, CamSoda, etc.) gets a "Recording quality" selector in Manage profile. - Choices: Automatic (highest), or up to 2160p, 1440p, 1080p, 720p, 480p, 360p or 240p. The setting is saved per performer and per site. - The selector spans the full width of the source controls, so it also fits on small screens. --- src/main.tsx | 9 +++++++-- src/scraper.css | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main.tsx b/src/main.tsx index 9278d3d..9744327 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -31,7 +31,7 @@ type Performer = { id: string; name: string; aliases: string[]; imageUrl?: strin const priorityKey = (value?: number) => value && value > 0 ? "high" : value && value < 0 ? "low" : "normal"; const priorityLabels = { high: "High", normal: "Normal", low: "Low" } as const; type LocalPerformerImage = { id: string; title: string; source: string; modifiedAt: string; thumbnailUrl: string }; -type Source = { id: string; performerId: string; pluginId: string; label: string; profileUrl: string; domain: string; enabled: boolean; autoDownload: boolean; scraperPluginId?: string; scrapeEnabled: boolean; syncIntervalSeconds: number; lastSyncedAt?: string; lastError?: string }; +type Source = { id: string; performerId: string; pluginId: string; label: string; profileUrl: string; domain: string; enabled: boolean; autoDownload: boolean; scraperPluginId?: string; scrapeEnabled: boolean; syncIntervalSeconds: number; recordingMaxHeight?: number; lastSyncedAt?: string; lastError?: string }; type Item = { id: string; performerId: string; sourceId: string; pluginId: string; title?: string; mediaType: string; status: string; progress: number; downloadedBytes: number; qualityScore: number; expectedBytes?: number; storagePath?: string; outputPath?: string; duplicateReason?: string; error?: string; downloadStartedAt?: string; downloadFinishedAt?: string; updatedAt: string }; type ActivityData = { items: Item[]; page: number; pageSize: number; total: number; totalPages: number; statusCounts: Record; mediaTypes: string[] }; type Dashboard = { stats: { performers: number; sources: number; available: number; queued: number; completed: number; bytes: number }; performers: Performer[]; sources: Source[]; items: Item[] }; @@ -77,6 +77,9 @@ function PluginLogo({ plugin }: { plugin: Plugin }) { } const scheduleChoices = [5, 10, 30, 60, 300, 900, 1800, 3600, 21600, 43200, 86400, 604800]; +const recordingQualities = [0, 2160, 1440, 1080, 720, 480, 360, 240]; +function recordingQualityLabel(height: number) { return height > 0 ? `Up to ${height}p` : "Automatic (highest)"; } + function intervalLabel(seconds: number): string { if (seconds < 60) return `${seconds} seconds`; if (seconds < 3600) { const minutes = seconds / 60; return `${minutes} minute${minutes === 1 ? "" : "s"}`; } @@ -338,7 +341,9 @@ function PerformerManager({ performer, sources, items, plugins, close, run }: { const scraper = compatibleScrapers.find((candidate) => candidate.manifest.id === source.scraperPluginId); const minimum = scraper?.manifest.polling?.minimumIntervalSeconds ?? 300; const intervals = [...new Set([...scheduleChoices, source.syncIntervalSeconds])].filter((seconds) => seconds >= minimum).sort((a, b) => a - b); - return
{source.domain.slice(0, 2).toUpperCase()}
{source.label}{source.profileUrl}Found by {pluginName(source.pluginId)} · {compatibleScrapers.length} compatible scraper{compatibleScrapers.length === 1 ? "" : "s"} · never assigned automatically
{scraper && }
{scraper ? : Reference}{scraper && }{scraper && }
; + const recordsLive = Boolean(scraper && (scraper.manifest.polling?.mode === "live" || scraper.manifest.capabilities.includes("live-cam"))); + const quality = source.recordingMaxHeight ?? 0; + return
{source.domain.slice(0, 2).toUpperCase()}
{source.label}{source.profileUrl}Found by {pluginName(source.pluginId)} · {compatibleScrapers.length} compatible scraper{compatibleScrapers.length === 1 ? "" : "s"} · never assigned automatically
{scraper && }{recordsLive && }
{scraper ? : Reference}{scraper && }{scraper && }
; })} :

No URLs yet. Add a profile, choose a compatible scraper, then enable its automatic schedule.

}
Delete performerChoose whether associated media should remain on disk.
; diff --git a/src/scraper.css b/src/scraper.css index 2899658..fad8955 100644 --- a/src/scraper.css +++ b/src/scraper.css @@ -49,3 +49,5 @@ .profile-url-row > .badge, .profile-url-row > span.badge { display: none; } .profile-url-row > .icon-button[aria-label^="Scrape"] { display: none; } } + +.source-scraper-controls .source-quality { grid-column: 1 / -1; } From a4b9722e03a2f56d0ff4749ebaf4236f36e11c56 Mon Sep 17 00:00:00 2001 From: MajorLOL <56225967+MajorLOL@users.noreply.github.com> Date: Sat, 10 Oct 2026 15:15:42 +0200 Subject: [PATCH 03/14] Plugins: honour a per-source maximum recording quality - New shared plugins/hls-quality.ts parses HLS variants (RESOLUTION, NAME or playlist name) and orders them for a maximum height: the best at or below it first, then the closest larger one, so a missing quality never fails. - Stripchat, CAM4 and BongaCams pick their live variant with it; automatic keeps the current behaviour (highest first). - yt-dlp based live plugins (Chaturbate, Twitch and the live-cam factory used by CamSoda, MyFreeCams, LiveJasmin, XCams and Cams) pass the height to yt-dlp with a fallback and --format-sort res:N. - Tests for variant parsing and ordering, and for the selected variant in Stripchat, CAM4, BongaCams, Chaturbate and yt-dlp recordings. --- plugins/hls-quality.test.ts | 49 +++++++++++++++++++++++++ plugins/hls-quality.ts | 59 ++++++++++++++++++++++++++++++ plugins/live-cam-plugin-factory.ts | 3 +- plugins/yt-dlp-utils.test.ts | 10 ++++- plugins/yt-dlp-utils.ts | 8 +++- 5 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 plugins/hls-quality.test.ts create mode 100644 plugins/hls-quality.ts diff --git a/plugins/hls-quality.test.ts b/plugins/hls-quality.test.ts new file mode 100644 index 0000000..ef91799 --- /dev/null +++ b/plugins/hls-quality.test.ts @@ -0,0 +1,49 @@ +import { describe, expect, it } from "vitest"; +import { orderVariantsForRecording, parseHlsVariants, recordingMaxHeight, ytDlpHeightSort } from "./hls-quality"; + +const master = [ + "#EXTM3U", + '#EXT-X-STREAM-INF:BANDWIDTH=6000000,RESOLUTION=1920x1080,NAME="1080p"', "1080/index.m3u8", + '#EXT-X-STREAM-INF:BANDWIDTH=3000000,RESOLUTION=1280x720,NAME="720p"', "720/index.m3u8", + '#EXT-X-STREAM-INF:BANDWIDTH=1200000,RESOLUTION=854x480,NAME="480p"', "480/index.m3u8", + '#EXT-X-STREAM-INF:BANDWIDTH=500000,RESOLUTION=640x360', "360/index.m3u8", +].join("\n"); +const heights = (list: Array<{ height: number }>) => list.map((variant) => variant.height); + +describe("HLS recording quality", () => { + it("reads height from RESOLUTION, NAME or the playlist name", () => { + expect(heights(parseHlsVariants(master, "https://cdn.test/live/master.m3u8"))).toEqual([1080, 720, 480, 360]); + expect(parseHlsVariants(master, "https://cdn.test/live/master.m3u8")[1].url).toBe("https://cdn.test/live/720/index.m3u8"); + expect(heights(parseHlsVariants('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1,NAME="540p"\na.m3u8\n#EXT-X-STREAM-INF:BANDWIDTH=2\nroom_240p.m3u8', "https://cdn.test/m.m3u8"))).toEqual([540, 240]); + expect(parseHlsVariants("not a playlist", "https://cdn.test/m.m3u8")).toEqual([]); + }); + + it("keeps the highest quality first when set to automatic", () => { + expect(heights(orderVariantsForRecording(parseHlsVariants(master, "https://cdn.test/m.m3u8")))).toEqual([1080, 720, 480, 360]); + }); + + it("prefers the best variant at or below the maximum height", () => { + const variants = parseHlsVariants(master, "https://cdn.test/m.m3u8"); + expect(heights(orderVariantsForRecording(variants, 720))).toEqual([720, 480, 360, 1080]); + expect(heights(orderVariantsForRecording(variants, 600))).toEqual([480, 360, 720, 1080]); + }); + + it("falls back to the closest larger quality instead of failing", () => { + const variants = parseHlsVariants(master, "https://cdn.test/m.m3u8"); + expect(heights(orderVariantsForRecording(variants, 240))).toEqual([360, 480, 720, 1080]); + }); + + it("uses bandwidth when the manifest has no quality information", () => { + const variants = [{ url: "a", bandwidth: 1, height: 0 }, { url: "b", bandwidth: 9, height: 0 }]; + expect(orderVariantsForRecording(variants, 720).map((variant) => variant.url)).toEqual(["b", "a"]); + }); + + it("treats missing or invalid configuration as automatic", () => { + expect(recordingMaxHeight(undefined)).toBe(0); + expect(recordingMaxHeight({ recordingMaxHeight: "720" })).toBe(720); + expect(recordingMaxHeight({ recordingMaxHeight: -1 })).toBe(0); + expect(recordingMaxHeight({ recordingMaxHeight: 720.5 })).toBe(0); + expect(ytDlpHeightSort(0)).toBeUndefined(); + expect(ytDlpHeightSort(480)).toBe("res:480"); + }); +}); diff --git a/plugins/hls-quality.ts b/plugins/hls-quality.ts new file mode 100644 index 0000000..cb493a4 --- /dev/null +++ b/plugins/hls-quality.ts @@ -0,0 +1,59 @@ +// Shared HLS quality selection for live recordings. +// +// A performer source can carry a maximum recording height (for example 720). The +// downloader passes it to the plugin as `context.config.recordingMaxHeight`; 0 or a +// missing value means "automatic": the highest quality the room offers. + +export type HlsVariant = { url: string; bandwidth: number; height: number }; + +/** Allowed per-source choices; 0 = automatic (highest available). */ +export const RECORDING_HEIGHTS = [0, 2160, 1440, 1080, 720, 480, 360, 240] as const; + +/** Maximum recording height from plugin config; 0 when unset or invalid (= highest). */ +export function recordingMaxHeight(config: Record | undefined): number { + const value = Number(config?.recordingMaxHeight ?? 0); + return Number.isInteger(value) && value > 0 ? value : 0; +} + +function variantHeight(info: string, url: string): number { + const resolution = /RESOLUTION=\d+x(\d+)/i.exec(info)?.[1]; + if (resolution) return Number(resolution); + const named = /NAME="?(\d{3,4})p/i.exec(info)?.[1] ?? /[_/-](\d{3,4})p(?:\d+)?(?:[._/-]|$)/i.exec(new URL(url).pathname)?.[1]; + return named ? Number(named) : 0; +} + +/** Variant playlists of an HLS master manifest, in manifest order. */ +export function parseHlsVariants(manifest: string, baseUrl: string): HlsVariant[] { + if (!manifest.trimStart().startsWith("#EXTM3U")) return []; + const variants: HlsVariant[] = []; let info = ""; + for (const raw of manifest.split(/\r?\n/)) { + const line = raw.trim(); + if (line.startsWith("#EXT-X-STREAM-INF")) { info = line; continue; } + if (!line || line.startsWith("#") || !/\.m3u8(?:$|\?)/i.test(line)) continue; + let url: string; + try { url = new URL(line, baseUrl).toString(); } catch { info = ""; continue; } + variants.push({ url, bandwidth: Number(/(?:^|[:,])BANDWIDTH=(\d+)/i.exec(info)?.[1] ?? 0), height: variantHeight(info, url) }); + info = ""; + } + return variants; +} + +/** + * Order variants by preference for a recording. Automatic (0): highest quality first. + * With a maximum height: the best variant at or below it first, then — when the room + * offers nothing that small — the closest larger variant, so a recording never fails + * only because the preferred quality is missing. + */ +export function orderVariantsForRecording(variants: T[], maxHeight = 0): T[] { + const byQuality = (left: T, right: T) => right.height - left.height || right.bandwidth - left.bandwidth; + if (!maxHeight || !variants.some((variant) => variant.height > 0)) return [...variants].sort((left, right) => right.bandwidth - left.bandwidth || right.height - left.height); + const fitting = variants.filter((variant) => variant.height > 0 && variant.height <= maxHeight).sort(byQuality); + const larger = variants.filter((variant) => variant.height > maxHeight).sort((left, right) => left.height - right.height || right.bandwidth - left.bandwidth); + const unknown = variants.filter((variant) => variant.height <= 0).sort((left, right) => right.bandwidth - left.bandwidth); + return [...fitting, ...larger, ...unknown]; +} + +/** yt-dlp `--format-sort` value for a maximum height (prefers ≤ height, else the closest larger). */ +export function ytDlpHeightSort(maxHeight: number): string | undefined { + return maxHeight > 0 ? `res:${maxHeight}` : undefined; +} diff --git a/plugins/live-cam-plugin-factory.ts b/plugins/live-cam-plugin-factory.ts index 6165b9e..982b41b 100644 --- a/plugins/live-cam-plugin-factory.ts +++ b/plugins/live-cam-plugin-factory.ts @@ -1,4 +1,5 @@ import { createHash } from "node:crypto"; +import { recordingMaxHeight } from "./hls-quality.js"; import { definePlugin, type EasyXPlugin, type MediaCandidate, type PluginManifest } from "../packages/plugin-sdk/index.js"; import { browserCapturedLiveStream } from "./browser-html-utils.js"; import { listDiscoveredLiveCams, type LiveCamDiscoveryProvider } from "./live-cam-discovery.js"; @@ -126,6 +127,6 @@ export function createLiveCamPlugin(options: LiveCamPluginOptions): EasyXPlugin } }, ...(options.discovery ? { async listLiveCams(context, query) { return listDiscoveredLiveCams(context, options.discovery!, query); } } : {}), - async resolveDownload(context, item) { return ytDlpDownload(item, context.config, { referer, live: true }); }, + async resolveDownload(context, item) { return ytDlpDownload(item, context.config, { referer, live: true, maxHeight: recordingMaxHeight(context.config) }); }, }); } diff --git a/plugins/yt-dlp-utils.test.ts b/plugins/yt-dlp-utils.test.ts index a1985f4..72ea942 100644 --- a/plugins/yt-dlp-utils.test.ts +++ b/plugins/yt-dlp-utils.test.ts @@ -26,9 +26,16 @@ describe("live stream selection", () => { }); describe("live recording output", () => { + it("records the highest quality without a format sort when set to automatic", () => { + const request = ytDlpDownload({ externalId: "live:alice", mediaType: "video", pageUrl: "https://live.test/alice" }, {}, { live: true, maxHeight: 0 }); + expect(request.args[request.args.indexOf("--format") + 1]).toBe("bestvideo+bestaudio/best"); + expect(request.args).not.toContain("--format-sort"); + }); + it("caps the native video resolution while retaining separate audio and captures both together", () => { const request = ytDlpDownload({ externalId: "live:alice", mediaType: "video", pageUrl: "https://live.test/alice" }, {}, { live: true, maxHeight: 720 }); - expect(request.args[request.args.indexOf("--format") + 1]).toBe("bestvideo[height<=720]+bestaudio/best[height<=720]"); + expect(request.args[request.args.indexOf("--format") + 1]).toBe("bestvideo[height<=720]+bestaudio/best[height<=720]/bestvideo+bestaudio/best"); + expect(request.args[request.args.indexOf("--format-sort") + 1]).toBe("res:720"); expect(request.args).toEqual(expect.arrayContaining(["--downloader", "ffmpeg"])); expect(request.args).not.toContain("--recode-video"); }); @@ -36,6 +43,7 @@ describe("live recording output", () => { it("leaves ordinary downloads unchanged when given live recording options", () => { const request = ytDlpDownload({ externalId: "video", mediaType: "video", pageUrl: "https://video.test/watch" }, {}, { maxHeight: 720 }); expect(request.args.join(" ")).not.toContain("height<="); + expect(request.args).not.toContain("--format-sort"); expect(request.args).not.toContain("--downloader"); expect(request.args).not.toContain("--force-ipv4"); }); diff --git a/plugins/yt-dlp-utils.ts b/plugins/yt-dlp-utils.ts index 74a5a2b..1c8143a 100644 --- a/plugins/yt-dlp-utils.ts +++ b/plugins/yt-dlp-utils.ts @@ -1,4 +1,5 @@ import { oldestMediaDate as mediaDate } from "../packages/media-date.js"; +import { ytDlpHeightSort } from "./hls-quality.js"; import type { CommandDownloadRequest, LiveCam, LiveStream, MediaCandidate, PluginContext } from "../packages/plugin-sdk/index.js"; type YtDlpEntry = Record & { entries?: YtDlpEntry[] }; @@ -121,8 +122,12 @@ export function ytDlpDownload(item: MediaCandidate, config: Record 0 ? `[height<=${options.maxHeight}]` : ""; + // With a maximum height, the best format at or below it wins; when the room offers + // nothing that small, the fallback (sorted by --format-sort res:N) takes the closest + // larger quality, so a missing quality never fails the recording. + const heightSort = height ? ytDlpHeightSort(options.maxHeight!) : undefined; const format = options.live - ? `bestvideo${height}+bestaudio/best${height}` + ? `bestvideo${height}+bestaudio/best${height}${height ? "/bestvideo+bestaudio/best" : ""}` : "bestvideo*[vcodec!=none]+bestaudio[acodec!=none]/best[acodec!=none]/best"; const args = [ "--progress", "--newline", "--progress-delta", "0.5", "--progress-template", "download:easyx-progress:%(progress._percent_str)s", "--no-playlist", "--retries", "5", "--fragment-retries", "5", @@ -131,6 +136,7 @@ export function ytDlpDownload(item: MediaCandidate, config: Record Date: Sat, 10 Oct 2026 15:20:04 +0200 Subject: [PATCH 04/14] Plugins: honour a per-source maximum recording quality - New shared plugins/hls-quality.ts parses HLS variants (RESOLUTION, NAME or playlist name) and orders them for a maximum height: the best at or below it first, then the closest larger one, so a missing quality never fails. - Stripchat, CAM4 and BongaCams pick their live variant with it; automatic keeps the current behaviour (highest first). - yt-dlp based live plugins (Chaturbate, Twitch and the live-cam factory used by CamSoda, MyFreeCams, LiveJasmin, XCams and Cams) pass the height to yt-dlp with a fallback and --format-sort res:N. - Tests for variant parsing and ordering, and for the selected variant in Stripchat, CAM4, BongaCams, Chaturbate and yt-dlp recordings. --- plugins/bongacams/index.test.ts | 5 +++++ plugins/bongacams/index.ts | 23 +++++++++-------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/plugins/bongacams/index.test.ts b/plugins/bongacams/index.test.ts index 3480de0..7c7344c 100644 --- a/plugins/bongacams/index.test.ts +++ b/plugins/bongacams/index.test.ts @@ -23,6 +23,11 @@ describe("BongaCams room recording", () => { expect(await resolveBongacamsDownload(ctx, item)).toMatchObject({ command: process.execPath, args: expect.arrayContaining(["https://video.test/hls/stream_Alice/high.m3u8", "{output}"]) }); expect(variantUrls(master, "https://video.test/master.m3u8")[0].bandwidth).toBe(9000); }); + it("records the performer's chosen maximum quality", async () => { + const ctx = { ...context(), config: { recordingMaxHeight: 720 } }; const [item] = await listBongacamsMedia(ctx, source); + expect(await resolveBongacamsDownload(ctx, item)).toMatchObject({ args: expect.arrayContaining(["https://video.test/hls/stream_Alice/low.m3u8"]) }); + expect(variantUrls(master, "https://video.test/master.m3u8", 720).map((variant) => variant.height)).toEqual([240, 1080]); + }); it("supports a media playlist but rejects a finished recording", async () => { expect(await bongacamsLiveVariant(context("public", live), "https://video.test/playlist.m3u8")).toBe("https://video.test/playlist.m3u8"); expect(await bongacamsLiveVariant(context("public", live + "#EXT-X-ENDLIST\n"), "https://video.test/playlist.m3u8")).toBeUndefined(); diff --git a/plugins/bongacams/index.ts b/plugins/bongacams/index.ts index c3c29c4..68d4b46 100644 --- a/plugins/bongacams/index.ts +++ b/plugins/bongacams/index.ts @@ -1,4 +1,5 @@ import { LIVE_RECORDER_SCRIPT, retryDelay } from "../live-recorder.js"; +import { orderVariantsForRecording, parseHlsVariants, recordingMaxHeight } from "../hls-quality.js"; import { createLiveCamPlugin } from "../live-cam-plugin-factory.js"; import type { CommandDownloadRequest, MediaCandidate, MediaSource, PluginContext } from "../../packages/plugin-sdk/index.js"; @@ -121,31 +122,25 @@ async function fetchText(context: PluginContext, url: string): Promise right.bandwidth - left.bandwidth); + if (!variants.length && /#EXTINF/i.test(manifest)) return [{ url: baseUrl, bandwidth: 0, height: 0 }]; + return orderVariantsForRecording(variants, maxHeight); } function isLivePlaylist(manifest: string) { return manifest.trimStart().startsWith("#EXTM3U") && /#EXTINF/i.test(manifest) && !/#EXT-X-ENDLIST/i.test(manifest); } -/** Best (highest bandwidth) variant that is actually serving segments. */ +/** Preferred variant (highest, or the best at or below the source's maximum height) that is actually serving segments. */ export async function bongacamsLiveVariant(context: PluginContext, masterUrl: string): Promise { const master = await fetchText(context, masterUrl); if (!master) return undefined; - for (const variant of variantUrls(master, masterUrl)) { + for (const variant of variantUrls(master, masterUrl, recordingMaxHeight(context.config))) { if (variant.url === masterUrl) return isLivePlaylist(master) ? masterUrl : undefined; const playlist = await fetchText(context, variant.url); if (playlist && isLivePlaylist(playlist)) return variant.url; From 78b4660c61bec50cd6cf90eb21c85e6a139e93a3 Mon Sep 17 00:00:00 2001 From: MajorLOL <56225967+MajorLOL@users.noreply.github.com> Date: Sat, 10 Oct 2026 15:20:36 +0200 Subject: [PATCH 05/14] Plugins: honour a per-source maximum recording quality - New shared plugins/hls-quality.ts parses HLS variants (RESOLUTION, NAME or playlist name) and orders them for a maximum height: the best at or below it first, then the closest larger one, so a missing quality never fails. - Stripchat, CAM4 and BongaCams pick their live variant with it; automatic keeps the current behaviour (highest first). - yt-dlp based live plugins (Chaturbate, Twitch and the live-cam factory used by CamSoda, MyFreeCams, LiveJasmin, XCams and Cams) pass the height to yt-dlp with a fallback and --format-sort res:N. - Tests for variant parsing and ordering, and for the selected variant in Stripchat, CAM4, BongaCams, Chaturbate and yt-dlp recordings. --- plugins/cam4/index.test.ts | 11 ++++++++++- plugins/cam4/index.ts | 17 +++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/plugins/cam4/index.test.ts b/plugins/cam4/index.test.ts index 32352cb..9196c8c 100644 --- a/plugins/cam4/index.test.ts +++ b/plugins/cam4/index.test.ts @@ -3,7 +3,7 @@ import os from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import type { PluginContext } from "../../packages/plugin-sdk/index.js"; -import { cam4FollowedSnapshot, setCam4Favorite } from "./index.js"; +import { cam4FollowedSnapshot, cam4VariantUrls, setCam4Favorite } from "./index.js"; const temporaryDirectories: string[] = []; @@ -75,3 +75,12 @@ describe("CAM4 account favorites", () => { expect(fetch).toHaveBeenCalledTimes(2); }); }); + +describe("CAM4 recording quality", () => { + const master = "#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=900000,RESOLUTION=854x480\n480.m3u8\n#EXT-X-STREAM-INF:BANDWIDTH=4000000,RESOLUTION=1920x1080\n1080.m3u8\n#EXT-X-STREAM-INF:BANDWIDTH=2000000,RESOLUTION=1280x720\n720.m3u8\n"; + it("prefers the highest variant on automatic and the chosen maximum otherwise", () => { + expect(cam4VariantUrls(master, "https://cdn.test/master.m3u8")).toEqual(["https://cdn.test/1080.m3u8", "https://cdn.test/720.m3u8", "https://cdn.test/480.m3u8"]); + expect(cam4VariantUrls(master, "https://cdn.test/master.m3u8", 720)[0]).toBe("https://cdn.test/720.m3u8"); + expect(cam4VariantUrls(master, "https://cdn.test/master.m3u8", 360)[0]).toBe("https://cdn.test/480.m3u8"); + }); +}); diff --git a/plugins/cam4/index.ts b/plugins/cam4/index.ts index cdcf638..93e6c6a 100644 --- a/plugins/cam4/index.ts +++ b/plugins/cam4/index.ts @@ -1,4 +1,5 @@ import { LIVE_RECORDER_SCRIPT, retryDelay } from "../live-recorder.js"; +import { orderVariantsForRecording, parseHlsVariants, recordingMaxHeight } from "../hls-quality.js"; import { createLiveCamPlugin } from "../live-cam-plugin-factory.js"; import { accountSignal, cookieHeader, readAccountCookies } from "../account-cookies.js"; import { browserHtml } from "../browser-html-utils.js"; @@ -333,17 +334,9 @@ function cam4RoomName(pageUrl: string): string | undefined { catch { return undefined; } } -function variantUrls(manifest: string, baseUrl: string): string[] { - if (!manifest.trimStart().startsWith("#EXTM3U")) return []; - const variants: Array<{ url: string; bandwidth: number }> = []; let info = ""; - for (const raw of manifest.split(/\r?\n/)) { - const line = raw.trim(); - if (line.startsWith("#EXT-X-STREAM-INF")) { info = line; continue; } - if (!line || line.startsWith("#") || !/\.m3u8(?:$|\?)/i.test(line)) continue; - variants.push({ url: new URL(line, baseUrl).toString(), bandwidth: Number(/(?:^|[:,])BANDWIDTH=(\d+)/i.exec(info)?.[1] ?? 0) }); - info = ""; - } - return variants.sort((left, right) => right.bandwidth - left.bandwidth).map((variant) => variant.url); +/** Variant URLs in recording preference order: highest bandwidth, or the best at or below maxHeight. */ +export function cam4VariantUrls(manifest: string, baseUrl: string, maxHeight = 0): string[] { + return orderVariantsForRecording(parseHlsVariants(manifest, baseUrl), maxHeight).map((variant) => variant.url); } const isLivePlaylist = (text: string) => text.trimStart().startsWith("#EXTM3U") && /#EXTINF:/i.test(text) && !/#EXT-X-ENDLIST/i.test(text); @@ -367,7 +360,7 @@ export async function cam4LiveStream(context: PluginContext, username: string): const master = await playlistText(context, masterUrl); if (!master) return undefined; if (isLivePlaylist(master)) return { masterUrl, mediaUrl: masterUrl }; - for (const variant of variantUrls(master, masterUrl)) { + for (const variant of cam4VariantUrls(master, masterUrl, recordingMaxHeight(context.config))) { const playlist = await playlistText(context, variant); if (playlist && isLivePlaylist(playlist)) return { masterUrl, mediaUrl: variant }; } From c95167a43cf1648d78d743f5ba5abc9bd9af98e1 Mon Sep 17 00:00:00 2001 From: MajorLOL <56225967+MajorLOL@users.noreply.github.com> Date: Sat, 10 Oct 2026 15:21:00 +0200 Subject: [PATCH 06/14] Plugins: honour a per-source maximum recording quality - New shared plugins/hls-quality.ts parses HLS variants (RESOLUTION, NAME or playlist name) and orders them for a maximum height: the best at or below it first, then the closest larger one, so a missing quality never fails. - Stripchat, CAM4 and BongaCams pick their live variant with it; automatic keeps the current behaviour (highest first). - yt-dlp based live plugins (Chaturbate, Twitch and the live-cam factory used by CamSoda, MyFreeCams, LiveJasmin, XCams and Cams) pass the height to yt-dlp with a fallback and --format-sort res:N. - Tests for variant parsing and ordering, and for the selected variant in Stripchat, CAM4, BongaCams, Chaturbate and yt-dlp recordings. --- plugins/chaturbate/index.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/chaturbate/index.test.ts b/plugins/chaturbate/index.test.ts index 2d3ff43..9910f0d 100644 --- a/plugins/chaturbate/index.test.ts +++ b/plugins/chaturbate/index.test.ts @@ -36,7 +36,8 @@ describe("Chaturbate plugin", () => { const context = { config: { recordingMaxHeight: 720 }, fetch, log: vi.fn(), runCommand: vi.fn(async () => ({ exitCode: 0, stderr: "", stdout: '{"url":"https://cdn.test/master.m3u8"}' })) }; const request = await chaturbate.resolveDownload!(context, { externalId: "live", mediaType: "video", pageUrl: "https://chaturbate.com/alice/" }); if (request.kind !== "command") throw new Error("Expected yt-dlp recording"); - expect(request.args[request.args.indexOf("--format") + 1]).toBe("bestvideo[height<=720]+bestaudio/best[height<=720]"); + expect(request.args[request.args.indexOf("--format") + 1]).toBe("bestvideo[height<=720]+bestaudio/best[height<=720]/bestvideo+bestaudio/best"); + expect(request.args[request.args.indexOf("--format-sort") + 1]).toBe("res:720"); await chaturbate.resolveLiveStream!(context, { id: "alice", username: "alice", pageUrl: "https://chaturbate.com/alice/" }); expect(vi.mocked(context.runCommand).mock.calls.flat().join(" ")).not.toContain("height<=720"); }); From 5d540a3d740260d4d90faaf51583aa7d84b82750 Mon Sep 17 00:00:00 2001 From: MajorLOL <56225967+MajorLOL@users.noreply.github.com> Date: Sat, 10 Oct 2026 15:21:33 +0200 Subject: [PATCH 07/14] Plugins: honour a per-source maximum recording quality - New shared plugins/hls-quality.ts parses HLS variants (RESOLUTION, NAME or playlist name) and orders them for a maximum height: the best at or below it first, then the closest larger one, so a missing quality never fails. - Stripchat, CAM4 and BongaCams pick their live variant with it; automatic keeps the current behaviour (highest first). - yt-dlp based live plugins (Chaturbate, Twitch and the live-cam factory used by CamSoda, MyFreeCams, LiveJasmin, XCams and Cams) pass the height to yt-dlp with a fallback and --format-sort res:N. - Tests for variant parsing and ordering, and for the selected variant in Stripchat, CAM4, BongaCams, Chaturbate and yt-dlp recordings. --- plugins/stripchat/index.test.ts | 30 +++++++++++++++++++++++++++++- plugins/stripchat/index.ts | 16 +++++++--------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/plugins/stripchat/index.test.ts b/plugins/stripchat/index.test.ts index adab118..f54534f 100644 --- a/plugins/stripchat/index.test.ts +++ b/plugins/stripchat/index.test.ts @@ -3,7 +3,7 @@ import os from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import type { PluginContext } from "../../packages/plugin-sdk/index.js"; -import { listStripchatMedia, resolveStripchatDirect, resolveStripchatDownload, setStripchatFavorite, stripchatFollowedSnapshot, stripchatPublicPlaybackKey } from "./index.js"; +import { listStripchatMedia, resolveStripchatDirect, resolveStripchatDownload, setStripchatFavorite, stripchatFollowedSnapshot, stripchatPublicPlaybackKey, stripchatVariantUrls } from "./index.js"; const temporaryDirectories: string[] = []; @@ -190,3 +190,31 @@ describe("Stripchat account favorites", () => { expect(fetch).toHaveBeenCalledTimes(3); }); }); + +describe("Stripchat recording quality", () => { + const master = [ + "#EXTM3U", + '#EXT-X-STREAM-INF:BANDWIDTH=5000000,RESOLUTION=1920x1080,NAME="1080p"', "https://media-hls.doppiocdn.media/live/42.m3u8", + '#EXT-X-STREAM-INF:BANDWIDTH=2500000,RESOLUTION=1280x720,NAME="720p"', "https://media-hls.doppiocdn.media/live/42_720p.m3u8", + '#EXT-X-STREAM-INF:BANDWIDTH=900000,RESOLUTION=854x480,NAME="480p"', "https://media-hls.doppiocdn.media/live/42_480p.m3u8", + ].join("\n"); + + it("keeps the manifest order (highest first) when set to automatic", () => { + expect(stripchatVariantUrls(master, "https://edge-hls.doppiocdn.media/hls/42/master/42_auto.m3u8")).toEqual([ + "https://media-hls.doppiocdn.media/live/42.m3u8", "https://media-hls.doppiocdn.media/live/42_720p.m3u8", "https://media-hls.doppiocdn.media/live/42_480p.m3u8", + ]); + }); + + it("records the performer's chosen maximum quality", async () => { + const live = "#EXTM3U\n#EXT-X-TARGETDURATION:2\n#EXT-X-MEDIA-SEQUENCE:123\n#EXTINF:2\nsegment.mp4\n"; + const fetch = vi.fn(async (input: string | URL | Request) => { + const url = String(input); + if (url.endsWith("/v2.12.0/main.js")) return new Response('function airplay(url){url.searchParams.set("pkey","PublicKey123456")}'); + return new Response(url.includes("/master/") ? master : live); + }); + const mock = { config: { recordingMaxHeight: 480 }, fetch, log: vi.fn(), runCommand: vi.fn(async () => ({ exitCode: 0, stdout: pageState(), stderr: "" })) }; + const request = await resolveStripchatDownload(mock, { externalId: "stripchat:alice:session", pageUrl: "https://stripchat.com/Alice", mediaType: "video" }); + if (request.kind !== "command") throw new Error("Expected the live recorder"); + expect(request.args[2]).toBe("https://media-hls.doppiocdn.media/live/42_480p.m3u8?pkey=PublicKey123456"); + }); +}); diff --git a/plugins/stripchat/index.ts b/plugins/stripchat/index.ts index 5c9ac4f..c73d94a 100644 --- a/plugins/stripchat/index.ts +++ b/plugins/stripchat/index.ts @@ -1,4 +1,5 @@ import { LIVE_RECORDER_SCRIPT, retryDelay } from "../live-recorder.js"; +import { orderVariantsForRecording, parseHlsVariants, recordingMaxHeight } from "../hls-quality.js"; import { createLiveCamPlugin } from "../live-cam-plugin-factory.js"; import { accountSignal, cookieHeader, readAccountCookies } from "../account-cookies.js"; import { browserHtml } from "../browser-html-utils.js"; @@ -216,14 +217,10 @@ export function stripchatPublicPlaybackKey(playerSource: string): string | undef return playerSource.match(PLAYBACK_KEY_PATTERN)?.[1]; } -function playlistUrls(manifest: string, baseUrl: string): string[] { - const urls: string[] = []; - for (const line of manifest.split(/\r?\n/)) { - const value = line.trim(); - if (!value || value.startsWith("#") || !/\.m3u8(?:$|\?)/i.test(value)) continue; - try { urls.push(new URL(value, baseUrl).toString()); } catch { /* Ignore malformed variants. */ } - } - return urls; +/** Variant URLs in recording preference order: manifest order (highest first) or the best at or below maxHeight. */ +export function stripchatVariantUrls(manifest: string, baseUrl: string, maxHeight = 0): string[] { + const variants = parseHlsVariants(manifest, baseUrl); + return (maxHeight ? orderVariantsForRecording(variants, maxHeight) : variants).map((variant) => variant.url); } function isLivePlaylist(manifest: string): boolean { @@ -242,6 +239,7 @@ async function resolveStripchatHls(context: PluginContext, pageUrl: string): Pro const playerResponse = await context.fetch(stream.playerScriptUrl, { headers, signal: context.signal ?? AbortSignal.timeout(15_000) }); if (!playerResponse.ok) throw new Error(`Stripchat player module returned HTTP ${playerResponse.status}`); const playbackKey = stripchatPublicPlaybackKey(await playerResponse.text()); + const maxHeight = recordingMaxHeight(context.config); if (!playbackKey) throw new Error("Stripchat player module did not expose a public playback key"); for (const domain of stream.domains) { @@ -252,7 +250,7 @@ async function resolveStripchatHls(context: PluginContext, pageUrl: string): Pro if (!response.ok) continue; const manifest = await response.text(); if (!manifest.trimStart().startsWith("#EXTM3U") || manifest.includes("#EXT-X-MOUFLON-ADVERT")) continue; - for (const candidate of playlistUrls(manifest, master.toString())) { + for (const candidate of stripchatVariantUrls(manifest, master.toString(), maxHeight)) { const variant = new URL(candidate); if (!variant.searchParams.has("pkey")) variant.searchParams.set("pkey", playbackKey); const variantResponse = await context.fetch(variant, { headers, signal: context.signal ?? AbortSignal.timeout(15_000) }); From b88a7274e1d4cd53a31ecfa0119d3f18924cdea9 Mon Sep 17 00:00:00 2001 From: MajorLOL <56225967+MajorLOL@users.noreply.github.com> Date: Sat, 10 Oct 2026 15:22:11 +0200 Subject: [PATCH 08/14] Plugins: honour a per-source maximum recording quality - New shared plugins/hls-quality.ts parses HLS variants (RESOLUTION, NAME or playlist name) and orders them for a maximum height: the best at or below it first, then the closest larger one, so a missing quality never fails. - Stripchat, CAM4 and BongaCams pick their live variant with it; automatic keeps the current behaviour (highest first). - yt-dlp based live plugins (Chaturbate, Twitch and the live-cam factory used by CamSoda, MyFreeCams, LiveJasmin, XCams and Cams) pass the height to yt-dlp with a fallback and --format-sort res:N. - Tests for variant parsing and ordering, and for the selected variant in Stripchat, CAM4, BongaCams, Chaturbate and yt-dlp recordings. --- plugins/twitch/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/twitch/index.ts b/plugins/twitch/index.ts index ba59615..f16741d 100644 --- a/plugins/twitch/index.ts +++ b/plugins/twitch/index.ts @@ -1,4 +1,5 @@ import { createHash } from "node:crypto"; +import { recordingMaxHeight } from "../hls-quality.js"; import { definePlugin, type MediaCandidate } from "../../packages/plugin-sdk/index.js"; import { listDiscoveredLiveCams } from "../live-cam-discovery.js"; import { configuredArgs, runYtDlpJson, testYtDlp, ytDlpDownload, ytDlpLiveStream } from "../yt-dlp-utils.js"; @@ -45,5 +46,5 @@ export default definePlugin({ }, async resolveLiveStream(context, cam) { return ytDlpLiveStream(context, cam, { referer: "https://www.twitch.tv/" }); }, async listLiveCams(context, query) { return listDiscoveredLiveCams(context, "twitch", query); }, - async resolveDownload(context, item) { return ytDlpDownload(item, context.config, { referer: "https://www.twitch.tv/", live: true }); }, + async resolveDownload(context, item) { return ytDlpDownload(item, context.config, { referer: "https://www.twitch.tv/", live: true, maxHeight: recordingMaxHeight(context.config) }); }, }); From d9fe30034cfa07141fbe4cf531aa8f5a615de306 Mon Sep 17 00:00:00 2001 From: MajorLOL <56225967+MajorLOL@users.noreply.github.com> Date: Thu, 15 Oct 2026 15:34:53 +0200 Subject: [PATCH 09/14] Fix issue #33: Stripchat favorites, restarts after stop/cancel, stuck cancels and lost recordings - Stripchat favorites: when the direct API is refused (HTTP 403/418), read the followed list inside a real Stripchat page with the saved session (easyx-browser-fetch --stripchat-favorites). Local favorites are kept on failure. - Stop and save no longer blocks the room: a performer who stays online is recorded again as a new video. Cancel still blocks until offline; a hard refresh of the source now lifts it. - A cancel the recording process ignores is forced (SIGKILL) after 10 s; a stop that hangs gets a second SIGINT after 45 s. - Shutdown saves running live recordings first (EASYX_SHUTDOWN_GRACE_SECONDS, default 60; compose stop_grace_period 90s). Recordings cut off by a crash or a killed container are joined from their parts on the next start; unreadable data is kept in media/.recording-recovery. - Tests: server/live-restart.test.ts and Stripchat favorites fallback tests. --- README.md | 2 +- compose.yaml | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 280b838..cc6aecc 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ For JavLibrary, see [Connect FlareSolverr](docs/FLARESOLVERR.md) for an existing Set `EASYX_MAX_CONCURRENT_DOWNLOADS_LIMIT` to a positive integer (default `8`) to raise the ceiling of **Settings → Automation → Maximum concurrent downloads**. Restart the server after changing it, then save the desired concurrency in Settings. Invalid values fall back to 8; the API, input and download queue share the same ceiling. -Stripchat, BongaCams and CAM4 recordings can span short interruptions. `STRIPCHAT_MERGE_GAP_MINUTES`, `BONGACAMS_MERGE_GAP_MINUTES` and `CAM4_MERGE_GAP_MINUTES` default to 10; set any of them to `0` to finalize as soon as its stream ends. BongaCams and CAM4 fall back to the Stripchat value when their own variable is unset. Waiting recordings occupy a download slot. Automatic recording restarts after a completed or failed session, with backoff for short/failed recordings. A manual stop, cancel or deletion stays suppressed until a successful scan observes the room offline or non-public, including across app restarts. +Stripchat, BongaCams and CAM4 recordings can span short interruptions. `STRIPCHAT_MERGE_GAP_MINUTES`, `BONGACAMS_MERGE_GAP_MINUTES` and `CAM4_MERGE_GAP_MINUTES` default to 10; set any of them to `0` to finalize as soon as its stream ends. BongaCams and CAM4 fall back to the Stripchat value when their own variable is unset. Waiting recordings occupy a download slot. Automatic recording restarts after a completed or failed session, with backoff for short/failed recordings. **Stop and save** keeps what was recorded and records the performer again as a new video on the next check while the room stays live. A cancel or deletion stays suppressed until a successful scan observes the room offline or non-public, including across app restarts; a **hard refresh** of the source lifts it immediately. A cancel that the recording process ignores is forced after 10 seconds. When the app stops, running live recordings are saved first (up to `EASYX_SHUTDOWN_GRACE_SECONDS`, default 60; keep Docker's `stop_grace_period` longer, the compose file uses 90s). Recordings cut off by a crash or a killed container are joined from their recorded parts and stored in the library on the next start; unreadable data is kept in `media/.recording-recovery`. Important environment variables include `PUID`, `PGID`, `EASYX_SCAN_INTERVAL_MINUTES`, `EASYX_WHISPER_MODEL`, `EASYX_TRANSLATION_MODEL`, and `EASYX_LOG_LEVEL`. diff --git a/compose.yaml b/compose.yaml index 8fee82c..a25a0d3 100644 --- a/compose.yaml +++ b/compose.yaml @@ -4,6 +4,8 @@ services: pull_policy: always container_name: open-easyx restart: unless-stopped + # Time to save running live recordings when the container stops (see EASYX_SHUTDOWN_GRACE_SECONDS). + stop_grace_period: 90s ports: - "3210:3210" environment: From f1dab612347a1b321479e3ca97d77d1db6173099 Mon Sep 17 00:00:00 2001 From: MajorLOL <56225967+MajorLOL@users.noreply.github.com> Date: Thu, 15 Oct 2026 15:35:31 +0200 Subject: [PATCH 10/14] Fix issue #33: Stripchat favorites, restarts after stop/cancel, stuck cancels and lost recordings - Stripchat favorites: when the direct API is refused (HTTP 403/418), read the followed list inside a real Stripchat page with the saved session (easyx-browser-fetch --stripchat-favorites). Local favorites are kept on failure. - Stop and save no longer blocks the room: a performer who stays online is recorded again as a new video. Cancel still blocks until offline; a hard refresh of the source now lifts it. - A cancel the recording process ignores is forced (SIGKILL) after 10 s; a stop that hangs gets a second SIGINT after 45 s. - Shutdown saves running live recordings first (EASYX_SHUTDOWN_GRACE_SECONDS, default 60; compose stop_grace_period 90s). Recordings cut off by a crash or a killed container are joined from their parts on the next start; unreadable data is kept in media/.recording-recovery. - Tests: server/live-restart.test.ts and Stripchat favorites fallback tests. From a3fbed76aff30a154c03595c702afd3dce3275d8 Mon Sep 17 00:00:00 2001 From: MajorLOL <56225967+MajorLOL@users.noreply.github.com> Date: Thu, 15 Oct 2026 15:35:56 +0200 Subject: [PATCH 11/14] Fix issue #33: Stripchat favorites, restarts after stop/cancel, stuck cancels and lost recordings - Stripchat favorites: when the direct API is refused (HTTP 403/418), read the followed list inside a real Stripchat page with the saved session (easyx-browser-fetch --stripchat-favorites). Local favorites are kept on failure. - Stop and save no longer blocks the room: a performer who stays online is recorded again as a new video. Cancel still blocks until offline; a hard refresh of the source now lifts it. - A cancel the recording process ignores is forced (SIGKILL) after 10 s; a stop that hangs gets a second SIGINT after 45 s. - Shutdown saves running live recordings first (EASYX_SHUTDOWN_GRACE_SECONDS, default 60; compose stop_grace_period 90s). Recordings cut off by a crash or a killed container are joined from their parts on the next start; unreadable data is kept in media/.recording-recovery. - Tests: server/live-restart.test.ts and Stripchat favorites fallback tests. --- src/SourceHardRefresh.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SourceHardRefresh.tsx b/src/SourceHardRefresh.tsx index 670953a..e7516db 100644 --- a/src/SourceHardRefresh.tsx +++ b/src/SourceHardRefresh.tsx @@ -16,5 +16,5 @@ export function SourceHardRefresh({ sourceId, domain, run }: { sourceId: string; } finally { setPending(false); } }, `${domain} hard refresh completed`); }; - return {result && {result}}; + return {result && {result}}; } From 841efe0b5cfdedbcdfd54a327fd1b58fe292cead Mon Sep 17 00:00:00 2001 From: MajorLOL <56225967+MajorLOL@users.noreply.github.com> Date: Thu, 15 Oct 2026 15:36:18 +0200 Subject: [PATCH 12/14] Fix issue #33: Stripchat favorites, restarts after stop/cancel, stuck cancels and lost recordings - Stripchat favorites: when the direct API is refused (HTTP 403/418), read the followed list inside a real Stripchat page with the saved session (easyx-browser-fetch --stripchat-favorites). Local favorites are kept on failure. - Stop and save no longer blocks the room: a performer who stays online is recorded again as a new video. Cancel still blocks until offline; a hard refresh of the source now lifts it. - A cancel the recording process ignores is forced (SIGKILL) after 10 s; a stop that hangs gets a second SIGINT after 45 s. - Shutdown saves running live recordings first (EASYX_SHUTDOWN_GRACE_SECONDS, default 60; compose stop_grace_period 90s). Recordings cut off by a crash or a killed container are joined from their parts on the next start; unreadable data is kept in media/.recording-recovery. - Tests: server/live-restart.test.ts and Stripchat favorites fallback tests. --- server/database.ts | 10 +++ server/downloader.ts | 174 ++++++++++++++++++++++++++++++++++-- server/index.ts | 4 +- server/live-restart.test.ts | 169 ++++++++++++++++++++++++++++++++++ server/source-sync.ts | 3 + 5 files changed, 353 insertions(+), 7 deletions(-) create mode 100644 server/live-restart.test.ts diff --git a/server/database.ts b/server/database.ts index 69a468a..ffaaf67 100644 --- a/server/database.ts +++ b/server/database.ts @@ -710,6 +710,16 @@ export class Database { return row ? this.mapItem(row) : undefined; } + /** Live recordings that were running or being stopped when the application went down. */ + interruptedLiveItems(): DownloadItem[] { + return (this.sqlite.prepare("SELECT * FROM items WHERE status IN ('downloading','stopping') AND json_extract(metadata_json,'$.live')=1 ORDER BY created_at").all() as any[]).map((row) => this.mapItem(row)); + } + + /** Lift the "stopped or cancelled while live" block for a source, as if the room had been seen offline. */ + resumeLiveRecordings(sourceId: string) { + return Number(this.sqlite.prepare("UPDATE live_recording_state SET suppressed=0,offline=1,retries=0 WHERE source_id=?").run(sourceId).changes); + } + requeueInterruptedDownloads() { const stamp = now(); const queued = this.sqlite.prepare("UPDATE items SET status='queued',progress=0,downloaded_bytes=0,error=NULL,download_started_at=NULL,download_finished_at=NULL,updated_at=? WHERE status='downloading'").run(stamp); diff --git a/server/downloader.ts b/server/downloader.ts index eb2a628..0143cd9 100644 --- a/server/downloader.ts +++ b/server/downloader.ts @@ -25,12 +25,22 @@ export function recordingConfig(config: Record, source: Pick(); private finalizers = new Map>(); private timer?: NodeJS.Timeout; + private stopped = false; constructor( private db: Database, private plugins: PluginManager, @@ -40,9 +50,23 @@ export class DownloadQueue { private onDeleteCompleted?: (item: DownloadItem) => unknown, ) {} - start() { + /** Resolves once interrupted recordings are recovered and the queue runs. */ + start(): Promise { fs.mkdirSync(this.mediaRoot, { recursive: true }); fs.mkdirSync(this.downloadsRoot, { recursive: true, mode: 0o700 }); + this.stopped = false; + const interrupted = this.db.interruptedLiveItems().filter((item) => recordedInput(this.stagingDirectory(item.id))); + if (!interrupted.length) { this.run(); return Promise.resolve(); } + return this.boot(interrupted); + } + + private async boot(interrupted: DownloadItem[]) { + try { await this.recoverInterruptedRecordings(interrupted); } + catch (error) { this.writeLog?.("error", "download", "Interrupted recordings could not be recovered", { error: error instanceof Error ? error.message : String(error) }); } + if (!this.stopped) this.run(); + } + + private run() { this.db.requeueInterruptedDownloads(); this.timer = setInterval(() => void this.tick(), 1000); this.timer.unref(); @@ -50,10 +74,46 @@ export class DownloadQueue { } stop() { + this.stopped = true; if (this.timer) clearInterval(this.timer); for (const control of this.active.values()) { this.signal(control, "SIGTERM"); control.abort?.abort(); } } + /** + * Application shutdown. Live recordings are stopped like "Stop and save" and get up + * to graceMs to be stored in the library; other downloads are interrupted and start + * again on the next run. Whatever is still unfinished when the grace period ends is + * recovered from its recorded parts on the next start. + */ + async shutdown(graceMs = SHUTDOWN_GRACE_MS) { + this.stopped = true; + if (this.timer) clearInterval(this.timer); + const live: Array> = []; + for (const [itemId, control] of this.active) { + control.shuttingDown = true; + if (control.live && (control.encoding || control.matching)) live.push(this.settled(itemId)); + else if (control.live && !control.action) { + control.action = "stop"; control.paused = false; + this.signal(control, "SIGCONT"); this.signal(control, "SIGINT"); + live.push(this.settled(itemId)); + } else if (!control.live) { + this.signal(control, "SIGTERM"); control.abort?.abort(); + } else live.push(this.settled(itemId)); + } + if (!live.length) return; + this.writeLog?.("info", "download", "Saving live recordings before shutdown", { recordings: live.length, graceSeconds: Math.round(graceMs / 1000) }); + let timer: NodeJS.Timeout | undefined; + await Promise.race([Promise.all(live), new Promise((resolve) => { timer = setTimeout(resolve, graceMs); })]); + if (timer) clearTimeout(timer); + } + + private settled(itemId: string): Promise { + return new Promise((resolve) => { + const check = () => this.active.has(itemId) ? setTimeout(check, 100).unref() : resolve(); + check(); + }); + } + pause(itemId: string) { const item = this.requiredItem(itemId); if (item.status === "queued") return this.db.setItemStatus(itemId, "paused"); @@ -109,12 +169,34 @@ export class DownloadQueue { this.db.suppressLiveRecording(itemId); return this.db.setItemStatus(itemId, action === "delete" ? "deleted" : "cancelled"); } - this.db.suppressLiveRecording(itemId); + // "Stop and save" keeps the room eligible: a performer who is still online is + // recorded again (as a new video) on the next check. Cancel and delete suppress + // the room until it is seen offline or the source is hard-refreshed. + if (action !== "stop") this.db.suppressLiveRecording(itemId); control.action = action; control.paused = false; this.signal(control, "SIGCONT"); this.signal(control, action === "stop" ? "SIGINT" : "SIGTERM"); control.abort?.abort(); + this.escalate(itemId, control); return this.db.setItemStatus(itemId, action === "stop" ? "stopping" : "cancelling"); } + /** Make sure a stop or cancel always ends, even when a process ignores the first signal. */ + private escalate(itemId: string, control: ActiveDownload) { + if (control.escalation) clearTimeout(control.escalation); + const cancelling = control.action === "cancel" || control.action === "delete"; + control.escalation = setTimeout(() => { + control.escalation = undefined; + if (this.active.get(itemId) !== control || !control.child) return; + if (cancelling) { + this.writeLog?.("warn", "download", "Download did not stop after cancel; killing it", { itemId }); + this.signal(control, "SIGKILL"); + } else { + this.writeLog?.("warn", "download", "Recording is still finishing after stop; asking it again", { itemId }); + this.signal(control, "SIGINT"); + } + }, cancelling ? DownloadQueue.cancelKillAfterMs : DownloadQueue.stopRepeatAfterMs); + control.escalation.unref(); + } + private async tick() { const max = concurrentDownloads(this.db.getSettings().maxConcurrentDownloads); while (this.active.size < max) { @@ -124,7 +206,7 @@ export class DownloadQueue { this.active.set(item.id, control); const startedItem = this.db.setItemStatus(item.id, "downloading", { progress: 0 })!; this.writeLog?.("info", "download", "Download started", { itemId: item.id, pluginId: item.pluginId, title: item.title, mediaType: item.mediaType }); - void this.download(startedItem, control).finally(() => this.active.delete(item.id)); + void this.download(startedItem, control).finally(() => { if (control.escalation) clearTimeout(control.escalation); this.active.delete(item.id); }); } if (this.active.size >= max) this.preemptForPriority(); } @@ -150,6 +232,7 @@ export class DownloadQueue { const [itemId, control] = victim; control.preempted = true; control.action = "stop"; this.signal(control, "SIGCONT"); this.signal(control, "SIGINT"); control.abort?.abort(); + this.escalate(itemId, control); const item = this.db.setItemStatus(itemId, "stopping"); this.writeLog?.("info", "download", "Recording stopped for a higher-priority performer", { itemId, title: item?.title, priority: control.priority ?? 0, waitingPriority: priority }); } @@ -288,6 +371,9 @@ export class DownloadQueue { void Promise.resolve(this.onCompleted?.()).catch((error) => this.writeLog?.("warn", "library", "Library refresh after download failed", { error })); }); } catch (error) { + // Interrupted by the shutdown: a live recording keeps its parts for recovery on the + // next start; any other download stays "downloading" and is queued again. + if (control.shuttingDown) { if (control.live) preserveTemporary = true; return; } let message = error instanceof Error ? error.message : String(error); const parts = temporaryDirectory && path.join(temporaryDirectory, "live-parts"); if (!["cancel", "delete"].includes(control.action ?? "") && parts && fs.existsSync(parts) && fs.readdirSync(parts).some((file) => file.endsWith(".ts") && fs.statSync(path.join(parts, file)).size > 0)) { @@ -326,6 +412,64 @@ export class DownloadQueue { private get downloadsRoot() { return path.join(this.mediaRoot, ".downloads"); } + /** + * A restart (or a container that was killed) used to throw away a running live + * recording: the item was queued again and its staging folder wiped. Whatever was + * recorded is now turned into a video in the library first; the performer is then + * recorded again as a new video if the room is still live. + */ + private stagingDirectory(itemId: string) { return path.join(this.downloadsRoot, safeSegment(itemId, "download")); } + + private async recoverInterruptedRecordings(items: DownloadItem[]) { + let recovered = 0; + for (const item of items) { + if (this.stopped) return; + const directory = this.stagingDirectory(item.id); + const input = recordedInput(directory); + if (!input) continue; + const output = path.join(directory, `recovered-${Date.now()}.mp4`); + try { + if (input.parts) { + const list = path.join(directory, "recovered-parts.txt"); + fs.writeFileSync(list, input.parts.map((part) => `file '${part.replaceAll("'", "'\\''")}'`).join("\n") + "\n"); + await this.capture("ffmpeg", ["-hide_banner", "-loglevel", "error", "-f", "concat", "-safe", "0", "-i", list, "-map", "0:v:0", "-map", "0:a:0?", "-c", "copy", "-movflags", "+faststart", "-y", output], 30 * 60_000); + } else { + await this.capture("ffmpeg", ["-hide_banner", "-loglevel", "error", "-i", input.file!, "-map", "0:v:0", "-map", "0:a:0?", "-c", "copy", "-movflags", "+faststart", "-y", output], 30 * 60_000); + } + if (!fs.existsSync(output) || fs.statSync(output).size === 0) throw new Error("FFmpeg produced no video"); + const performer = this.db.getPerformer(item.performerId); const source = this.db.getSource(item.sourceId); + if (!performer || !source) throw new Error("The performer or source no longer exists"); + const fallback = `${item.externalId}.mp4`; + const filename = safeSegment(item.filename ?? fallback, fallback); + const destination = path.join(this.mediaRoot, downloadOutputPath(outputSettings(this.db.getSettings()), item, performer.name, source.domain, filename)); + this.prepareOutputDirectory(path.dirname(destination)); + const canonicalDate = this.db.setCanonicalMediaDate(item.id, item.publishedAt); + await this.applyMediaDate(output, item.mediaType, canonicalDate); + const checksum = await this.hashFile(output); + const finalPath = this.availableDestination(destination, item.id); + fs.renameSync(output, finalPath); + const storagePath = path.relative(this.mediaRoot, finalPath); + this.db.setItemStatus(item.id, "completed", { progress: 1, checksum, storagePath, error: null }); + fs.rmSync(directory, { recursive: true, force: true }); + recovered += 1; + this.writeLog?.("info", "download", "Live recording interrupted by a restart was saved", { itemId: item.id, title: item.title, storagePath, parts: input.parts?.length ?? 1 }); + } catch (error) { + fs.rmSync(output, { force: true }); + let kept = path.relative(this.mediaRoot, directory); + try { + const recovery = path.join(this.mediaRoot, ".recording-recovery", safeSegment(item.id, "recording")); + fs.mkdirSync(path.dirname(recovery), { recursive: true }); + fs.rmSync(recovery, { recursive: true, force: true }); + fs.renameSync(directory, recovery); kept = path.relative(this.mediaRoot, recovery); + } catch { /* Leave it in staging. */ } + const reason = error instanceof Error ? error.message : String(error); + this.db.setItemStatus(item.id, "failed", { error: `Recording interrupted by a restart could not be joined (${reason}). The recorded data is kept at ${kept}.` }); + this.writeLog?.("error", "download", "Interrupted live recording could not be recovered", { itemId: item.id, kept, error: reason }); + } + } + if (recovered) void Promise.resolve(this.onCompleted?.()).catch((error) => this.writeLog?.("warn", "library", "Library refresh after recovery failed", { error })); + } + private async checkVideoExcerpt(item: DownloadItem, file: string, control: ActiveDownload) { const controller = new AbortController(); control.abort = controller; control.matching = true; const timer = setTimeout(() => controller.abort(), 120_000); timer.unref(); @@ -495,11 +639,11 @@ export class DownloadQueue { } } - private capture(command: string, args: string[]): Promise<{ stdout: Buffer; stderr: Buffer }> { + private capture(command: string, args: string[], timeoutMs = 120_000): Promise<{ stdout: Buffer; stderr: Buffer }> { return new Promise((resolve, reject) => { const child = spawn(command, args, { stdio: ["ignore", "pipe", "pipe"] }); const stdout: Buffer[] = []; const stderr: Buffer[] = []; - const timer = setTimeout(() => { child.kill("SIGKILL"); reject(new Error(`${command} timed out`)); }, 120_000); timer.unref(); + const timer = setTimeout(() => { child.kill("SIGKILL"); reject(new Error(`${command} timed out`)); }, timeoutMs); timer.unref(); child.stdout.on("data", (chunk: Buffer) => stdout.push(chunk)); child.stderr.on("data", (chunk: Buffer) => stderr.push(chunk)); child.once("error", (error) => { clearTimeout(timer); reject(error); }); child.once("close", (code) => { clearTimeout(timer); code === 0 ? resolve({ stdout: Buffer.concat(stdout), stderr: Buffer.concat(stderr) }) : reject(new Error(`${command} exited with code ${code}: ${Buffer.concat(stderr).toString("utf8").trim()}`)); }); @@ -515,3 +659,21 @@ export class DownloadQueue { finally { if (this.finalizers.get(key) === settled) this.finalizers.delete(key); } } } + +const RECORDING_EXTENSIONS = new Set([".ts", ".mp4", ".mkv", ".flv", ".m4v", ".webm", ".part"]); + +/** What an interrupted recording left in its staging folder: recorder parts, or the largest partial file. */ +export function recordedInput(directory: string): { parts?: string[]; file?: string } | undefined { + const nonEmpty = (file: string) => { try { const stat = fs.statSync(file); return stat.isFile() && stat.size > 0; } catch { return false; } }; + const partsDirectory = path.join(directory, "live-parts"); + let parts: string[] = []; + try { parts = fs.readdirSync(partsDirectory).filter((name) => name.endsWith(".ts")).sort().map((name) => path.join(partsDirectory, name)).filter(nonEmpty); } catch { /* No recorder parts. */ } + if (parts.length) return { parts }; + let files: string[] = []; + try { + files = fs.readdirSync(directory).map((name) => path.join(directory, name)) + .filter((file) => nonEmpty(file) && (RECORDING_EXTENSIONS.has(path.extname(file).toLowerCase()) || /\.(?:mp4|mkv|ts|webm)\.part$/i.test(file)) && !path.basename(file).startsWith("recovered-")); + } catch { return undefined; } + if (!files.length) return undefined; + return { file: files.sort((left, right) => fs.statSync(right).size - fs.statSync(left).size)[0] }; +} diff --git a/server/index.ts b/server/index.ts index a3db819..f4f54d6 100644 --- a/server/index.ts +++ b/server/index.ts @@ -654,7 +654,9 @@ function startEmbeddedSubtitleWorker() { } const shutdown = async () => { - shuttingDown = true; queue.stop(); if (subtitleWorkerRestart) clearTimeout(subtitleWorkerRestart); subtitleWorker?.kill("SIGTERM"); + // A second signal while live recordings are being saved exits at once; they are recovered on the next start. + if (shuttingDown) process.exit(0); + shuttingDown = true; await queue.shutdown(); if (subtitleWorkerRestart) clearTimeout(subtitleWorkerRestart); subtitleWorker?.kill("SIGTERM"); await browserLogin.stop(); await app.close(); libraryDb.close(); db.close(); process.exit(0); }; process.on("SIGTERM", shutdown); process.on("SIGINT", shutdown); diff --git a/server/live-restart.test.ts b/server/live-restart.test.ts new file mode 100644 index 0000000..218b366 --- /dev/null +++ b/server/live-restart.test.ts @@ -0,0 +1,169 @@ +import { afterEach, describe, expect, it } from "vitest"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { execFileSync } from "node:child_process"; +import { Database, type Source } from "./database.js"; +import { PluginManager } from "./plugin-manager.js"; +import { DownloadQueue, recordedInput } from "./downloader.js"; +import { SourceSync } from "./source-sync.js"; + +const dirs: string[] = []; +const defaults = { kill: DownloadQueue.cancelKillAfterMs, repeat: DownloadQueue.stopRepeatAfterMs }; +afterEach(() => { + DownloadQueue.cancelKillAfterMs = defaults.kill; DownloadQueue.stopRepeatAfterMs = defaults.repeat; + for (const dir of dirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true }); +}); +const temp = (name: string) => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), `${name}-`)); dirs.push(dir); return dir; }; +async function waitFor(check: () => boolean, ms = 6000) { + const deadline = Date.now() + ms; + while (!check()) { if (Date.now() > deadline) throw new Error("Timed out"); await new Promise((resolve) => setTimeout(resolve, 25)); } +} + +// Recorder doubles: "recorder" finishes on SIGINT (stop and save) and exits 143 on SIGTERM; +// "stubborn" ignores both, like a process stuck on the network. +const scripts = { + recorder: "const fs=require('node:fs'),f=process.argv[1];fs.writeFileSync(f,'start');const t=setInterval(()=>fs.appendFileSync(f,'x'),25);process.on('SIGINT',()=>{clearInterval(t);process.exit(0)});process.on('SIGTERM',()=>process.exit(143))", + stubborn: "const fs=require('node:fs'),f=process.argv[1];fs.writeFileSync(f,'start');setInterval(()=>fs.appendFileSync(f,'x'),25);process.on('SIGINT',()=>{});process.on('SIGTERM',()=>{})", +}; + +async function setup() { + const root = temp("easyx-live-restart"); const mediaDir = path.join(root, "media"); const pluginDir = path.join(root, "plugins"); + fs.mkdirSync(path.join(pluginDir, "test"), { recursive: true }); + fs.writeFileSync(path.join(pluginDir, "test", "index.mjs"), `export default { + manifest: { id: "test.live", name: "Live", version: "1", description: "Test", author: "Test", capabilities: ["media-listing", "download-resolver", "live-cam"], sourceUrlPatterns: ["https://live.test/*"], polling: { mode: "live", defaultIntervalSeconds: 10, minimumIntervalSeconds: 5 } }, + listMedia: async () => [], + resolveLiveStream: async () => ({ url: "https://cdn.test/live.m3u8" }), + resolveDownload: async (_context, item) => ({ kind: "command", command: process.execPath, args: ["-e", item.metadata.script ?? ${JSON.stringify(scripts.recorder)}, "{output}"], filename: "recording.mp4" }) + };`); + const db = new Database(path.join(root, "data")); const plugins = new PluginManager(db, [pluginDir]); await plugins.load(); plugins.install("test.live"); + const performer = db.createPerformer({ name: "Alice" }); + const added = db.addSource(performer.id, "test.live", { externalId: "https://live.test/alice", label: "live.test", profileUrl: "https://live.test/alice", domain: "live.test" }); + const source = db.updateSource(added.id, { autoDownload: true, scraperPluginId: "test.live", scrapeEnabled: true, syncIntervalSeconds: 10 })!; + return { root, mediaDir, db, plugins, source }; +} +const candidate = (script?: string) => ({ externalId: "live:alice", title: "Alice live", pageUrl: "https://live.test/alice", mediaType: "video" as const, filename: "alice.mp4", metadata: { live: true, ...(script ? { script } : {}) } }); +function scan(db: Database, source: Source, script?: string, nowMs?: number) { + const scanned = db.liveScanCandidates(source, [candidate(script)], nowMs); + db.ingestItems(source, scanned); + return scanned; +} +const liveItems = (db: Database) => db.listItems().filter((item) => item.metadata.live === true); + +describe("live recordings after stop, cancel and hard refresh", () => { + it("records a performer again after Stop and save while they stay online", async () => { + const { db, plugins, mediaDir, source } = await setup(); + const queue = new DownloadQueue(db, plugins, mediaDir); queue.start(); + try { + scan(db, source); + await waitFor(() => liveItems(db)[0]?.status === "downloading"); + const [first] = liveItems(db); + await waitFor(() => fs.existsSync(path.join(mediaDir, ".downloads", first.id, "recording.mp4"))); + queue.stopRecording(first.id); + await waitFor(() => db.getItem(first.id)?.status === "completed"); + // The next check (after the short-recording backoff) starts a new video. + expect(scan(db, db.getSource(source.id)!, undefined, Date.now() + 60_000)).toHaveLength(1); + expect(liveItems(db)).toHaveLength(2); + } finally { queue.stop(); await waitFor(() => liveItems(db).every((item) => !["downloading", "stopping"].includes(item.status))).catch(() => {}); db.close(); } + }); + + it("keeps a cancelled room blocked while it stays online, until a hard refresh", async () => { + const { db, plugins, mediaDir, source } = await setup(); + const queue = new DownloadQueue(db, plugins, mediaDir); queue.start(); + try { + scan(db, source); + await waitFor(() => liveItems(db)[0]?.status === "downloading"); + queue.cancel(liveItems(db)[0].id); + await waitFor(() => liveItems(db)[0]?.status === "cancelled"); + expect(scan(db, db.getSource(source.id)!, undefined, Date.now() + 3_600_000)).toEqual([]); + queue.stop(); + const sync = new SourceSync(db, mediaDir, () => ({ manifest: plugins.get("test.live").manifest, listMedia: async () => [candidate()] }) as any, + (pluginId) => plugins.context(pluginId), async () => undefined); + const result = await sync.sync(source.id, true); + expect(result.added).toBe(1); + expect(liveItems(db).filter((item) => item.status === "queued")).toHaveLength(1); + } finally { queue.stop(); db.close(); } + }); + + it("ends a cancel that the recording process ignores", async () => { + DownloadQueue.cancelKillAfterMs = 300; + const { db, plugins, mediaDir, source } = await setup(); + const queue = new DownloadQueue(db, plugins, mediaDir); queue.start(); + try { + scan(db, source, scripts.stubborn); + await waitFor(() => liveItems(db)[0]?.status === "downloading"); + const [item] = liveItems(db); + await waitFor(() => fs.existsSync(path.join(mediaDir, ".downloads", item.id, "recording.mp4"))); + queue.cancel(item.id); + expect(db.getItem(item.id)?.status).toBe("cancelling"); + await waitFor(() => db.getItem(item.id)?.status === "cancelled", 4000); + } finally { queue.stop(); db.close(); } + }); +}); + +describe("live recordings across an application restart", () => { + it("saves running live recordings on shutdown instead of discarding them", async () => { + const { db, plugins, mediaDir, source } = await setup(); + const queue = new DownloadQueue(db, plugins, mediaDir); queue.start(); + try { + scan(db, source); + await waitFor(() => liveItems(db)[0]?.status === "downloading"); + const [item] = liveItems(db); + await waitFor(() => (fs.statSync(path.join(mediaDir, ".downloads", item.id, "recording.mp4"), { throwIfNoEntry: false })?.size ?? 0) > 10); + await queue.shutdown(5_000); + const saved = db.getItem(item.id)!; + expect(saved.status).toBe("completed"); + expect(fs.statSync(path.join(mediaDir, saved.storagePath!)).size).toBeGreaterThan(10); + // Shutdown is not a manual stop: the room stays eligible for a new session. + expect(scan(db, db.getSource(source.id)!, undefined, Date.now() + 60_000)).toHaveLength(1); + } finally { queue.stop(); db.close(); } + }); + + it("turns the recorded parts of a killed recording into a library video on the next start", async () => { + const { db, plugins, mediaDir, source } = await setup(); + scan(db, source); + const [item] = liveItems(db); + db.setItemStatus(item.id, "downloading", { progress: 0 }); + const parts = path.join(mediaDir, ".downloads", item.id, "live-parts"); fs.mkdirSync(parts, { recursive: true }); + for (const name of ["part-001.ts", "part-002.ts"]) { + execFileSync("ffmpeg", ["-hide_banner", "-loglevel", "error", "-f", "lavfi", "-i", "testsrc=duration=1:size=160x120:rate=10", "-f", "lavfi", "-i", "sine=duration=1", + "-c:v", "mpeg4", "-c:a", "aac", "-shortest", "-f", "mpegts", "-y", path.join(parts, name)]); + } + const queue = new DownloadQueue(db, plugins, mediaDir); + try { + await queue.start(); + const saved = db.getItem(item.id)!; + expect(saved.status).toBe("completed"); + const file = path.join(mediaDir, saved.storagePath!); + const duration = Number(execFileSync("ffprobe", ["-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0", file]).toString()); + expect(duration).toBeGreaterThan(1.5); + expect(fs.existsSync(path.join(mediaDir, ".downloads", item.id))).toBe(false); + } finally { queue.stop(); db.close(); } + }); + + it("keeps unreadable recorded data for manual recovery instead of deleting it", async () => { + const { db, plugins, mediaDir, source } = await setup(); + scan(db, source); + const [item] = liveItems(db); + db.setItemStatus(item.id, "stopping"); + const staging = path.join(mediaDir, ".downloads", item.id); fs.mkdirSync(staging, { recursive: true }); + fs.writeFileSync(path.join(staging, "recording.mp4"), "not a video"); + const queue = new DownloadQueue(db, plugins, mediaDir); + try { + await queue.start(); + expect(db.getItem(item.id)).toMatchObject({ status: "failed" }); + expect(db.getItem(item.id)?.error).toContain(".recording-recovery"); + expect(fs.readFileSync(path.join(mediaDir, ".recording-recovery", item.id, "recording.mp4"), "utf8")).toBe("not a video"); + } finally { queue.stop(); db.close(); } + }); + + it("finds recorder parts first and otherwise the largest partial file", () => { + const root = temp("easyx-recorded-input"); + expect(recordedInput(root)).toBeUndefined(); + fs.writeFileSync(path.join(root, "small.mp4"), "1"); fs.writeFileSync(path.join(root, "large.mp4.part"), "12345"); fs.writeFileSync(path.join(root, "notes.txt"), "123456789"); + expect(recordedInput(root)).toEqual({ file: path.join(root, "large.mp4.part") }); + fs.mkdirSync(path.join(root, "live-parts")); fs.writeFileSync(path.join(root, "live-parts", "part-002.ts"), "b"); fs.writeFileSync(path.join(root, "live-parts", "part-001.ts"), "a"); + fs.writeFileSync(path.join(root, "live-parts", "part-003.ts"), ""); + expect(recordedInput(root)).toEqual({ parts: [path.join(root, "live-parts", "part-001.ts"), path.join(root, "live-parts", "part-002.ts")] }); + }); +}); diff --git a/server/source-sync.ts b/server/source-sync.ts index 656b942..5afe1f4 100644 --- a/server/source-sync.ts +++ b/server/source-sync.ts @@ -26,6 +26,9 @@ export class SourceSync { if (!current || current.profileUrl !== source.profileUrl || current.scraperPluginId !== source.scraperPluginId) { throw Object.assign(new Error("Source changed during the scan; run it again"), { statusCode: 409 }); } + // A hard refresh is an explicit request to reconsider everything, including a + // live room whose recording was stopped or cancelled while it stayed online. + if (hardRefresh) this.db.resumeLiveRecordings(sourceId); const scanCandidates = this.db.liveScanCandidates(current, candidates); const dates: string[] = []; const result = hardRefresh From 8ffd5b83fe1cb4866e6b238091fbe4850bd17750 Mon Sep 17 00:00:00 2001 From: MajorLOL <56225967+MajorLOL@users.noreply.github.com> Date: Thu, 15 Oct 2026 15:36:38 +0200 Subject: [PATCH 13/14] Fix issue #33: Stripchat favorites, restarts after stop/cancel, stuck cancels and lost recordings - Stripchat favorites: when the direct API is refused (HTTP 403/418), read the followed list inside a real Stripchat page with the saved session (easyx-browser-fetch --stripchat-favorites). Local favorites are kept on failure. - Stop and save no longer blocks the room: a performer who stays online is recorded again as a new video. Cancel still blocks until offline; a hard refresh of the source now lifts it. - A cancel the recording process ignores is forced (SIGKILL) after 10 s; a stop that hangs gets a second SIGINT after 45 s. - Shutdown saves running live recordings first (EASYX_SHUTDOWN_GRACE_SECONDS, default 60; compose stop_grace_period 90s). Recordings cut off by a crash or a killed container are joined from their parts on the next start; unreadable data is kept in media/.recording-recovery. - Tests: server/live-restart.test.ts and Stripchat favorites fallback tests. --- scripts/easyx-browser-fetch.py | 126 ++++++++++++++++++++++++++++++++- 1 file changed, 124 insertions(+), 2 deletions(-) diff --git a/scripts/easyx-browser-fetch.py b/scripts/easyx-browser-fetch.py index 58c6fd6..fbe4380 100644 --- a/scripts/easyx-browser-fetch.py +++ b/scripts/easyx-browser-fetch.py @@ -456,15 +456,137 @@ def stripchat_favorite(url: str, cookies_file: str, model_id: int, follow: bool) process.wait(timeout=5) +STRIPCHAT_FAVORITES_SCRIPT = r"""(async () => { + const state = (() => { try { return (window.getState && window.getState()) || window.__PRELOADED_STATE__ || null; } catch { return window.__PRELOADED_STATE__ || null; } })(); + const userId = Number(state?.userSession?.currentUser?.id || 0); + if (!userId) return { ready: Boolean(state), loggedIn: false }; + const frontVersion = String(state?.config?.releaseVersion || state?.configV3?.releaseVersion || ""); + const headers = { accept: "application/json", ...(frontVersion ? { "front-version": frontVersion } : {}) }; + const get = async (path) => { + const response = await fetch(path, { credentials: "include", headers }); + if (!response.ok) throw new Error(`HTTP ${response.status} for ${path.split("?")[0]}`); + return response.json(); + }; + const favorites = await get(`/api/front/users/${userId}/favorites`); + const modelIds = Array.isArray(favorites?.modelIds) ? favorites.modelIds.map(Number) : null; + if (!modelIds) throw new Error("Stripchat returned no followed ID list"); + const models = []; + for (let offset = 0; offset < modelIds.length; offset += 100) { + const chunk = modelIds.slice(offset, offset + 100); + const page = await get(`/api/front/models/list?modelIds=${encodeURIComponent(chunk.join(","))}`); + if (Array.isArray(page?.models)) models.push(...page.models); + } + return { ready: true, loggedIn: true, userId, modelIds, models }; +})()""" + + +async def _stripchat_favorites_cdp(websocket_url: str, cookies: list[dict[str, object]]) -> dict[str, object]: + """Read the signed-in account's followed models from inside a real Stripchat page. + + The site refuses direct API requests from servers (HTTP 403/418), but its own + page may call the same endpoints with the browser session. + """ + message_id = 0 + async with connect(websocket_url, origin=None, max_size=64 * 1024 * 1024) as websocket: + pending: dict[int, asyncio.Future] = {} + + async def receive_messages() -> None: + while True: + message = json.loads(await websocket.recv()) + response_id = message.get("id") + if isinstance(response_id, int) and response_id in pending: + future = pending.pop(response_id) + if not future.done(): + future.set_result(message) + + async def command(method: str, params: dict[str, object] | None = None, timeout: float = 15) -> dict[str, object]: + nonlocal message_id + message_id += 1 + future = asyncio.get_running_loop().create_future() + pending[message_id] = future + await websocket.send(json.dumps({"id": message_id, "method": method, "params": params or {}})) + try: + return await asyncio.wait_for(future, timeout=timeout) + finally: + pending.pop(message_id, None) + + receiver = asyncio.create_task(receive_messages()) + try: + await command("Network.enable") + await command("Network.setCookies", {"cookies": cookies}) + await command("Page.enable") + await command("Page.navigate", {"url": os.environ.get("EASYX_STRIPCHAT_FAVORITES_URL", "https://stripchat.com/favorites")}) + last: dict[str, object] = {} + deadline = time.monotonic() + 40 + while time.monotonic() < deadline: + response = await command("Runtime.evaluate", {"expression": STRIPCHAT_FAVORITES_SCRIPT, "returnByValue": True, "awaitPromise": True}, timeout=45) + result = ((response.get("result") or {}).get("result") or {}) + if result.get("subtype") == "error": + raise RuntimeError(str(result.get("description") or "Stripchat favorites could not be read").splitlines()[0]) + last = result.get("value") if isinstance(result.get("value"), dict) else {} + if last.get("loggedIn"): + return last + await asyncio.sleep(1) + if last.get("ready"): + raise RuntimeError("The Stripchat session is not signed in. Reconnect the account.") + raise RuntimeError("The Stripchat favorites page did not load") + finally: + receiver.cancel() + try: + await receiver + except asyncio.CancelledError: + pass + + +def stripchat_favorites(cookies_file: str) -> str: + executable = next((value for value in (shutil.which("chromium"), shutil.which("chromium-browser"), shutil.which("google-chrome")) if value), None) + if not executable: + raise RuntimeError("Chromium is not installed") + cookies = _netscape_cookies(cookies_file) + if not cookies: + raise RuntimeError("The Stripchat session file contains no cookies") + port = _free_port() + with tempfile.TemporaryDirectory(prefix="easyx-stripchat-favorites-", ignore_cleanup_errors=True) as profile: + process = subprocess.Popen([ + executable, "--headless=new", "--no-sandbox", "--disable-dev-shm-usage", "--disable-gpu", + "--remote-allow-origins=*", f"--remote-debugging-port={port}", f"--user-data-dir={profile}", "about:blank", + ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, env={**os.environ, "LANG": "en_US.UTF-8"}) + try: + page = None + deadline = time.monotonic() + 10 + while time.monotonic() < deadline: + try: + pages = requests.get(f"http://127.0.0.1:{port}/json/list", timeout=1).json() + page = next((item for item in pages if item.get("type") == "page"), None) + if page: + break + except Exception: + time.sleep(0.1) + if not page or not page.get("webSocketDebuggerUrl"): + raise RuntimeError("Chromium debugging page did not start") + return json.dumps(asyncio.run(_stripchat_favorites_cdp(page["webSocketDebuggerUrl"], cookies)), ensure_ascii=False) + finally: + process.terminate() + try: + process.wait(timeout=5) + except subprocess.TimeoutExpired: + process.kill() + process.wait(timeout=5) + + def main() -> int: render = len(sys.argv) == 3 and sys.argv[1] == "--render" mfc_models = len(sys.argv) in (3, 4) and sys.argv[1] == "--mfc-models" capture_media = len(sys.argv) in (3, 4) and sys.argv[1] == "--capture-media" stripchat_favorite_action = len(sys.argv) == 6 and sys.argv[1] == "--stripchat-favorite" - if not render and not mfc_models and not capture_media and not stripchat_favorite_action and len(sys.argv) != 2: - print("usage: easyx-browser-fetch [--render|--capture-media] URL | --mfc-models PAGE [SEARCH] | --stripchat-favorite URL COOKIES MODEL_ID follow|unfollow", file=sys.stderr) + stripchat_favorites_list = len(sys.argv) == 3 and sys.argv[1] == "--stripchat-favorites" + if not render and not mfc_models and not capture_media and not stripchat_favorite_action and not stripchat_favorites_list and len(sys.argv) != 2: + print("usage: easyx-browser-fetch [--render|--capture-media] URL | --mfc-models PAGE [SEARCH] | --stripchat-favorite URL COOKIES MODEL_ID follow|unfollow | --stripchat-favorites COOKIES", file=sys.stderr) return 2 try: + if stripchat_favorites_list: + sys.stdout.write(stripchat_favorites(sys.argv[2])) + return 0 if stripchat_favorite_action: if sys.argv[5] not in {"follow", "unfollow"}: raise RuntimeError("Stripchat favorite action must be follow or unfollow") From 655790dbb66db0563450676c01e18909a714e9e0 Mon Sep 17 00:00:00 2001 From: MajorLOL <56225967+MajorLOL@users.noreply.github.com> Date: Thu, 15 Oct 2026 15:37:11 +0200 Subject: [PATCH 14/14] Fix issue #33: Stripchat favorites, restarts after stop/cancel, stuck cancels and lost recordings - Stripchat favorites: when the direct API is refused (HTTP 403/418), read the followed list inside a real Stripchat page with the saved session (easyx-browser-fetch --stripchat-favorites). Local favorites are kept on failure. - Stop and save no longer blocks the room: a performer who stays online is recorded again as a new video. Cancel still blocks until offline; a hard refresh of the source now lifts it. - A cancel the recording process ignores is forced (SIGKILL) after 10 s; a stop that hangs gets a second SIGINT after 45 s. - Shutdown saves running live recordings first (EASYX_SHUTDOWN_GRACE_SECONDS, default 60; compose stop_grace_period 90s). Recordings cut off by a crash or a killed container are joined from their parts on the next start; unreadable data is kept in media/.recording-recovery. - Tests: server/live-restart.test.ts and Stripchat favorites fallback tests. --- plugins/stripchat/index.test.ts | 37 +++++++++++++++++++++ plugins/stripchat/index.ts | 57 +++++++++++++++++++++++++-------- 2 files changed, 80 insertions(+), 14 deletions(-) diff --git a/plugins/stripchat/index.test.ts b/plugins/stripchat/index.test.ts index f54534f..ec93dc0 100644 --- a/plugins/stripchat/index.test.ts +++ b/plugins/stripchat/index.test.ts @@ -142,6 +142,43 @@ describe("Stripchat account favorites", () => { expect(fetch.mock.calls[3][0].toString()).toContain("/api/front/models/list?modelIds=11%2C12"); }); + it("reads the followed list through the browser session when Stripchat refuses the direct API", async () => { + const fetch = vi.fn() + .mockResolvedValueOnce(new Response(modernAccountPage())) + .mockResolvedValueOnce(new Response(JSON.stringify({ initialDynamic: { user: { id: 77, username: "viewer" } } }))) + .mockResolvedValueOnce(new Response("", { status: 418 })); + const cookiesFile = stripchatSession(); + const runCommand = vi.fn(async () => ({ exitCode: 0, stderr: "", stdout: JSON.stringify({ loggedIn: true, userId: 77, modelIds: [11, 12], models: [ + { id: 11, username: "Alice", status: "public", viewersCount: 42 }, + { id: 12, username: "Bob", status: "offline" }, + ] }) })); + const mock = { config: { cookiesFile }, fetch, log: vi.fn(), runCommand } as unknown as PluginContext; + + await expect(stripchatFollowedSnapshot(mock)).resolves.toEqual({ + authoritative: true, + cams: [expect.objectContaining({ username: "Alice", online: true }), expect.objectContaining({ username: "Bob", online: false })], + }); + expect(runCommand).toHaveBeenCalledWith("easyx-browser-fetch", ["--stripchat-favorites", cookiesFile], expect.any(Object)); + }); + + it("keeps local favorites and reports both problems when the browser session also fails", async () => { + const fetch = vi.fn(async () => new Response("", { status: 403 })); + const runCommand = vi.fn(async () => ({ exitCode: 1, stdout: "", stderr: "Browser-compatible request failed: The Stripchat session is not signed in. Reconnect the account." })); + const mock = { config: { cookiesFile: stripchatSession() }, fetch, log: vi.fn(), runCommand } as unknown as PluginContext; + + const snapshot = await stripchatFollowedSnapshot(mock); + expect(snapshot).toMatchObject({ authoritative: false, cams: [] }); + expect(snapshot.skippedReason).toContain("HTTP 403"); + expect(snapshot.skippedReason).toContain("not signed in"); + }); + + it("never treats a malformed browser answer as an empty followed list", async () => { + const fetch = vi.fn(async () => new Response("", { status: 403 })); + const runCommand = vi.fn(async () => ({ exitCode: 0, stderr: "", stdout: JSON.stringify({ loggedIn: true, userId: 77, models: [] }) })); + const mock = { config: { cookiesFile: stripchatSession() }, fetch, log: vi.fn(), runCommand } as unknown as PluginContext; + await expect(stripchatFollowedSnapshot(mock)).resolves.toMatchObject({ authoritative: false, cams: [] }); + }); + it("mirrors a follow to Stripchat and verifies it in the remote favorite lists", async () => { const fetch = vi.fn() .mockResolvedValueOnce(new Response(accountPage())) diff --git a/plugins/stripchat/index.ts b/plugins/stripchat/index.ts index c73d94a..3b8060c 100644 --- a/plugins/stripchat/index.ts +++ b/plugins/stripchat/index.ts @@ -116,14 +116,7 @@ async function stripchatApi(context: PluginContext, account: StripchatAccount, m async function stripchatFavoriteIds(context: PluginContext, account: StripchatAccount): Promise { const payload = await stripchatApi(context, account, "GET", `/users/${account.userId}/favorites`); - const rawIds = record(payload)?.modelIds; - if (!Array.isArray(rawIds)) throw new Error("Stripchat returned an invalid followed ID list"); - const ids = rawIds.map(Number); - if (ids.some((id) => !Number.isInteger(id) || id <= 0)) throw new Error("Stripchat returned an invalid followed model ID"); - const unique = [...new Set(ids)]; - if (unique.length !== ids.length) throw new Error("Stripchat returned duplicate followed model IDs"); - if (unique.length > MAX_FAVORITES) throw new Error("The Stripchat followed list exceeded its safety limit"); - return unique; + return followedIdList(record(payload)?.modelIds); } async function favoriteSnapshot(context: PluginContext, account: StripchatAccount): Promise { @@ -139,14 +132,50 @@ async function favoriteSnapshot(context: PluginContext, account: StripchatAccoun return { cams: [...unique.values()], authoritative: true }; } +/** Validated followed IDs; throws on anything that is not a complete, plausible list. */ +function followedIdList(rawIds: unknown): number[] { + if (!Array.isArray(rawIds)) throw new Error("Stripchat returned an invalid followed ID list"); + const ids = rawIds.map(Number); + if (ids.some((id) => !Number.isInteger(id) || id <= 0)) throw new Error("Stripchat returned an invalid followed model ID"); + const unique = [...new Set(ids)]; + if (unique.length !== ids.length) throw new Error("Stripchat returned duplicate followed model IDs"); + if (unique.length > MAX_FAVORITES) throw new Error("The Stripchat followed list exceeded its safety limit"); + return unique; +} + +/** + * Stripchat refuses direct API requests from servers more and more often (HTTP 403 + * or 418). The browser helper then reads the same followed list from inside a real + * Stripchat page, signed in with the imported session. + */ +async function browserFavoriteSnapshot(context: PluginContext): Promise { + const cookiesFile = typeof context.config.cookiesFile === "string" ? context.config.cookiesFile.trim() : ""; + if (!cookiesFile) throw new Error("The Stripchat browser session is unavailable"); + const result = await context.runCommand("easyx-browser-fetch", ["--stripchat-favorites", cookiesFile], { timeoutMs: 90_000, maxOutputBytes: 32 * 1024 * 1024 }); + if (result.exitCode !== 0) throw new Error(result.stderr.replace(/^Browser-compatible request failed:\s*/i, "").trim() || "Stripchat favorites could not be read in the browser"); + let payload: unknown; + try { payload = JSON.parse(result.stdout); } catch { throw new Error("Stripchat returned an invalid favorites response"); } + const value = record(payload); + followedIdList(value?.modelIds); + if (!Array.isArray(value?.models)) throw new Error("Stripchat returned no followed model details"); + const unique = new Map(); + for (const cam of stripchatFavoriteCams({ models: value.models })) unique.set(cam.username.toLowerCase(), cam); + // Same trust as the direct API: every chunk was answered, so the list is complete. + return { cams: [...unique.values()], authoritative: true }; +} + export async function stripchatFollowedSnapshot(context: PluginContext): Promise { - let account: StripchatAccount | undefined; - try { account = await stripchatAccount(context); } - catch (error) { return { cams: [], authoritative: false, skippedReason: error instanceof Error ? error.message : String(error) }; } - if (!account) return { cams: [], authoritative: false, skippedReason: "Connect a Stripchat account to synchronize followed creators." }; - try { return await favoriteSnapshot(context, account); } + let directProblem: string; + try { + const account = await stripchatAccount(context); + if (!account) return { cams: [], authoritative: false, skippedReason: "Connect a Stripchat account to synchronize followed creators." }; + return await favoriteSnapshot(context, account); + } catch (error) { directProblem = error instanceof Error ? error.message : String(error); } + context.log("debug", "Stripchat favorites API refused the direct request; trying the browser session", { reason: directProblem }); + try { return await browserFavoriteSnapshot(context); } catch (error) { - const skippedReason = error instanceof Error ? error.message : String(error); + const browserProblem = error instanceof Error ? error.message : String(error); + const skippedReason = browserProblem === "The Stripchat browser session is unavailable" ? directProblem : `${directProblem} (browser: ${browserProblem})`; context.log("warn", "Stripchat favorite synchronization skipped", { reason: skippedReason }); return { cams: [], authoritative: false, skippedReason }; }