|
1
|
|
View Code Duplication |
var fs = require('fs'); |
|
2
|
|
|
var YoutubeMp3Downloader = require('youtube-mp3-downloader'); |
|
3
|
|
|
|
|
4
|
|
|
var file_put_contents = function(filename, content) { |
|
5
|
|
|
var typedata = typeof content; |
|
6
|
|
|
if (Array.isArray(content)) { |
|
7
|
|
|
typedata = 'array'; |
|
8
|
|
|
} |
|
9
|
|
|
if (typedata == 'object' || Array.isArray(content)) { |
|
10
|
|
|
content = JSON.stringify(content, null, 4); |
|
11
|
|
|
} |
|
12
|
|
|
fs.writeFile(filename, content, { |
|
13
|
|
|
flag: 'w' |
|
14
|
|
|
}, function(err) { |
|
15
|
|
|
if (err) |
|
16
|
|
|
return console.error(err); |
|
17
|
|
|
fs.readFile(filename, 'utf-8', function(err, data) { |
|
18
|
|
|
if (err) |
|
19
|
|
|
return console.error(err); |
|
20
|
|
|
console.log(data); |
|
|
|
|
|
|
21
|
|
|
}); |
|
|
|
|
|
|
22
|
|
|
}); |
|
23
|
|
|
} |
|
24
|
|
|
var create_folder = function(dir) { |
|
25
|
|
|
if (!fs.existsSync(dir)) { |
|
26
|
|
|
fs.mkdirSync(dir, { |
|
27
|
|
|
recursive: true |
|
28
|
|
|
}); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
var arg = ''; |
|
33
|
|
|
var VID; |
|
34
|
|
|
for (let j = 0; j < process.argv.length; j++) { |
|
35
|
|
|
arg = process.argv[j]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if (arg == 'install') { |
|
39
|
|
|
create_folder('log'); |
|
40
|
|
|
create_folder('log/progress'); |
|
41
|
|
|
create_folder('log/error'); |
|
42
|
|
|
create_folder('log/progress'); |
|
43
|
|
|
create_folder('mp3'); |
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
var YD = new YoutubeMp3Downloader({ |
|
48
|
|
|
'ffmpegPath': 'D:\\bin\\ffmpeg\\bin\\ffmpeg.exe', // Where is the FFmpeg binary located? |
|
49
|
|
|
'outputPath': 'mp3/', // Where should the downloaded and encoded files be stored? |
|
50
|
|
|
'youtubeVideoQuality': 'highest', // What video quality should be used? |
|
51
|
|
|
'queueParallelism': 2, // How many parallel downloads/encodes should be started? |
|
52
|
|
|
'progressTimeout': 2000 // How long should be the interval of the progress reports |
|
53
|
|
|
}); |
|
54
|
|
|
|
|
55
|
|
|
//Download video and save as MP3 file |
|
56
|
|
|
YD.download(arg); |
|
57
|
|
|
YD.on('finished', function(err, data) { |
|
58
|
|
|
//console.log(JSON.stringify(data)); |
|
59
|
|
|
var file_finish = 'finish.json'; |
|
60
|
|
|
if (VID) { |
|
|
|
|
|
|
61
|
|
|
file_finish = `${VID}.json`; |
|
62
|
|
|
} |
|
63
|
|
|
if (data.hasOwnProperty('videoId')) { |
|
64
|
|
|
file_finish = `${data.videoId}.json`; |
|
65
|
|
|
} |
|
66
|
|
|
file_put_contents(`log/finish/${file_finish}`, data); |
|
67
|
|
|
}); |
|
68
|
|
|
|
|
69
|
|
|
YD.on('error', function(error) { |
|
70
|
|
|
//console.log(error); |
|
71
|
|
|
var file_error = 'error.json'; |
|
72
|
|
|
if (VID) { |
|
|
|
|
|
|
73
|
|
|
file_error = `${VID}.json`; |
|
74
|
|
|
} |
|
75
|
|
|
file_put_contents(`log/error/${file_error}`, error); |
|
76
|
|
|
}); |
|
77
|
|
|
|
|
78
|
|
|
YD.on('progress', function(progress) { |
|
79
|
|
|
//console.log(JSON.stringify(progress)); |
|
80
|
|
|
var file_progress = 'progress.json'; |
|
81
|
|
|
if (progress.hasOwnProperty('videoId')) { |
|
82
|
|
|
file_progress = `${progress.videoId}.json`; |
|
83
|
|
|
} |
|
84
|
|
|
file_put_contents(`log/progress/${file_progress}`, progress); |
|
85
|
|
|
}); |