feat: allow specific source id in getVideoStream

This commit is contained in:
Adrian Castro
2024-02-18 11:10:19 +01:00
parent a63bee2923
commit 68ec709c51

View File

@@ -3,6 +3,7 @@ import hls from "parse-hls";
import { default as toWebVTT } from "srt-webvtt"; import { default as toWebVTT } from "srt-webvtt";
import type { import type {
EmbedOutput,
FileBasedStream, FileBasedStream,
Qualities, Qualities,
RunnerOptions, RunnerOptions,
@@ -44,10 +45,12 @@ export type RunnerEvent =
| DiscoverEmbedsEvent; | DiscoverEmbedsEvent;
export async function getVideoStream({ export async function getVideoStream({
sourceId,
media, media,
forceVTT, forceVTT,
onEvent, onEvent,
}: { }: {
sourceId?: string;
media: ScrapeMedia; media: ScrapeMedia;
forceVTT?: boolean; forceVTT?: boolean;
onEvent?: (event: RunnerEvent) => void; onEvent?: (event: RunnerEvent) => void;
@@ -68,12 +71,49 @@ export async function getVideoStream({
}, },
}; };
const result = await providers.runAll(options); let stream: Stream | null = null;
if (!result) return null;
if (sourceId) {
let embedOutput: EmbedOutput | undefined;
const sourceResult = await providers
.runSourceScraper({
id: sourceId,
media,
})
.catch(() => undefined);
if (sourceResult) {
for (const embed of sourceResult.embeds) {
const embedResult = await providers
.runEmbedScraper({
id: embed.embedId,
url: embed.url,
})
.catch(() => undefined);
if (embedResult) {
embedOutput = embedResult;
}
}
}
if (embedOutput) {
stream = embedOutput.stream[0] ?? null;
} else if (sourceResult) {
stream = sourceResult.stream?.[0] ?? null;
}
} else {
stream = await providers
.runAll(options)
.then((result) => result?.stream ?? null);
}
if (!stream) return null;
if (forceVTT) { if (forceVTT) {
if (result.stream.captions && result.stream.captions.length > 0) { if (stream.captions && stream.captions.length > 0) {
for (const caption of result.stream.captions) { for (const caption of stream.captions) {
if (caption.type === "srt") { if (caption.type === "srt") {
const response = await fetch(caption.url); const response = await fetch(caption.url);
const srtSubtitle = await response.blob(); const srtSubtitle = await response.blob();
@@ -84,7 +124,7 @@ export async function getVideoStream({
} }
} }
} }
return result.stream; return stream;
} }
export function findHighestQuality( export function findHighestQuality(