feat: add function to parse hls tracks

This commit is contained in:
Adrian Castro
2024-02-15 10:56:48 +01:00
parent 4d754061ea
commit 35a3ab8050
3 changed files with 45 additions and 0 deletions

View File

@@ -18,6 +18,7 @@
"@movie-web/eslint-config": "workspace:^0.2.0",
"@movie-web/prettier-config": "workspace:^0.1.0",
"@movie-web/tsconfig": "workspace:^0.1.0",
"@types/hls-parser": "^0.8.7",
"eslint": "^8.56.0",
"prettier": "^3.1.1",
"typescript": "^5.3.3"
@@ -30,6 +31,7 @@
"prettier": "@movie-web/prettier-config",
"dependencies": {
"@movie-web/providers": "^2.2.0",
"hls-parser": "^0.10.8",
"srt-webvtt": "^2.0.0",
"tmdb-ts": "^1.6.1"
}

View File

@@ -1,3 +1,5 @@
import { parse } from "hls-parser";
import { MasterPlaylist } from "hls-parser/types";
import { default as toWebVTT } from "srt-webvtt";
import type {
@@ -103,3 +105,28 @@ export function findHighestQuality(
}
return undefined;
}
export async function extractTracksFromHLS(
playlistUrl: string,
headers: Record<string, string>,
) {
try {
const response = await fetch(playlistUrl, { headers }).then((res) =>
res.text(),
);
const playlist = parse(response);
if (!playlist.isMasterPlaylist) return null;
if (!(playlist instanceof MasterPlaylist)) return null;
const tracks = playlist.variants.map((variant) => {
return {
video: variant.video,
audio: variant.audio,
subtitles: variant.subtitles,
};
});
return tracks;
} catch (e) {
return null;
}
}