forked from przemyslawpluta/node-youtube-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownload.js
34 lines (28 loc) · 800 Bytes
/
download.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var path = require('path');
var fs = require('fs');
var ytdl = require('..');
var video = ytdl('http://www.youtube.com/watch?v=Seku9G1kT0c',
// Optional arguments passed to youtube-dl.
['-f', '22']);
var size = 0;
video.on('info', function(info) {
size = info.size;
console.log('Got video info');
console.log('saving to ' + info._filename);
var output = path.join(__dirname, 'videos', info._filename);
video.pipe(fs.createWriteStream(output));
});
var pos = 0;
video.on('data', function(data) {
pos += data.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('end', function() {
console.log();
});