forked from mayeaux/videodownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.js
143 lines (107 loc) · 3.4 KB
/
testing.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var youtubedl = require('youtube-dl');
const fs = require('fs');
const spawn = require('child_process').spawn;
const readline = require('readline');
const ytdl = require('ytdl-core');
const ffmpeg = require('fluent-ffmpeg');
const url = 'https://www.youtube.com/watch?v=ZcAiayke00I'
function ytdlStyle1(url){
let stream = ytdl(url, {
quality: 'highestaudio',
//filter: 'audioonly',
});
let starttime;
stream.pipe(fs.createWriteStream('thi11ng.webm'))
stream.once('response', () => {
starttime = Date.now();
});
stream.on('progress', (chunkLength, downloaded, total) => {
const percent = downloaded / total;
const downloadedMinutes = (Date.now() - starttime) / 1000 / 60;
readline.cursorTo(process.stdout, 0);
process.stdout.write(`${(percent * 100).toFixed(2)}% downloaded`);
process.stdout.write(`(${(downloaded / 1024 / 1024).toFixed(2)}MB of ${(total / 1024 / 1024).toFixed(2)}MB)\n`);
process.stdout.write(`running for: ${downloadedMinutes.toFixed(2)}minutes`);
process.stdout.write(`, estimated time left: ${(downloadedMinutes / percent - downloadedMinutes).toFixed(2)}minutes `);
readline.moveCursor(process.stdout, 0, -1);
});
}
function ytdlStyle(url){
let stream = ytdl(url, {
quality: 'highestaudio',
//filter: 'audioonly',
});
let start = Date.now();
ffmpeg(stream)
.audioBitrate(128)
.save(`${__dirname}/${`yo`}.mp3`)
.on('progress', (p) => {
readline.cursorTo(process.stdout, 0);
process.stdout.write(`${p.targetSize}kb downloaded`);
})
.on('end', () => {
console.log(`\ndone, thanks - ${(Date.now() - start) / 1000}s`);
});
}
function ytdlexec(url){
youtubedl.exec(url, ['-f', '251'], {}, function(err, output) {
if (err) throw err;
console.log(output.join('\n'));
});
}
function youtubeDl(url){
var arguments = ['-f', '140'];
var video = youtubedl(url,
// Optional arguments passed to youtube-dl.
arguments,
// Additional options can be given for calling `child_process.execFile()`.
{ cwd: __dirname + '/videos'});
var size;
var ext;
// Will be called when the download starts.
video.on('info', function(info) {
console.log(info);
console.log('Download started');
console.log('filename: ' + info._filename);
console.log('size: ' + info.size / 1000000 + 'MB');
size = info.size;
ext = info.ext;
});
console.log(ext);
var pos = 0;
video.on('data', function data(chunk) {
// console.log(chunk.length);
pos += chunk.length;
// `size` should not be 0 here.
if (size) {
var percent = (pos / size * 100).toFixed(2);
console.log(percent);
// console.log(percent + '%');
}
});
video.on('end', function() {
// shell.openItem('./videos');
});
video.pipe(fs.createWriteStream(`${'fre3434d'}.${`webm`}`));
}
function download(url){
const ls = spawn('node_modules/youtube-dl/bin/youtube-dl', ['-f' , '140', url]);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
ls.on('close', (code) => {
// downloaded++;// if(downloaded == needsToDownload){
// downloading = false;
// console.log('no longer downloading');
// }
console.log(`child process exited with code ${code}`);
});
}
// ytdlStyle1(url);
// ytdlStyle(url);
download(url);
// youtubeDl(url);
// ytdlexec(url);