feat: convert srt to vtt if required

This commit is contained in:
Adrian Castro
2024-02-12 15:26:54 +01:00
parent 3d1a5a88f2
commit 33a62752e2
4 changed files with 39 additions and 8 deletions

View File

@@ -30,6 +30,7 @@
"prettier": "@movie-web/prettier-config",
"dependencies": {
"@movie-web/providers": "^2.2.0",
"srt-webvtt": "^2.0.0",
"tmdb-ts": "^1.6.1"
}
}

View File

@@ -1,3 +1,5 @@
import { default as toWebVTT } from "srt-webvtt";
import type {
FileBasedStream,
Qualities,
@@ -11,9 +13,13 @@ import {
targets,
} from "@movie-web/providers";
export async function getVideoStream(
media: ScrapeMedia,
): Promise<Stream | null> {
export async function getVideoStream({
media,
forceVTT,
}: {
media: ScrapeMedia;
forceVTT?: boolean;
}): Promise<Stream | null> {
const providers = makeProviders({
fetcher: makeStandardFetcher(fetch),
target: targets.NATIVE,
@@ -24,9 +30,23 @@ export async function getVideoStream(
media,
};
const results = await providers.runAll(options);
if (!results) return null;
return results.stream;
const result = await providers.runAll(options);
if (!result) return null;
if (forceVTT) {
if (result.stream.captions && result.stream.captions.length > 0) {
for (const caption of result.stream.captions) {
if (caption.type === "srt") {
const response = await fetch(caption.url);
const srtSubtitle = await response.blob();
const vttSubtitleUrl = await toWebVTT(srtSubtitle);
caption.url = vttSubtitleUrl;
caption.type = "vtt";
}
}
}
}
return result.stream;
}
export function findHighestQuality(