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

@@ -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;
}
}