diff --git a/README.md b/README.md index 5024890..07dfc26 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,16 @@ youtubedl.getInfo([url1, url2], function(err, info) { }) ``` +### Using a proxy + +``` js +const youtubedl = require('youtube-dl') + +const video = youtubedl('http://www.youtube.com/watch?v=90AiXO1pAiA', + // Optional arguments passed to youtube-dl. + ['--proxy', 'http://ip:port'], +``` + ### Downloading subtitles ``` js diff --git a/lib/youtube-dl.js b/lib/youtube-dl.js index 356a273..74231d0 100644 --- a/lib/youtube-dl.js +++ b/lib/youtube-dl.js @@ -34,7 +34,7 @@ function youtubeDl (args, options, cb) { * @param {Object} stream */ -function processData (data, options, stream) { +function processData (data, args, options, stream) { const item = !data.length ? data : data.shift() // fix for pause/resume downloads @@ -47,13 +47,15 @@ function processData (data, options, stream) { headers.Range = 'bytes=' + options.start + '-' + options.end } else { headers.Range = 'bytes=0-' + item.filesize - } + } + const proxyArgIndex = args.indexOf('--proxy') const req = request({ url: item.url, headers: headers, ecdhCurve: 'auto', - timeout: 30000 + timeout: 30000, + ...(proxyArgIndex > -1 && { proxy: args[proxyArgIndex + 1] }) }) req.on('response', function response (res) { @@ -94,12 +96,12 @@ const ytdl = (module.exports = function (videoUrl, args, options) { }) if (!isString(videoUrl)) { - processData(videoUrl, options, stream) + processData(videoUrl, args, options, stream) return stream } ytdl.getInfo(videoUrl, args, options, function getInfo (err, data) { - return err ? stream.emit('error', err) : processData(data, options, stream) + return err ? stream.emit('error', err) : processData(data, args, options, stream) }) return stream