pax_global_header00006660000000000000000000000064124050673570014523gustar00rootroot0000000000000052 comment=c360b42fc32ebaa03b11a334398ff8620f7cf140 node-ytdl-core-0.2.4+dfsg/000077500000000000000000000000001240506735700152725ustar00rootroot00000000000000node-ytdl-core-0.2.4+dfsg/.gitignore000066400000000000000000000000151240506735700172560ustar00rootroot00000000000000node_modules node-ytdl-core-0.2.4+dfsg/.travis.yml000066400000000000000000000000441240506735700174010ustar00rootroot00000000000000language: node_js node_js: - 0.10 node-ytdl-core-0.2.4+dfsg/LICENSE000066400000000000000000000020451240506735700163000ustar00rootroot00000000000000Copyright (C) 2012 by Roly Fentanes Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. node-ytdl-core-0.2.4+dfsg/README.md000066400000000000000000000055561240506735700165640ustar00rootroot00000000000000# node-ytdl-core [![Build Status](https://secure.travis-ci.org/fent/node-ytdl-core.png)](http://travis-ci.org/fent/node-ytdl-core) Yet another youtube downloading module. This time written with only Javascript and a more node-friendly streaming interface. For a CLI version of this, check out [ytdl](https://github.com/fent/node-ytdl). # Usage ```js var fs = require('fs'); var ytdl = require('ytdl-core'); ytdl('http://www.youtube.com/watch?v=A02s8omM_hI') .pipe(fs.createWriteStream('video.flv')); ``` # API ### ytdl(url, options) Attempts to download a video from the given url. Returns a readable stream. `options` can have the following keys * `quality` - Video quality to download. Can be an [itag value](http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs) value, `highest`, or `lowest`. Defaults to `highest`. * `filter` - You can give a filtering function that gets called with each format available. Used to decide what format to download. This function is given the `format` object as its first argument, and should return true if the format is preferable. * `range` - A byte range in the form `INT-INT` that specifies a part of the video to download. ie 10355705-12452856. ```js // Example with `filter` option. ytdl(url, { filter: function(format) { return format.container === 'mp4'; } }) .pipe(fs.createWriteStream('vide.mp4')); ``` `options` can also have any [request](https://github.com/mikeal/request) options. The returned readable stream emits these additional events. #### Event: 'info' * `Object` - Info. * `Object` - Format. Emitted when the a video's `info` hash is fetched. Along with the chosen format metadata to download. `format.url` might be different if `start` was given. `format.size` will also be available. Info and format may look like [this](https://gist.github.com/fent/6c8251132e1addb5121e). ### ytdl.getInfo(url, [options], callback(err, info)) Use this if you only want to get metainfo from a video. `options` gets passed to the `request()`, it can also have a `downloadURL` property set to `true` if you want ytdl to include the download url instead of the regular one. In some cases, a signature needs to be deciphered, and will require ytdl to make additional requests. ### ytdl.downloadFromInfo(info, options) Once you have received metadata from a video with the `getInfo` function, you may pass that `info`, along with other `options` to `downloadFromInfo`. Note: Be sure to set the `downloadURL` option to `true` when you call `getInfo` or you will receive a 403 error when you call `downloadFromInfo`. The returned readable stream emits these additional events: #### Event: 'format' * `Object` - Format. Emitted when a format metadata has been chosen. `format.size` will also be available. # Install npm install ytdl-core # Tests Tests are written with [mocha](http://visionmedia.github.com/mocha/) ```bash npm test ``` # License MIT node-ytdl-core-0.2.4+dfsg/lib/000077500000000000000000000000001240506735700160405ustar00rootroot00000000000000node-ytdl-core-0.2.4+dfsg/lib/crequest.js000066400000000000000000000004621240506735700202330ustar00rootroot00000000000000var request = require('request'); module.exports = function(options, callback) { request(options, function(err, res, body) { if (err) return callback(err); if (res.statusCode !== 200) { return callback(new Error('status code ' + res.statusCode)); } callback(null, body); }); }; node-ytdl-core-0.2.4+dfsg/lib/formats.js000066400000000000000000000210771240506735700200600ustar00rootroot00000000000000/** * http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs */ module.exports = { '5': { container: 'flv', resolution: '240p', encoding: 'Sorenson H.283', profile: null, bitrate: '0.25', audioEncoding: 'mp3', audioBitrate: 64, }, '6': { container: 'flv', resolution: '270p', encoding: 'Sorenson H.263', profile: null, bitrate: '0.8', audioEncoding: 'mp3', audioBitrate: 64, }, '13': { container: '3gp', resolution: null, encoding: 'MPEG-4 Visual', profile: null, bitrate: '0.5', audioEncoding: 'aac', audioBitrate: null, }, '17': { container: '3gp', resolution: '144p', encoding: 'MPEG-4 Visual', profile: 'simple', bitrate: '0.05', audioEncoding: 'aac', audioBitrate: 24, }, '18': { container: 'mp4', resolution: '360p', encoding: 'H.264', profile: 'baseline', bitrate: '0.5', audioEncoding: 'aac', audioBitrate: 96, }, '22': { container: 'mp4', resolution: '720p', encoding: 'H.264', profile: 'high', bitrate: '2-3', audioEncoding: 'aac', audioBitrate: 192, }, '34': { container: 'flv', resolution: '360p', encoding: 'H.264', profile: 'main', bitrate: '0.5', audioEncoding: 'aac', audioBitrate: 128, }, '35': { container: 'flv', resolution: '480p', encoding: 'H.264', profile: 'main', bitrate: '0.8-1', audioEncoding: 'aac', audioBitrate: 128, }, '36': { container: '3gp', resolution: '240p', encoding: 'MPEG-4 Visual', profile: 'simple', bitrate: '0.175', audioEncoding: 'aac', audioBitrate: 36, }, '37': { container: 'mp4', resolution: '1080p', encoding: 'H.264', profile: 'high', bitrate: '3-5.9', audioEncoding: 'aac', audioBitrate: 192, }, '38': { container: 'mp4', resolution: '3072p', encoding: 'H.264', profile: 'high', bitrate: '3.5-5', audioEncoding: 'aac', audioBitrate: 192, }, '43': { container: 'webm', resolution: '360p', encoding: 'VP8', profile: null, bitrate: '0.5', audioEncoding: 'vorbis', audioBitrate: 128, }, '44': { container: 'webm', resolution: '480p', encoding: 'VP8', profile: null, bitrate: '1', audioEncoding: 'vorbis', audioBitrate: 128, }, '45': { container: 'webm', resolution: '720p', encoding: 'VP8', profile: null, bitrate: '2', audioEncoding: 'vorbis', audioBitrate: 192, }, '46': { container: 'webm', resolution: '1080p', encoding: 'vp8', profile: null, bitrate: null, audioEncoding: 'vorbis', audioBitrate: 192, }, '82': { container: 'mp4', resolution: '360p', encoding: 'H.264', profile: '3d', bitrate: '0.5', audioEncoding: 'aac', audioBitrate: 96, }, '83': { container: 'mp4', resolution: '240p', encoding: 'H.264', profile: '3d', bitrate: '0.5', audioEncoding: 'aac', audioBitrate: 96, }, '84': { container: 'mp4', resolution: '720p', encoding: 'H.264', profile: '3d', bitrate: '2-3', audioEncoding: 'aac', audioBitrate: 192, }, '85': { container: 'mp4', resolution: '1080p', encoding: 'H.264', profile: '3d', bitrate: '3-4', audioEncoding: 'aac', audioBitrate: 192, }, '100': { container: 'webm', resolution: '360p', encoding: 'VP8', profile: '3d', bitrate: null, audioEncoding: 'vorbis', audioBitrate: 128, }, '101': { container: 'webm', resolution: '360p', encoding: 'VP8', profile: '3d', bitrate: null, audioEncoding: 'vorbis', audioBitrate: 192, }, '102': { container: 'webm', resolution: '720p', encoding: 'VP8', profile: '3d', bitrate: null, audioEncoding: 'vorbis', audioBitrate: 192, }, // DASH (video only) '133': { container: 'mp4', resolution: '240p', encoding: 'H.264', profile: 'main', bitrate: '0.2-0.3', audioEncoding: null, audioBitrate: null, }, '134': { container: 'mp4', resolution: '360p', encoding: 'H.264', profile: 'main', bitrate: '0.3-0.4', audioEncoding: null, audioBitrate: null, }, '135': { container: 'mp4', resolution: '480p', encoding: 'H.264', profile: 'main', bitrate: '0.5-1', audioEncoding: null, audioBitrate: null, }, '136': { container: 'mp4', resolution: '720p', encoding: 'H.264', profile: 'main', bitrate: '1-1.5', audioEncoding: null, audioBitrate: null, }, '137': { container: 'mp4', resolution: '1080p', encoding: 'H.264', profile: 'high', bitrate: '2-3', audioEncoding: null, audioBitrate: null, }, '160': { container: 'mp4', resolution: '144p', encoding: 'H.264', profile: 'main', bitrate: '0.1', audioEncoding: null, audioBitrate: null, }, '264': { container: 'mp4', resolution: '1440p', encoding: 'H.264', profile: 'high', bitrate: '4-5', audioEncoding: null, audioBitrate: null, }, // DASH (audio only) '139': { container: 'mp4', resolution: null, encoding: null, profile: null, bitrate: null, audioEncoding: 'aac', audioBitrate: 48, }, '140': { container: 'mp4', resolution: null, encoding: null, profile: null, bitrate: null, audioEncoding: 'aac', audioBitrate: 128, }, '141': { container: 'mp4', resolution: null, encoding: null, profile: null, bitrate: null, audioEncoding: 'aac', audioBitrate: 256, }, '171': { container: 'webm', resolution: null, encoding: null, profile: null, bitrate: null, audioEncoding: 'vorbis', audioBitrate: 128, }, '172': { container: 'webm', resolution: null, encoding: null, profile: null, bitrate: null, audioEncoding: 'vorbis', audioBitrate: 192, }, // Live streaming '92': { container: 'ts', resolution: '240p', encoding: 'H.264', profile: 'main', bitrate: '0.15-0.3', audioEncoding: 'aac', audioBitrate: 48, }, '93': { container: 'ts', resolution: '480p', encoding: 'H.264', profile: 'main', bitrate: '0.5-1', audioEncoding: 'aac', audioBitrate: 128, }, '94': { container: 'ts', resolution: '720p', encoding: 'H.264', profile: 'main', bitrate: '0.8-1.25', audioEncoding: 'aac', audioBitrate: 128, }, '95': { container: 'ts', resolution: '1080p', encoding: 'H.264', profile: 'main', bitrate: '1.5-3', audioEncoding: 'aac', audioBitrate: 256, }, '96': { container: 'ts', resolution: '720p', encoding: 'H.264', profile: 'main', bitrate: '2.5-6', audioEncoding: 'aac', audioBitrate: 256, }, '120': { container: 'flv', resolution: '720p', encoding: 'H.264', profile: 'Main@L3.1', bitrate: '2', audioEncoding: 'aac', audioBitrate: 128, }, '127': { container: 'ts', resolution: null, encoding: null, profile: null, bitrate: null, audioEncoding: 'aac', audioBitrate: 96, }, '128': { container: 'ts', resolution: null, encoding: null, profile: null, bitrate: null, audioEncoding: 'aac', audioBitrate: 96, }, '132': { container: 'ts', resolution: '240p', encoding: 'H.264', profile: 'baseline', bitrate: '0.15-0.2', audioEncoding: 'aac', audioBitrate: 48, }, '151': { container: 'ts', resolution: '720p', encoding: 'H.264', profile: 'baseline', bitrate: '0.05', audioEncoding: 'aac', audioBitrate: 24, }, '242': { container: 'webm', resolution: '240p', encoding: 'VP9', profile: null, bitrate: '0.14', audioEncoding: null, audioBitrate: null, }, '243': { container: 'webm', resolution: '360p', encoding: 'VP9', profile: null, bitrate: '0.26', audioEncoding: null, audioBitrate: null, }, '244': { container: 'webm', resolution: '480p', encoding: 'VP9', profile: null, bitrate: '0.585', audioEncoding: null, audioBitrate: null, }, '247': { container: 'webm', resolution: '720p', encoding: 'VP9', profile: null, bitrate: '1.184', audioEncoding: null, audioBitrate: null, }, '248': { container: 'webm', resolution: '1080p', encoding: 'VP9', profile: null, bitrate: '1.895', audioEncoding: null, audioBitrate: null, }, }; node-ytdl-core-0.2.4+dfsg/lib/index.js000066400000000000000000000043411240506735700175070ustar00rootroot00000000000000var http = require('http'); var PassThrough = require('stream').PassThrough; var request = require('request'); var _ = require('underscore'); var getInfo = require('./info'); var crequest = require('./crequest'); var util = require('./util'); module.exports = ytdl; ytdl.getInfo = getInfo; ytdl.downloadFromInfo = downloadFromInfo; /** * @param {String} link * @param {Object} options * @return {ReadableStream} */ function ytdl(link, options) { options = options || {}; options.downloadURL = true; var stream = new PassThrough(); getInfo(link, options, function(err, info) { if (err) { stream.emit('error', err); return; } downloadFromInfoCallback(info, options, function(err, format, videoStream) { if (err) { stream.emit('error', err); return; } stream.emit('info', info, format); videoStream.pipe(stream); }); }); return stream; } function downloadFromInfoCallback(info, options, callback) { options = options || {}; var format = util.chooseFormat(info.formats, options); if (format instanceof Error) { // The caller expects this function to be async. setImmediate(callbackWithFormatError); return; } var requestOptions = _.clone(options); requestOptions.url = format.url; if (requestOptions.range) { requestOptions.url += '&range=' + requestOptions.range; } delete requestOptions.quality; delete requestOptions.range; delete requestOptions.filter; // Start downloading the video. var req = request(requestOptions); req.on('error', callback); req.on('response', function(res) { if (res.statusCode !== 200) { callback(new Error('status code ' + res.statusCode)); return; } format.size = res.headers['content-length']; callback(null, format, req); }); function callbackWithFormatError() { callback(format); } } function downloadFromInfo(info, options) { var stream = new PassThrough(); options = options || {}; downloadFromInfoCallback(info, options, function(err, format, videoStream) { if (err) { stream.emit('error', err); return; } stream.emit('format', format); videoStream.pipe(stream); }); return stream; } node-ytdl-core-0.2.4+dfsg/lib/info.js000066400000000000000000000050101240506735700173250ustar00rootroot00000000000000var qs = require('querystring'); var _ = require('underscore'); var crequest = require('./crequest'); var util = require('./util'); var sig = require('./sig'); var INFO_URL = 'http://www.youtube.com/get_video_info?' + 'hl=en_US&el=detailpage&video_id='; var VIDEO_URL = 'http://www.youtube.com/watch?v='; var KEYS_TO_SPLIT = [ 'keywords', 'fmt_list', 'fexp', 'watermark', 'ad_channel_code_overlay' ]; /** * Gets info from a video. * * @param {String} link * @param {Object} requestOptions * @param {Function(Error, Object)} callback */ module.exports = function getInfo(link, requestOptions, callback) { if (typeof requestOptions === 'function') { callback = requestOptions; requestOptions = {}; } else { requestOptions = _.clone(requestOptions); } var id = util.getVideoID(link); requestOptions.url = INFO_URL + id; link = VIDEO_URL + id; var downloadURL = requestOptions.downloadURL; delete requestOptions.downloadURL; crequest(requestOptions, function(err, body) { if (err) return callback(err); var info = qs.parse(body); if (info.status === 'fail') { callback(new Error('Error ' + info.errorcode + ': ' + info.reason)); return; } // Split some keys by commas. KEYS_TO_SPLIT.forEach(function(key) { if (!info[key]) return; info[key] = info[key] .split(',') .filter(function(v) { return v !== ''; }); }); // Convert some strings to javascript numbers and booleans. Object.keys(info).forEach(function(key) { var val = info[key]; var intVal = parseInt(val, 10); var floatVal = parseFloat(val, 10); if (intVal.toString() === val) { val = intVal; } else if (floatVal.toString() === val) { val = floatVal; } else if (val === 'True') { val = true; } else if (val === 'False') { val = false; } info[key] = val; }); if (info.fmt_list) { info.fmt_list = info.fmt_list.map(function(format) { return format.split('/'); }); } else { info.fmt_list = []; } info.formats = util.parseFormats(info); if (info.video_verticals) { info.video_verticals = info.video_verticals .slice(1, -1) .split(', ') .filter(function(val) { return val !== ''; }) .map(function(val) { return parseInt(val, 10); }) ; } if (downloadURL) { sig.get(link, info, requestOptions.debug, callback); } else { callback(null, info); } }); }; node-ytdl-core-0.2.4+dfsg/lib/sig.js000066400000000000000000000161251240506735700171650ustar00rootroot00000000000000var fs = require('fs'); var path = require('path'); var url = require('url'); var util = require('./util'); var crequest = require('./crequest'); var JStream = require('jstream'); /** * Gets signature for formats. * * @param {String} link * @param {Object} info * @param {Boolean} debug * @param {Function(Error, String)} callback */ exports.get = function(link, info, debug, callback) { if (info.use_cipher_signature) { crequest(link, function(err, body) { if (err) return callback(err); var jsonStr = util.between(body, 'ytplayer.config = ', ''); if (!jsonStr) { return callback(new Error('could not find `ytplayer.config`')); } var jstream = new JStream(); var ended = false; jstream.on('data', function(config) { ended = true; jstream.pause(); var newFormats = util.parseFormats(config.args); var html5playerfile = 'http:' + config.assets.js; crequest(html5playerfile, function(err, body) { if (err) return callback(err); var tokens = exports.extractActions(body); if (!tokens) { if (debug) { var filename = path.basename(config.assets.js); var filepath = path.resolve( __dirname, '../tests/files/html5player/' + filename); fs.writeFile(filepath, body); var name = path.basename(filename, '.js'); var html5player = require('../test/html5player.json'); html5player[name] = []; fs.writeFile( path.resolve(__dirname, '../test/html5player.json'), JSON.stringify(html5player, null, 2)); } callback( new Error('could not extract signature deciphering actions')); return; } info.formats = info.formats.map(function(format) { var newFormat = newFormats .filter(function(f) { return f.itag === format.itag; })[0]; if (newFormat && newFormat.s) { format = newFormat; } var sig = exports.decipher(tokens, format.s); format.url = exports.getDownloadURL(format, sig, debug); return format; }); callback(null, info); }); }); jstream.on('error', function(err) { if (ended) { return; } callback( new Error('could not parse `ytplayer.config`: ' + err.message)); }); jstream.end(jsonStr); }); } else { info.formats.forEach(function(format) { var sig = format.sig || ''; format.url = exports.getDownloadURL(format, sig, debug); }); callback(null, info); } }; /** * @param {Object} format * @param {Array.} tokens * @param {Boolean} debug * @return {!String} */ exports.getDownloadURL = function(format, sig, debug) { var decodedUrl; if (format.url) { decodedUrl = format.url; } else if (format.stream) { if (format.conn) { decodedUrl = format.conn; if (decodedUrl[decodedUrl.length - 1] !== '/') { decodedUrl += '/'; } decodedUrl += format.stream; } else { decodedUrl = format.stream; } } else { if (debug) { console.warn('download url not found for itag ' + format.itag); } return null; } try { decodedUrl = decodeURIComponent(decodedUrl); } catch (err) { if (debug) { console.warn('could not decode url: ' + err.message); } return null; } // Make some adjustments to the final url. var parsedUrl = url.parse(decodedUrl, true); // Deleting the `search` part is necessary otherwise changes to // `query` won't reflect when running `url.format()` delete parsedUrl.search; var query = parsedUrl.query; query.ratebypass = 'yes'; if (sig) { query.signature = sig; } return url.format(parsedUrl); }; /** * Decipher a signature based on action tokens. * * @param {Array.} tokens * @param {String} sig * @return {String} */ exports.decipher = function(tokens, sig) { sig = sig.split(''); var pos; for (var i = 0, len = tokens.length; i < len; i++) { var token = tokens[i]; switch (token[0]) { case 'r': sig = sig.reverse(); break; case 'w': pos = ~~token.slice(1); sig = swapHeadAndPosition(sig, pos); break; case 's': pos = ~~token.slice(1); sig = sig.slice(pos); break; case 'p': pos = ~~token.slice(1); sig.splice(0, pos); break; } } return sig.join(''); }; /** * Swaps the first element of an array with one of given position. * * @param {Array.} arr * @param {Number} position * @return {Array.} */ function swapHeadAndPosition(arr, position) { var first = arr[0]; arr[0] = arr[position % arr.length]; arr[position] = first; return arr; } var jsvarStr = '[a-zA-Z_][a-zA-Z_0-9]*'; var reverseStr = ':function\\(a\\)\\{' + '(?:return )?a\\.reverse\\(\\)' + '\\}'; var sliceStr = ':function\\(a,b\\)\\{' + 'return a\\.slice\\(b\\)' + '\\}'; var spliceStr = ':function\\(a,b\\)\\{' + 'a\\.splice\\(0,b\\)' + '\\}'; var swapStr = ':function\\(a,b\\)\\{' + 'var c=a\\[0\\];a\\[0\\]=a\\[b%a\\.length\\];a\\[b\\]=c(?:;return a)?' + '\\}'; var actionsRegexp = new RegExp( 'var (' + jsvarStr + ')=\\{((?:(?:' + jsvarStr + reverseStr + '|' + jsvarStr + sliceStr + '|' + jsvarStr + spliceStr + '|' + jsvarStr + swapStr + '),?)+)\\};' + 'function ' + jsvarStr + '\\(a\\)\\{' + 'a=a\\.split\\(""\\);' + '((?:(?:a=)?\\1\\.' + jsvarStr + '\\(a,\\d+\\);)+)' + 'return a\\.join\\(""\\)' + '\\}' ); var reverseRegexp = new RegExp('(?:^|,)(' + jsvarStr + ')' + reverseStr); var sliceRegexp = new RegExp('(?:^|,)(' + jsvarStr + ')' + sliceStr); var spliceRegexp = new RegExp('(?:^|,)(' + jsvarStr + ')' + spliceStr); var swapRegexp = new RegExp('(?:^|,)(' + jsvarStr + ')' + swapStr); /** * Extracts the actions that should be taken to decypher a signature. * * * @param {String} body * @return {Array.} */ exports.extractActions = function(body) { var result = actionsRegexp.exec(body); if (!result) { return null; } var obj = result[1]; var objBody = result[2]; var funcbody = result[3]; result = reverseRegexp.exec(objBody); var reverseKey = result && result[1]; result = sliceRegexp.exec(objBody); var sliceKey = result && result[1]; result = spliceRegexp.exec(objBody); var spliceKey = result && result[1]; result = swapRegexp.exec(objBody); var swapKey = result && result[1]; var myreg = '(?:a=)?' + obj + '\\.(' + [reverseKey, sliceKey, spliceKey, swapKey].join('|') + ')\\(a,(\\d+)\\)'; var tokenizeRegexp = new RegExp(myreg, 'g'); var tokens = []; while ((result = tokenizeRegexp.exec(funcbody)) !== null) { switch (result[1]) { case swapKey: tokens.push('w' + result[2]); break; case reverseKey: tokens.push('r'); break; case sliceKey: tokens.push('s' + result[2]); break; case spliceKey: tokens.push('p' + result[2]); break; } } return tokens; }; node-ytdl-core-0.2.4+dfsg/lib/util.js000066400000000000000000000105561240506735700173620ustar00rootroot00000000000000var qs = require('querystring'); var url = require('url'); var _ = require('underscore'); var FORMATS = require('./formats'); /** * Parses a string representation of amount of milliseconds. * * @param {String} time * @return {Number} */ var timeRegexp = /(?:(\d+)h)?(?:(\d+)m(?!s))?(?:(\d+)s)?(?:(\d+)(?:ms)?)?/; exports.parseTime = function(time) { var result = timeRegexp.exec(time.toString()); var hours = result[1] || 0; var mins = result[2] || 0; var secs = result[3] || 0; var ms = result[4] || 0; return hours * 3600000 + mins * 60000 + secs * 1000 + parseInt(ms, 10); }; /** * Sort formats from highest quality to lowest. * By resolution, then video bitrate, then audio bitrate. * * @param {Object} a * @param {Object} b */ exports.sortFormats = function(a, b) { var ares = a.resolution ? parseInt(a.resolution.slice(0, -1), 10) : 0; var bres = b.resolution ? parseInt(b.resolution.slice(0, -1), 10) : 0; var aabitrate = a.audioBitrate || 0; var babitrate = b.audioBitrate || 0; var afeats = ~~!!ares * 2 + ~~!!aabitrate; var bfeats = ~~!!bres * 2 + ~~!!babitrate; if (afeats === bfeats) { if (ares === bres) { var abitrate, bbitrate, s; if (a.bitrate) { s = a.bitrate.split('-'); abitrate = parseFloat(s[s.length - 1], 10); } else { abitrate = 0; } if (b.bitrate) { s = b.bitrate.split('-'); bbitrate = parseFloat(s[s.length - 1], 10); } else { bbitrate = 0; } if (abitrate === bbitrate) { return babitrate - aabitrate; } else { return bbitrate - abitrate; } } else { return bres - ares; } } else { return bfeats - afeats; } }; /** * Choose a format depending on the given options. * * @param {Array.} formats * @param {Object} options * @return {Object|Error} */ exports.chooseFormat = function(formats, options) { if (options.filter) { formats = formats.filter(options.filter); if (formats.length === 0) { return new Error('no formats found with custom filter'); } } var format; var quality = options.quality || 'highest'; switch (quality) { case 'highest': format = formats[0]; break; case 'lowest': format = formats[formats.length - 1]; break; default: format = formats.filter(function(format) { return format.itag === '' + quality; })[0]; } if (!format) { return new Error('No such format found: ' + quality); } else if (format.rtmp) { return new Error('rtmp protocol not supported'); } return format; }; /** * Extract string inbetween another. * * @param {String} haystack * @param {String} left * @param {String} right * @return {String} */ exports.between = function(haystack, left, right) { var pos; pos = haystack.indexOf(left); if (pos === -1) { return ''; } haystack = haystack.slice(pos + left.length); pos = haystack.indexOf(right); if (pos === -1) { return ''; } haystack = haystack.slice(0, pos); return haystack; }; /** * Get video ID. * * There are a few type of video URL formats. * - http://www.youtube.com/watch?v=VIDEO_ID * - http://youtu.be/VIDEO_ID * * @param {String} link * @return {String} */ exports.getVideoID = function(link) { var linkParsed = url.parse(link, true); var id = linkParsed.hostname === 'youtu.be' ? linkParsed.pathname.slice(1) : linkParsed.query.v; if (!id) { throw new Error('No video id found: ' + link); } return id; }; /** * @param {Object} info * @param {Boolean} debug * @return {Array.} */ exports.parseFormats = function(info, debug) { var formats = []; if (info.url_encoded_fmt_stream_map) { formats = formats .concat(info.url_encoded_fmt_stream_map.split(',')); } if (info.adaptive_fmts) { formats = formats.concat(info.adaptive_fmts.split(',')); } formats = formats .map(function(format) { var data = qs.parse(format); if (data.conn && data.conn.indexOf('rtmp') === 0) { data.rtmp = true; } var meta = FORMATS[data.itag]; if (!meta && debug) { console.warn('No format metadata for itag ' + data.itag + ' found'); } _.extend(data, meta); return data; }); delete info.url_encoded_fmt_stream_map; delete info.adaptive_fmts; formats.sort(exports.sortFormats); return formats; }; node-ytdl-core-0.2.4+dfsg/package.json000066400000000000000000000013621240506735700175620ustar00rootroot00000000000000{ "name": "ytdl-core", "description": "Youtube video downloader in pure javascript.", "keywords": [ "youtube", "video", "download" ], "version": "0.2.4", "repository": { "type": "git", "url": "git://github.com/fent/node-ytdl-core.git" }, "author": "Roly Fentanes (https://github.com/fent)", "main": "./lib/index.js", "scripts": { "test": "mocha -R spec -t 8000 test/*-test.js" }, "dependencies": { "jstream": "~0.2.7", "request": "~2.37.0", "underscore": "~1.6.0" }, "devDependencies": { "mocha": "*", "nock": "~0.34.1", "stream-equal": "~0.1.0" }, "licenses": [ { "type": "MIT", "url": "http://github.com/fent/node-ytdl-core/raw/master/LICENSE" } ] }