Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
2 changes: 2 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions plugins/bongacams/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
23 changes: 9 additions & 14 deletions plugins/bongacams/index.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -121,31 +122,25 @@ async function fetchText(context: PluginContext, url: string): Promise<string |
} catch (error) { context.signal?.throwIfAborted(); throw error; }
}

type Variant = { url: string; bandwidth: number };
export function variantUrls(manifest: string, baseUrl: string): Variant[] {
type Variant = { url: string; bandwidth: number; height: number };
/** Variants in recording preference order: highest bandwidth, or the best at or below maxHeight. */
export function variantUrls(manifest: string, baseUrl: string, maxHeight = 0): Variant[] {
if (!manifest.trimStart().startsWith("#EXTM3U")) return [];
const variants: Variant[] = []; 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 = "";
}
const variants = parseHlsVariants(manifest, baseUrl);
// A media playlist (no variants) with segments is itself playable.
if (!variants.length && /#EXTINF/i.test(manifest)) variants.push({ url: baseUrl, bandwidth: 0 });
return variants.sort((left, right) => 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<string | undefined> {
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;
Expand Down
11 changes: 10 additions & 1 deletion plugins/cam4/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];

Expand Down Expand Up @@ -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");
});
});
17 changes: 5 additions & 12 deletions plugins/cam4/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -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 };
}
Expand Down
3 changes: 2 additions & 1 deletion plugins/chaturbate/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand Down
49 changes: 49 additions & 0 deletions plugins/hls-quality.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
59 changes: 59 additions & 0 deletions plugins/hls-quality.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown> | 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<T extends { bandwidth: number; height: number }>(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;
}
3 changes: 2 additions & 1 deletion plugins/live-cam-plugin-factory.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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) }); },
});
}
Loading
Loading