-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (56 loc) · 2.76 KB
/
index.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
#!/usr/bin/env node
const { Command } = require('commander');
const program = new Command();
var options = {escape:true,prettyPrint:true,xmlHeader:{standalone:true}};
var YoutubeMp3Downloader = require('./lib/ytDownloader');
program.version('0.0.1');
program
.option('-i, --id <id>', 'id')
.option('-quality, --quality <quality>', 'video/audio quality : highestaudio / lowestaudio / highestvideo / lowestvideo ')
program.parse(process.argv);
if(program._optionValues.hasOwnProperty("id")){
if(program._optionValues.id == "" || program._optionValues.id == null ){
process.exit(1);
}else{
if(program._optionValues.quality == "" || program._optionValues.quality == null ){
var YD = new YoutubeMp3Downloader({
"ffmpegPath": "./ffmpeg/bin/ffmpeg.exe", // FFmpeg binary location
"outputPath": "./", // Output file location (default: the home directory)
"youtubeVideoQuality": "highestaudio", // Desired video quality (default: highestaudio)
"queueParallelism": 2, // Download parallelism (default: 1)
"progressTimeout": 2000, // Interval in ms for the progress reports (default: 1000)
"allowWebm": false // Enable download from WebM sources (default: false)
});
}else{
var YD = new YoutubeMp3Downloader({
"ffmpegPath": "./ffmpeg/bin/ffmpeg.exe", // FFmpeg binary location
"outputPath": "./", // Output file location (default: the home directory)
"youtubeVideoQuality": program._optionValues.quality, // Desired video quality (default: highestaudio)
"queueParallelism": 2, // Download parallelism (default: 1)
"progressTimeout": 2000, // Interval in ms for the progress reports (default: 1000)
"allowWebm": false // Enable download from WebM sources (default: false)
});
}
if(program._optionValues.id.indexOf("www.youtube.com") !=-1){
YD.download(getIdFromUrl(program._optionValues.id));
}else{
YD.download(program._optionValues.id);
}
YD.on("finished", function(err, data) {
console.log(JSON.stringify(data));
});
YD.on("error", function(error) {
console.log(error);
});
YD.on("progress", function(progress) {
console.log(JSON.stringify(progress));
});
}
}else{
process.exit(1);
}
function getIdFromUrl(link) {
var query = link.replace("https://www.youtube.com/watch?","");
var vars = query.split('&');
return vars[0].replace("v=","").trim();
}