This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytutils.js
87 lines (83 loc) · 3.52 KB
/
ytutils.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
// Imports
let args = process.argv;
let youtubedl = require('youtube-dl');
let fs = require('fs');
// Messages
let INVALID_ARGUMENTS = "Invalid arguments";
let NOT_IMPLEMENTED_YET = "This feature is not implemented yet";
let INFO_SUCESSFULLY_WRITTEN_TO_FILE = "The information of the video {0} was sucessfully written to the file {1}";
let INVALID_FILE_ENDING = "The output file has to end with {0}"
let OR = "or"
let DOWNLOAD_STARTED = "Download started!{n}Size: {0}"
if(args.length <= 2) {
console.log(INVALID_ARGUMENTS);
} else {
if(args[2] === "download") {
if(args[3] === "video") {
if(args.length === 6) {
let input = args[4];
let output = args[5];
let format = "mp4";
let basicFormat = "best";
if(!(output.endsWith(".mp3") || output.endsWith(".mp4"))) {
return console.log(INVALID_FILE_ENDING.replace("{0}", ".mp3 " + OR + " .mp4"));
}
// if(output.endsWith(".flv")) format = "flv";
// if(output.endsWith(".mp3")) format = "mp3";
// if(output.endsWith(".mp4")) format = "mp4";
// if(output.endsWith(".ogg")) format = "ogg";
// if(output.endsWith(".wav")) format = "wav";
// if(output.endsWith(".webm")) format = "webm";
if(output.endsWith(".mp3")) basicFormat = "bestaudio";
if(output.endsWith(".mp4")) basicFormat = "best";
let video = youtubedl(input, ["--format=" + basicFormat]);
video.on("info", function(info) {
console.log(DOWNLOAD_STARTED.split("{n}")[0]);
console.log(DOWNLOAD_STARTED.split("{n}")[1].replace("{0}", info.size));
});
video.pipe(fs.createWriteStream(output));
} else {
console.log(INVALID_ARGUMENTS);
}
} else if(args[3] === "thumbnail") {
console.log(NOT_IMPLEMENTED_YET);
} else if(args[3] === "subtitles") {
console.log(NOT_IMPLEMENTED_YET);
} else {
console.log(INVALID_ARGUMENTS);
}
} else if(args[2] === "information" || args[2] === "infos") {
if(args.length === 5) {
let input = args[3];
let output = args[4];
let options = [];
if(!(output.endsWith(".json") || output.endsWith(".json"))) {
return console.log(INVALID_FILE_ENDING.replace("{0}", ".json " + OR + " .txt"));
}
youtubedl.getInfo(input, options, function(err, info) {
if (err) throw err;
let infoObject = {
"id": info.id,
"title": info.title,
"url": info.url,
"thumbnail": info.thumbnail,
"description": info.description,
"filename": info.filename,
"format-id": info.format_id
}
try {
fs.writeFile(output, JSON.stringify(infoObject), function(err, data) {
if(err) console.log(err);
});
return console.log(INFO_SUCESSFULLY_WRITTEN_TO_FILE.replace("{0}", info.id).replace("{1}", output));
} catch (err) {
return console.log(err);
}
});
} else {
console.log(INVALID_ARGUMENTS);
}
} else {
console.log(INVALID_ARGUMENTS);
}
}