This repository was archived by the owner on Jan 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
52 lines (40 loc) · 2.02 KB
/
main.js
File metadata and controls
52 lines (40 loc) · 2.02 KB
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
const optionDefinitions = [
{ name: 'link', alias: 'l', type: String, multiple: true, defaultOption: true },
{ name: 'help', alias: 'h', type: Boolean},
{ name: 'path', alias: 'p', type: String }
]
const fs = require('fs');
const ytdl = require('ytdl-core');
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const ffmpeg = require('fluent-ffmpeg');
ffmpeg.setFfmpegPath(ffmpegPath);
const commandLineArgs = require('command-line-args');
const options = commandLineArgs(optionDefinitions);
if (options['help'] == true) {
console.log("node main.js -p PATH(optional) -l youtube_link(s)");
}
let link = options['link']; // array of links
let path = options['path'] || "./video/"; // optional
fs.existsSync(path) || fs.mkdirSync(path);
let index = 0;
let size = link.length;
link.forEach(video => { // video link forEach
ytdl.getInfo(video, (err, info) => { // getting info Map
if (err) throw err;
index += 1;
console.log("downloading " + info['title'] + " as " + info['video_id'] + "!");
ytdl(video, { quality: 137 }).pipe(fs.createWriteStream(path + info['video_id'] + '_videoonly.mp4')).on("finish", ()=> { // download video only file. when it's finished
ytdl(video, { quality: 140 }).pipe(fs.createWriteStream(path + info['video_id'] + '_audioonly.m4a')).on("finish", ()=>{ // download audio only file
ffmpeg() // then use ffmpeg
.mergeAdd(path + info['video_id'] + '_videoonly.mp4')
.mergeAdd(path + info['video_id'] + '_audioonly.m4a') // wrap video to audio
.on("end", ()=>{
fs.unlinkSync(path + info['video_id'] + '_videoonly.mp4'); // delete video only file
fs.unlinkSync(path + info['video_id'] + '_audioonly.m4a'); // delete audio only file
console.log(index + "/" + size + " complete!");
})
.save(path + info['video_id'] + '.mp4'); // finally save
})
})
});
});