From 68ec709c5178a3874d0465184394ca4abc8fe08b Mon Sep 17 00:00:00 2001 From: Adrian Castro <22133246+castdrian@users.noreply.github.com> Date: Sun, 18 Feb 2024 11:10:19 +0100 Subject: [PATCH] feat: allow specific source id in getVideoStream --- packages/provider-utils/src/video.ts | 50 +++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/packages/provider-utils/src/video.ts b/packages/provider-utils/src/video.ts index 4fd6bb1..9bf34b3 100644 --- a/packages/provider-utils/src/video.ts +++ b/packages/provider-utils/src/video.ts @@ -3,6 +3,7 @@ import hls from "parse-hls"; import { default as toWebVTT } from "srt-webvtt"; import type { + EmbedOutput, FileBasedStream, Qualities, RunnerOptions, @@ -44,10 +45,12 @@ export type RunnerEvent = | DiscoverEmbedsEvent; export async function getVideoStream({ + sourceId, media, forceVTT, onEvent, }: { + sourceId?: string; media: ScrapeMedia; forceVTT?: boolean; onEvent?: (event: RunnerEvent) => void; @@ -68,12 +71,49 @@ export async function getVideoStream({ }, }; - const result = await providers.runAll(options); - if (!result) return null; + let stream: Stream | null = 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 (result.stream.captions && result.stream.captions.length > 0) { - for (const caption of result.stream.captions) { + if (stream.captions && stream.captions.length > 0) { + for (const caption of stream.captions) { if (caption.type === "srt") { const response = await fetch(caption.url); const srtSubtitle = await response.blob(); @@ -84,7 +124,7 @@ export async function getVideoStream({ } } } - return result.stream; + return stream; } export function findHighestQuality(