Skip to content

Commit

Permalink
fix: add more filters for hls manifest, fix relative uri resolving, a…
Browse files Browse the repository at this point in the history
…dd video track type
  • Loading branch information
vitalygashkov committed Aug 6, 2024
1 parent b7ea233 commit 3f49cd1
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions lib/hls.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
'use strict';

const { dirname } = require('node:path');
const m3u8Parser = require('m3u8-parser');
const { parseBitrate, getQualityLabel } = require('./util');
const {
createResolutionFilter,
createVideoQualityFilter,
createAudioLanguageFilter,
createSubtitleLanguageFilter,
createVideoCodecFilter,
createAudioCodecFilter,
createAudioChannelsFilter,
} = require('./track');

const parseM3u8 = (manifestString) => {
Expand All @@ -22,9 +26,9 @@ const fetchPlaylist = async (url) =>
.then(parseM3u8);

const parseUrl = (playlistUri, manifestUri) => {
if (playlistUri.includes('https://')) return playlistUri;
const uri = new URL(manifestUri);
return uri.origin + playlistUri;
let value = playlistUri;
if (!value.startsWith('https://')) value = new URL(value, manifestUri).toString();
return value;
};

const urlsSame = (url1, url2) => {
Expand Down Expand Up @@ -63,6 +67,7 @@ const getVideoPlaylists = (m3u8, manifestUri) => {
const bandwidth = data.attributes.BANDWIDTH;
const url = data.resolvedUri || parseUrl(data.uri, manifestUri);
const track = { bitrate: parseBitrate(bandwidth), url };
track.type = 'video';
if (data.attributes.RESOLUTION) {
track.resolution = data.attributes.RESOLUTION;
track.quality = getQualityLabel(track.resolution);
Expand All @@ -73,13 +78,20 @@ const getVideoPlaylists = (m3u8, manifestUri) => {
});
};

const segmentsDto = (data = []) => {
const mapSegment = (item) => ({
url: item.resolvedUri || item.uri,
duration: item.duration,
number: item.number,
presentationTime: item.presentationTime,
});
const segmentsDto = (data = [], track) => {
const mapSegment = (item) => {
let url = item.resolvedUri || item.uri;
if (!url.startsWith('https://')) {
const baseUrl = dirname(track.url) + '/';
url = new URL(url, baseUrl).toString();
}
return {
url,
duration: item.duration,
number: item.number,
presentationTime: item.presentationTime,
};
};
const segments = data.map(mapSegment);
if (data.length && data[0].map?.resolvedUri)
segments.unshift({
Expand All @@ -96,7 +108,7 @@ const fetchTrackSegments = (tracks) => {
return Promise.all(
tracks.map(async (track) => {
const playlist = await fetchPlaylist(track.url);
track.segments = segmentsDto(playlist.segments);
track.segments = segmentsDto(playlist.segments, track);
if (playlist.contentProtection) {
track.protection = {};
const fairplayLegacy = playlist.contentProtection['com.apple.fps.1_0'];
Expand Down Expand Up @@ -130,8 +142,11 @@ const parseManifest = async (manifestString, manifestUri) => {
audios,
subtitles,
withResolution: createResolutionFilter(videos),
withVideoCodecs: createVideoCodecFilter(videos),
withVideoQuality: createVideoQualityFilter(videos),
withAudioCodecs: createAudioCodecFilter(audios),
withAudioLanguages: createAudioLanguageFilter(audios),
withAudioChannels: createAudioChannelsFilter(audios),
withSubtitleLanguages: createSubtitleLanguageFilter(subtitles),
},
};
Expand Down

0 comments on commit 3f49cd1

Please sign in to comment.