dasha 3.1.6 → 3.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/lib/hls.js +17 -24
  2. package/lib/track.js +10 -22
  3. package/package.json +1 -1
package/lib/hls.js CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  const { dirname, basename } = require('node:path');
4
4
  const m3u8Parser = require('m3u8-parser');
5
- const { parseBitrate, getQualityLabel } = require('./util');
6
5
  const {
7
6
  createResolutionFilter,
8
7
  createVideoQualityFilter,
@@ -24,8 +23,7 @@ const parseM3u8 = (manifestString) => {
24
23
 
25
24
  const fetchPlaylist = async (url) => {
26
25
  const response = await fetch(url);
27
- if (!response.ok)
28
- throw new Error(`Failed to fetch playlist (${response.status}): ${url}`);
26
+ if (!response.ok) throw new Error(`Failed to fetch playlist (${response.status}): ${url}`);
29
27
  const text = await response.text();
30
28
  return parseM3u8(text);
31
29
  };
@@ -33,8 +31,7 @@ const fetchPlaylist = async (url) => {
33
31
  const parseUrl = (playlistUri, manifestUri) => {
34
32
  let value = playlistUri;
35
33
  if (!value.startsWith('https://'))
36
- value =
37
- new URL(value, manifestUri).toString() + new URL(manifestUri).search;
34
+ value = new URL(value, manifestUri).toString() + new URL(manifestUri).search;
38
35
  return value;
39
36
  };
40
37
 
@@ -77,23 +74,21 @@ const getSubtitlePlaylists = (m3u8, manifestUri) => {
77
74
  const getVideoPlaylists = (m3u8, manifestUri) => {
78
75
  if (!m3u8.playlists) return [];
79
76
  return m3u8.playlists.map((data) => {
80
- const bandwidth = data.attributes?.BANDWIDTH;
81
- const url = data.resolvedUri || parseUrl(data.uri, manifestUri);
82
- const track = {
77
+ const track = createVideoTrack({
83
78
  id: data.uri.replace('/', ''),
84
- bitrate: parseBitrate(bandwidth),
85
- url,
86
- };
87
- track.type = 'video';
88
- if (data.attributes.RESOLUTION) {
89
- track.resolution = data.attributes.RESOLUTION;
90
- track.quality = getQualityLabel(track.resolution);
91
- }
92
- if (data.attributes['VIDEO-RANGE'])
93
- track.dynamicRange = data.attributes['VIDEO-RANGE'];
94
- if (data.attributes.CODECS) track.codecs = data.attributes.CODECS;
95
- if (data.attributes['FRAME-RATE'])
96
- track.frameRate = data.attributes['FRAME-RATE'];
79
+ label: data.attributes['NAME'],
80
+ type: 'video',
81
+ codec: data.attributes.CODECS,
82
+ dynamicRange: data.attributes['VIDEO-RANGE'],
83
+ bitrate: data.attributes?.BANDWIDTH,
84
+ width: data.attributes.RESOLUTION?.width,
85
+ height: data.attributes.RESOLUTION?.height,
86
+ fps: data.attributes['FRAME-RATE'],
87
+ language: data.attributes['LANGUAGE'],
88
+ });
89
+
90
+ track.url = data.resolvedUri || parseUrl(data.uri, manifestUri);
91
+
97
92
  return track;
98
93
  });
99
94
  };
@@ -168,9 +163,7 @@ const parseManifest = async (manifestString, manifestUri) => {
168
163
  // TODO: Handle audio-only manifests
169
164
  const { pathname } = new URL(manifestUri);
170
165
  const isAudio =
171
- pathname.includes('.m4a') ||
172
- pathname.includes('.mp3') ||
173
- pathname.includes('.opus');
166
+ pathname.includes('.m4a') || pathname.includes('.mp3') || pathname.includes('.opus');
174
167
  if (isAudio) {
175
168
  const track = createAudioTrack({
176
169
  id: 'audio' + basename(pathname),
package/lib/track.js CHANGED
@@ -11,9 +11,7 @@ const parseMimes = (codecs) =>
11
11
  const createResolutionFilter = (videos) => {
12
12
  return ({ width, height }) => {
13
13
  return videos.filter(
14
- (track) =>
15
- (!width || track.width === width) &&
16
- (!height || track.height === height),
14
+ (track) => (!width || track.width === width) && (!height || track.height === height),
17
15
  );
18
16
  };
19
17
  };
@@ -32,11 +30,9 @@ const createVideoCodecFilter = (videos) => createCodecFilter(videos);
32
30
  const createVideoQualityFilter = (videos) => {
33
31
  return (quality) => {
34
32
  if (!quality) return [getBestTrack(videos)];
35
- const trackQuality = String(quality).includes('p')
36
- ? quality
37
- : `${quality}p`;
33
+ const trackQuality = String(quality).includes('p') ? quality : `${quality}p`;
38
34
  const results = videos.filter((track) => track.quality === trackQuality);
39
- results.sort((a, b) => b.bitrate.bps - a.bitrate.bps);
35
+ results.filter((v) => !!v.bitrate).sort((a, b) => b.bitrate.bps - a.bitrate.bps);
40
36
  return results.length ? results : [getBestTrack(videos)];
41
37
  };
42
38
  };
@@ -53,15 +49,13 @@ const createAudioLanguageFilter = (audios) => {
53
49
  if (!alreadyAdded) languages.push(audio.language);
54
50
  }
55
51
  } else {
56
- audios.sort((a, b) => b.bitrate.bps - a.bitrate.bps);
52
+ audios.filter((a) => !!a.bitrate).sort((a, b) => b.bitrate.bps - a.bitrate.bps);
57
53
  return audios.slice(0, maxTracksPerLanguage);
58
54
  }
59
55
  }
60
56
  const filtered = [];
61
57
  for (const language of languages) {
62
- const tracks = audios.filter((track) =>
63
- track.language?.startsWith(language),
64
- );
58
+ const tracks = audios.filter((track) => track.language?.startsWith(language));
65
59
  filtered.push(...tracks);
66
60
  }
67
61
  const results = [];
@@ -81,8 +75,7 @@ const createAudioLanguageFilter = (audios) => {
81
75
  const createAudioChannelsFilter = (audios) => {
82
76
  return (channels) => {
83
77
  if (!channels) return audios;
84
- const value =
85
- typeof channels === 'string' ? parseFloat(channels) : channels;
78
+ const value = typeof channels === 'string' ? parseFloat(channels) : channels;
86
79
  return audios.filter((track) => track.channels === value);
87
80
  };
88
81
  };
@@ -92,23 +85,18 @@ const createSubtitleLanguageFilter = (subtitles) => {
92
85
  if (!languages.length) return subtitles;
93
86
  return subtitles.filter((track) =>
94
87
  languages.some(
95
- (language) =>
96
- track.language?.startsWith(language) ||
97
- track.label?.startsWith(language),
88
+ (language) => track.language?.startsWith(language) || track.label?.startsWith(language),
98
89
  ),
99
90
  );
100
91
  };
101
92
  };
102
93
 
103
- const filterByResolution = (tracks, resolution) =>
104
- createResolutionFilter(tracks)(resolution);
105
- const filterByQuality = (tracks, quality) =>
106
- createVideoQualityFilter(tracks)(quality);
94
+ const filterByResolution = (tracks, resolution) => createResolutionFilter(tracks)(resolution);
95
+ const filterByQuality = (tracks, quality) => createVideoQualityFilter(tracks)(quality);
107
96
  const filterByCodecs = (tracks, codecs) => createCodecFilter(tracks)(codecs);
108
97
  const filterByLanguages = (tracks, languages, maxTracksPerLanguage) =>
109
98
  createAudioLanguageFilter(tracks)(languages, maxTracksPerLanguage);
110
- const filterByChannels = (tracks, channels) =>
111
- createAudioChannelsFilter(tracks)(channels);
99
+ const filterByChannels = (tracks, channels) => createAudioChannelsFilter(tracks)(channels);
112
100
 
113
101
  module.exports = {
114
102
  parseMimes,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dasha",
3
- "version": "3.1.6",
3
+ "version": "3.1.8",
4
4
  "description": "Streaming manifest parser",
5
5
  "files": [
6
6
  "dasha.js",