ytdl-core
Advanced tools
Comparing version 0.10.4 to 0.11.0
@@ -61,5 +61,2 @@ var PassThrough = require('stream').PassThrough; | ||
var url = format.url; | ||
if (options.range) { | ||
url += '&range=' + options.range; | ||
} | ||
if (options.begin) { | ||
@@ -69,3 +66,10 @@ url += '&begin=' + util.fromHumanTime(options.begin); | ||
doDownload(stream, url, options, 3); | ||
doDownload(stream, url, options, 3, { | ||
trys: 5, | ||
range: { | ||
start: options.range && options.range.start ? options.range.start : 0, | ||
end: options.range && options.range.end ? options.range.end : -1, | ||
}, | ||
downloaded: 0, | ||
}); | ||
} | ||
@@ -84,4 +88,5 @@ | ||
* @param {Number} tryCount Prevent infinite redirects. | ||
* @param {Object} reconnectInfo Continue after ECONNRESET | ||
*/ | ||
function doDownload(stream, url, options, tryCount) { | ||
function doDownload(stream, url, options, tryCount, reconnectInfo) { | ||
if (stream._isDestroyed) { return; } | ||
@@ -92,13 +97,26 @@ if (tryCount === 0) { | ||
} | ||
if (reconnectInfo.trys === 0) { | ||
stream.emit('error', new Error('Too many reconnects')); | ||
return; | ||
} | ||
// Start downloading the video. | ||
var myrequest = options.request || request; | ||
var req = myrequest(url, options.requestOptions); | ||
var rangedUrl = url; | ||
if (reconnectInfo.downloaded !== 0 || | ||
reconnectInfo.range.start !== 0 || | ||
reconnectInfo.range.end !== -1) { | ||
var start = reconnectInfo.range.start + reconnectInfo.downloaded; | ||
var end = reconnectInfo.range.end != -1 ? reconnectInfo.range.end : ''; | ||
rangedUrl += '&range=' + start + '-' + end; | ||
} | ||
var req = myrequest(rangedUrl, options.requestOptions); | ||
var myres; | ||
stream.destroy = function() { | ||
stream._isDestroyed = true; | ||
req.abort(); | ||
if (myres) { | ||
myres.unpipe(); | ||
myres.destroy(); | ||
myres.unpipe(); | ||
} | ||
@@ -109,3 +127,7 @@ stream.emit('abort'); | ||
req.on('error', function(err) { | ||
stream.emit('error', err); | ||
if (stream._isDestroyed || !myres) { | ||
stream.emit('error', err); | ||
} else { | ||
myres.unpipe(); | ||
} | ||
}); | ||
@@ -120,3 +142,3 @@ | ||
// Redirection header. | ||
doDownload(stream, res.headers.location, options, tryCount - 1); | ||
doDownload(stream, res.headers.location, options, tryCount - 1, reconnectInfo); | ||
return; | ||
@@ -129,3 +151,24 @@ } | ||
res.pipe(stream); | ||
if (reconnectInfo.downloaded === 0) { | ||
reconnectInfo.total = parseInt(res.headers['content-length']); | ||
} | ||
// Keep track of the download progress | ||
res.on('data', function(chunk) { | ||
var downloaded = reconnectInfo.downloaded += chunk.length; | ||
stream.emit('progress', chunk.length, downloaded, reconnectInfo.total); | ||
}); | ||
res.on('end', function() { | ||
// Reconnect if there is more to be downloaded. | ||
if (reconnectInfo.downloaded < reconnectInfo.total) { | ||
myres.unpipe(); | ||
reconnectInfo.trys = reconnectInfo.trys - 1; | ||
doDownload(stream, url, options, tryCount, reconnectInfo); | ||
} else { | ||
stream.end(); | ||
} | ||
}); | ||
res.pipe(stream, {end : false}); | ||
stream.emit('response', res); | ||
@@ -132,0 +175,0 @@ }); |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "0.10.4", | ||
"version": "0.11.0", | ||
"repository": { | ||
@@ -16,2 +16,5 @@ "type": "git", | ||
"author": "Roly Fentanes (https://github.com/fent)", | ||
"contributors": [ | ||
"Tobias Kutscha (https://github.com/TimeForANinja)" | ||
], | ||
"main": "./lib/index.js", | ||
@@ -18,0 +21,0 @@ "types": "./typings/index.d.ts", |
@@ -30,3 +30,3 @@ # node-ytdl-core | ||
* `format` - This can be a specific `format` object returned from `getInfo`. This is primarily used to download specific video or audio streams. **Note:** Supplying this option will ignore the `filter` and `quality` options since the format is explicitly provided. | ||
* `range` - A byte range in the form `INT-INT` that specifies part of the file to download. ie 10355705-12452856. Note that this downloads a portion of the file, and not a separately spliced video. | ||
* `range` - A byte range in the form `{start: INT, end: INT}` that specifies part of the file to download. ie {start: 10355705, end: 12452856}. Note that this downloads a portion of the file, and not a separately spliced video. | ||
* `begin` - What time to begin downloading the video, supports formats 00:00:00.000, or 0ms, 0s, 0m, 0h, or number of milliseconds. Example: 1:30, 05:10.123, 10m30s. This option may not work on super short (less than 30s) videos, and has to be at ar above 6s. See [#129](https://github.com/fent/node-ytdl-core/issues/129) | ||
@@ -53,4 +53,11 @@ * `requestOptions` - Anything to merge into the request options which `http.get()` is called with, such as headers. | ||
Emitted when the video response has been found, and has started downloading. Can be used to get the size of download. This is also emitted if there is an error with the download. | ||
Emitted when the video response has been found, and has started downloading. Can be used to get the size of download. This is also emitted if there is an error with the download or it needs to reconnect to YouTube. | ||
#### Event: 'progress' | ||
* `Number` - Chunk length. | ||
* `Number` - Total downloaded. | ||
* `Number` - Total download length. | ||
Emitted whenever a new chunk is received. Passes values descriping the download progress and the parsed percentage. | ||
### Stream#destroy() | ||
@@ -57,0 +64,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53538
1712
126