dasha 2.1.0 → 2.2.0

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.
package/lib/manifest.js CHANGED
@@ -5,6 +5,14 @@ const { processManifest } = require('./processor');
5
5
  const { getQualityLabel } = require('./utils');
6
6
  const { CONTENT_TYPE, KEY_SYSTEMS } = require('./constants');
7
7
 
8
+ const isUrl = (value) => {
9
+ try {
10
+ return !!new URL(value);
11
+ } catch (e) {
12
+ return false;
13
+ }
14
+ };
15
+
8
16
  class Manifest {
9
17
  constructor(manifest) {
10
18
  for (const key of Object.keys(manifest)) this[key] = manifest[key];
@@ -25,6 +33,7 @@ class Manifest {
25
33
  const representation = this.findBestRepresentation(matches.length ? matches : representations);
26
34
  const adaptationSet = adaptationSets.find((a) => a.representations.includes(representation));
27
35
  return {
36
+ id: 0,
28
37
  type: CONTENT_TYPE.video,
29
38
  pssh: this.getWidevinePssh(adaptationSet),
30
39
  licenseUrl: this.getWidevineLicenseUrl(adaptationSet),
@@ -39,10 +48,9 @@ class Manifest {
39
48
 
40
49
  getAudioTracks(languages) {
41
50
  const isAudioAdaptationSet = (adaptationSet) =>
42
- adaptationSet.contentType === CONTENT_TYPE.audio ||
43
- adaptationSet.mimeType === 'audio/mp4' ||
44
- !adaptationSet.maxWidth ||
45
- !adaptationSet.maxHeight;
51
+ (adaptationSet.contentType === CONTENT_TYPE.audio ||
52
+ adaptationSet.mimeType === 'audio/mp4') &&
53
+ !adaptationSet.maxWidth;
46
54
  const adaptationSets = this.periods
47
55
  .map((p) => p.adaptationSets)
48
56
  .flat(1)
@@ -50,16 +58,16 @@ class Manifest {
50
58
 
51
59
  const representations = adaptationSets.map((a) => a.representations).flat(2);
52
60
  const matches = representations.filter((r) => languages?.some((lang) => r.lang.includes(lang)));
53
- // TODO: Select all audio tracks if they with different langs but same bandwidth
54
- const selectedRepresentations = matches?.length
55
- ? matches
56
- : [this.findBestRepresentation(representations)];
61
+ const selectedRepresentations = matches?.length ? matches : representations;
57
62
 
58
63
  const tracks = [];
59
64
  for (const representation of selectedRepresentations) {
60
65
  const adaptationSet = adaptationSets.find((a) => a.representations.includes(representation));
61
66
  const track = {
67
+ id: tracks.length,
62
68
  type: CONTENT_TYPE.audio,
69
+ label: adaptationSet.label,
70
+ language: adaptationSet.lang,
63
71
  pssh: this.getWidevinePssh(adaptationSet),
64
72
  licenseUrl: this.getWidevineLicenseUrl(adaptationSet),
65
73
  segments: this.getSegments(adaptationSet, representation),
@@ -74,19 +82,53 @@ class Manifest {
74
82
  }
75
83
 
76
84
  getSubtitleTracks(languages) {
77
- // TODO: Return subtitle tracks
85
+ const isSubtitleAdaptationSet = (adaptationSet) =>
86
+ (adaptationSet.contentType === CONTENT_TYPE.text ||
87
+ adaptationSet.mimeType.includes('text')) &&
88
+ !adaptationSet.maxWidth;
89
+ const adaptationSets = this.periods
90
+ .map((p) => p.adaptationSets)
91
+ .flat(1)
92
+ .filter(isSubtitleAdaptationSet);
93
+
94
+ const representations = adaptationSets.map((a) => a.representations).flat(2);
95
+ const matches = representations.filter((r) => languages?.some((lang) => r.lang.includes(lang)));
96
+ const selectedRepresentations = matches?.length ? matches : representations;
97
+
98
+ const tracks = [];
99
+ for (const representation of selectedRepresentations) {
100
+ const adaptationSet = adaptationSets.find((a) => a.representations.includes(representation));
101
+ const baseUrl = this.baseUrls?.[0]?.value;
102
+ const representationBaseUrl = representation.baseUrls?.[0]?.value;
103
+ const url = isUrl(representationBaseUrl)
104
+ ? representationBaseUrl
105
+ : `${baseUrl || ''}${representationBaseUrl || ''}`;
106
+ const track = {
107
+ id: tracks.length,
108
+ type: CONTENT_TYPE.text,
109
+ label: adaptationSet.label,
110
+ language: adaptationSet.lang,
111
+ format: representation.mimeType?.split('/')[1] || adaptationSet.mimeType?.split('/')[1],
112
+ segments: [{ url }],
113
+ bitrate: Math.round(representation.bandwidth / 1000), // Kbps
114
+ size: Math.ceil((representation.bandwidth / 8e6) * this.mediaPresentationDuration), // MB
115
+ };
116
+ tracks.push(track);
117
+ }
118
+
119
+ return tracks;
78
120
  }
79
121
 
80
122
  getWidevinePssh(adaptationSet) {
81
123
  const isWidevineProtection = (protection) =>
82
124
  protection.schemeIdUri === KEY_SYSTEMS['com.widevine.alpha'];
83
- return adaptationSet.contentProtections.find(isWidevineProtection)?.cencPssh || null;
125
+ return adaptationSet?.contentProtections?.find(isWidevineProtection)?.cencPssh || null;
84
126
  }
85
127
 
86
128
  getWidevineLicenseUrl(adaptationSet) {
87
129
  const isWidevineProtection = (protection) =>
88
130
  protection.schemeIdUri === KEY_SYSTEMS['com.widevine.alpha'];
89
- return adaptationSet.contentProtections.find(isWidevineProtection)?.licenseUrl || null;
131
+ return adaptationSet?.contentProtections?.find(isWidevineProtection)?.licenseUrl || null;
90
132
  }
91
133
 
92
134
  findBestRepresentation(representations) {
package/lib/processor.js CHANGED
@@ -58,6 +58,8 @@ const processAdaptationSet = (adaptationSet) => {
58
58
  maxFrameRate: adaptationSet['maxFrameRate'],
59
59
  contentType: adaptationSet['contentType'],
60
60
  segmentTemplate: adaptationSet['SegmentTemplate'],
61
+ mimeType: adaptationSet['mimeType'],
62
+ label: adaptationSet['label'],
61
63
  contentProtections,
62
64
  representations,
63
65
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dasha",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "author": "Vitaliy Gashkov <vitnore@gmail.com>",
5
5
  "description": "Simple MPEG-DASH MPD parser",
6
6
  "license": "Apache-2.0",