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

@@ -53,7 +53,7 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({ data }) => {
const fetchVideo = async () => { const fetchVideo = async () => {
if (!data) return null; if (!data) return null;
const { id, type } = data; const { id, type } = data;
const media = await fetchMediaDetails(id, type); const media = await fetchMediaDetails(id, type).catch(() => null);
if (!media) return null; if (!media) return null;
const { result } = media; const { result } = media;
@@ -72,7 +72,10 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({ data }) => {
episode, episode,
); );
const stream = await getVideoStream(scrapeMedia); const stream = await getVideoStream({
media: scrapeMedia,
forceVTT: true,
}).catch(() => null);
if (!stream) { if (!stream) {
await ScreenOrientation.lockAsync( await ScreenOrientation.lockAsync(
ScreenOrientation.OrientationLock.PORTRAIT_UP, ScreenOrientation.OrientationLock.PORTRAIT_UP,

View File

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

View File

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

7
pnpm-lock.yaml generated
View File

@@ -170,6 +170,9 @@ importers:
'@movie-web/providers': '@movie-web/providers':
specifier: ^2.2.0 specifier: ^2.2.0
version: 2.2.0 version: 2.2.0
srt-webvtt:
specifier: ^2.0.0
version: 2.0.0
tmdb-ts: tmdb-ts:
specifier: ^1.6.1 specifier: ^1.6.1
version: 1.6.1 version: 1.6.1
@@ -9741,6 +9744,10 @@ packages:
resolution: {integrity: sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==} resolution: {integrity: sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==}
dev: false dev: false
/srt-webvtt@2.0.0:
resolution: {integrity: sha512-G2Z7/Jf2NRKrmLYNSIhSYZZYE6OFlKXFp9Au2/zJBKgrioUzmrAys1x7GT01dwl6d2sEnqr5uahEIOd0JW/Rbw==}
dev: false
/ssri@8.0.1: /ssri@8.0.1:
resolution: {integrity: sha512-97qShzy1AiyxvPNIkLWoGua7xoQzzPjQ0HAH4B0rWKo7SZ6USuPcrUiAFrws0UH8RrbWmgq3LMTObhPIHbbBeQ==} resolution: {integrity: sha512-97qShzy1AiyxvPNIkLWoGua7xoQzzPjQ0HAH4B0rWKo7SZ6USuPcrUiAFrws0UH8RrbWmgq3LMTObhPIHbbBeQ==}
engines: {node: '>= 8'} engines: {node: '>= 8'}