Last active
December 19, 2019 20:52
-
-
Save max-mapper/52bc1df4f834d9fb32b838a9e16c475a to your computer and use it in GitHub Desktop.
youtube playlist downloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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