youtube playlist downloader
var path = require('path') | |
var fs = require('fs') | |
var ytdl = require('youtube-dl') | |
function playlist (url) { | |
var video = ytdl(url) // add , ['--audio-format=mp3'] for mp3 only | |
video.on('error', function error (err) { | |
console.log('error 2:', err) | |
}) | |
var size = 0 | |
var dir | |
video.on('info', function (info) { | |
if (!dir) { | |
dir = __dirname + '/' + info.playlist_title | |
try { fs.mkdirSync(dir) } catch (e) {} | |
} | |
size = info.size | |
var num = ("00" + info.playlist_index).substr(-3, 3) // left pad | |
var output = path.join(dir, num + ' - ' + info.fulltitle + '.mp4') | |
video.pipe(fs.createWriteStream(output)) | |
}) | |
var pos = 0 | |
video.on('data', function data (chunk) { | |
pos += chunk.length | |
// `size` should not be 0 here. | |
if (size) { | |
var percent = (pos / size * 100).toFixed(2) | |
process.stdout.cursorTo(0) | |
process.stdout.clearLine(1) | |
process.stdout.write(percent + '%') | |
} | |
}) | |
video.on('next', playlist) | |
} | |
playlist(process.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment