-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwalk-manifest.js
181 lines (156 loc) · 4.93 KB
/
walk-manifest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var m3u8 = require('m3u8-parser');
var syncRequest = require('sync-request');
var url = require('url');
var path = require('path');
var fs = require('fs');
var debug = require('debug')('hls');
var joinURI = function(absolute, relative) {
var parse = url.parse(absolute);
parse.pathname = path.join(parse.pathname, relative);
return url.format(parse);
};
var isAbsolute = function(uri) {
var parsed = url.parse(uri);
if (parsed.protocol) {
return true;
}
return false;
};
var mediaGroupPlaylists = function(mediaGroups) {
var playlists = [];
['AUDIO', 'VIDEO', 'CLOSED-CAPTIONS', 'SUBTITLES'].forEach(function(type) {
var mediaGroupType = mediaGroups[type];
if (mediaGroupType && !Object.keys(mediaGroupType).length) {
return;
}
for (var group in mediaGroupType) {
for (var item in mediaGroupType[group]) {
var props = mediaGroupType[group][item];
playlists.push(props);
}
}
});
debug('mediaGroupPlaylists:');
debug(playlists);
return playlists;
};
var parseManifest = function(content) {
var parser = new m3u8.Parser();
parser.push(content);
parser.end();
debug('parsed manifest:');
debug(parser.manifest);
return parser.manifest;
};
var parseKey = function(basedir, decrypt, resources, manifest, parent) {
if (!manifest.parsed.segments[0] || !manifest.parsed.segments[0].key) {
return {};
}
var key = manifest.parsed.segments[0].key;
var keyUri = key.uri;
if (!isAbsolute(keyUri)) {
keyUri = joinURI(path.dirname(manifest.uri), keyUri);
}
// if we are not decrypting then we just download the key
if (!decrypt) {
// put keys in parent-dir/key-name.key
key.file = basedir;
if (parent) {
key.file = path.dirname(parent.file);
}
key.file = path.join(key.file, path.basename(key.uri));
manifest.content = new Buffer(manifest.content.toString().replace(
key.uri,
path.relative(path.dirname(manifest.file), key.file)
));
key.uri = keyUri;
resources.push(key);
return key;
}
// get the aes key
var keyContent = syncRequest('GET', keyUri).getBody();
key.bytes = new Uint32Array([
keyContent.readUInt32BE(0),
keyContent.readUInt32BE(4),
keyContent.readUInt32BE(8),
keyContent.readUInt32BE(12)
]);
// remove the key from the manifest
manifest.content = new Buffer(manifest.content.toString().replace(
new RegExp('.*' + key.uri + '.*'),
''
));
return key;
};
var walkPlaylist = function(decrypt, basedir, uri, parent, manifestIndex, playlistFilter) {
var resources = [];
var manifest = {};
manifest.uri = uri;
manifest.file = path.join(basedir, path.basename(uri));
resources.push(manifest);
// if we are not the master playlist
if (parent) {
manifest.file = path.join(
path.dirname(parent.file),
'manifest' + manifestIndex,
path.basename(manifest.file)
);
// get the real uri of this playlist
if (!isAbsolute(manifest.uri)) {
manifest.uri = joinURI(path.dirname(parent.uri), manifest.uri);
}
// replace original uri in file with new file path
parent.content = new Buffer(parent.content.toString().replace(uri, path.relative(path.dirname(parent.file), manifest.file)));
}
manifest.content = syncRequest('GET', manifest.uri).getBody();
debug('manifest.content');
debug(manifest.content);
manifest.parsed = parseManifest(manifest.content);
manifest.parsed.segments = manifest.parsed.segments || [];
manifest.parsed.playlists = manifest.parsed.playlists || [];
manifest.parsed.mediaGroups = manifest.parsed.mediaGroups || {};
var playlists = manifest.parsed.playlists.concat(mediaGroupPlaylists(manifest.parsed.mediaGroups));
var key = parseKey(basedir, decrypt, resources, manifest, parent);
// SEGMENTS
manifest.parsed.segments.forEach(function(s, i) {
if (!s.uri) {
return;
}
// put segments in manifest-name/segment-name.ts
s.file = path.join(path.dirname(manifest.file), path.basename(s.uri));
if (!isAbsolute(s.uri)) {
s.uri = joinURI(path.dirname(manifest.uri), s.uri);
}
if (key) {
s.key = key;
s.key.iv = s.key.iv || new Uint32Array([0, 0, 0, manifest.parsed.mediaSequence, i]);
}
manifest.content = new Buffer(manifest.content.toString().replace(s.uri, path.basename(s.uri)));
resources.push(s);
});
// Pick a SUB playlist:
playlists = playlists.filter(p => p.uri);
var filterFunc = playlistFilter || (function(allPlaylists) {
return [].concat(allPlaylists[0] || []);
});
playlists = filterFunc(playlists);
// if (process.env.PLAYLIST_NAME) {
// // Presuming the playlist with this name exists
// playlists = playlists.filter((p) => {
// return p.attributes.NAME === process.env.PLAYLIST_NAME;
// });
// } else {
// playlists = [].concat(playlists[0] || []);
// }
debug('playlists:');
debug(playlists);
// SUB Playlists
playlists.forEach(function(p, z) {
if (!p.uri) {
return;
}
resources = resources.concat(walkPlaylist(decrypt, basedir, p.uri, manifest, z, playlistFilter));
});
return resources;
};
module.exports = walkPlaylist;