Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
fix: don't fail on videos when ytdl returns filesize null
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelKr committed Jan 31, 2020
1 parent e0bf0c6 commit 8f6235b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/youtube-dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function processData (data, args, options, stream) {

if (options && options.start > 0 && options.end > 0) {
headers.Range = 'bytes=' + options.start + '-' + options.end
} else {
} else if (typeof item.filesize === 'number') {
headers.Range = 'bytes=0-' + item.filesize
}

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@
{
"name": "coderaiser",
"email": "mnemonic.enemy@gmail.com"
},
{
"name": "RafaelKr",
"email": "coding@rafael.kr"
}
],
"repository": {
Expand Down
55 changes: 55 additions & 0 deletions test/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var video2 = 'https://www.youtube.com/watch?v=179MiZSibco'
var video3 = 'https://www.youtube.com/watch?v=AW8OOp2undg'
var video4 = 'https://www.youtube.com/watch?v=yy7EUIR0fic'
var video5 = 'https://www.youtube.com/watch?v=LDDy4m_TiVk'
var video6 = 'http://www.youtube.com/watch?v=NOaxV9N108g'
var subtitleFile = '1 1 1-179MiZSibco.en.vtt'
var thumbnailFile = 'Too Many Twists (Heist Night 5_5)-yy7EUIR0fic.jpg'

Expand Down Expand Up @@ -182,6 +183,60 @@ vows
}
}
},
'a video with `filesize: null`': {
topic: function () {
'use strict'
var dl = ytdl(video6)
var cb = this.callback

dl.on('error', cb)

dl.on('info', function (info) {
var pos = 0
var progress

dl.on('data', function (data) {
pos += data.length
progress = pos / info.size
})

dl.on('end', function () {
cb(null, progress, info)
})

var filepath = path.join(__dirname, info._filename)
dl.pipe(fs.createWriteStream(filepath))
})
},

'data returned': function (err, progress, data) {
'use strict'
if (err) {
throw err
}

assert.strictEqual(progress, 1)
assert.isObject(data)
assert.strictEqual(data.id, 'NOaxV9N108g')
},

'file was downloaded': function (err, progress, data) {
'use strict'
if (err) {
throw err
}

// Check existance.
var filepath = path.join(__dirname, data._filename)
var exists = fs.existsSync(filepath)
if (exists) {
// Delete file after each test.
fs.unlinkSync(filepath)
} else {
assert.isTrue(exists)
}
}
},
'a video with subtitles': {
topic: function () {
'use strict'
Expand Down

0 comments on commit 8f6235b

Please sign in to comment.