Skip to content

Commit

Permalink
perf: make additional dash and m3u8 format requests faster
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed May 20, 2020
1 parent f778d84 commit c0f2eae
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 26 deletions.
25 changes: 4 additions & 21 deletions lib/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,11 @@ exports.getFullInfo = async(id, options) => {
info.player_response.streamingData.dashManifestUrl ||
info.player_response.streamingData.hlsManifestUrl
);
let funcs = [];
if (info.formats.length) {
const html5playerfile = urllib.resolve(VIDEO_URL, info.html5player);
let tokens = await sig.getTokens(html5playerfile, options);
sig.decipherFormats(info.formats, tokens, options.debug);
const html5player = urllib.resolve(VIDEO_URL, info.html5player);
funcs.push(sig.decipherFormats(info.formats, html5player, options));
}
let funcs = [];
if (hasManifest && info.player_response.streamingData.dashManifestUrl) {
let url = info.player_response.streamingData.dashManifestUrl;
funcs.push(getDashManifest(url, options));
Expand All @@ -207,30 +206,14 @@ exports.getFullInfo = async(id, options) => {
}

let results = await Promise.all(funcs);
if (results[0]) { mergeFormats(info, results[0]); }
if (results[1]) { mergeFormats(info, results[1]); }

info.formats = Object.values(Object.assign({}, ...results));
info.formats = info.formats.map(util.addFormatMeta);
info.formats.sort(util.sortFormats);
info.full = true;
return info;
};


/**
* Merges formats from DASH or M3U8 with formats from video info page.
*
* @param {Object} info
* @param {Object} formatsMap
*/
const mergeFormats = (info, formatsMap) => {
info.formats.forEach(f => {
formatsMap[f.itag] = formatsMap[f.itag] || f;
});
info.formats = Object.values(formatsMap);
};


/**
* Gets additional DASH formats.
*
Expand Down
11 changes: 6 additions & 5 deletions lib/sig.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ exports.getTokens = async(html5playerfile, options) => {
if (!tokens || !tokens.length) {
throw Error('Could not extract signature deciphering actions');
}

exports.cache.set(html5playerfile, tokens);
return tokens;
}
Expand Down Expand Up @@ -245,10 +244,11 @@ exports.setDownloadURL = (format, sig, debug) => {
* Applies `sig.decipher()` to all format URL's.
*
* @param {Array.<Object>} formats
* @param {Array.<string>} tokens
* @param {boolean} debug
* @param {string} html5player
* @param {Object} options
*/
exports.decipherFormats = (formats, tokens, debug) => {
exports.decipherFormats = async(formats, html5player, options) => {
let tokens = await exports.getTokens(html5player, options);
formats.forEach(format => {
let cipher = format.signatureCipher || format.cipher;
if (cipher) {
Expand All @@ -257,6 +257,7 @@ exports.decipherFormats = (formats, tokens, debug) => {
delete format.cipher;
}
const sig = tokens && format.s ? exports.decipher(tokens, format.s) : null;
exports.setDownloadURL(format, sig, debug);
exports.setDownloadURL(format, sig, options.debug);
});
return formats;
};

0 comments on commit c0f2eae

Please sign in to comment.