-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
119 lines (105 loc) · 4.03 KB
/
main.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
const ytdl = require('ytdl-core');
const ytpl = require("ytpl");
const express = require('express');
require("./assents/ffmpeg-static/install.js");
const app = express();
const port = 1524;
const fs = require('fs');
const downloadProgessLimit = 10;
const uuid = require('uuid');
const child_process = require('child_process');
if (!fs.existsSync('temp')) {
fs.mkdirSync('temp');
}
if (!fs.existsSync('videos')) {
fs.mkdirSync('videos');
}
if (!fs.existsSync('songs')) {
fs.mkdirSync('songs');
}
app.use(express.json());
app.post("/dwn", async (req, res) => {
res.json({
code: 0,
message: "Check your console!"
});
var nowdwn = 0;
var done = 0;
var unjson = req.body;
var errVds = [];
if (typeof (unjson.videos) === "string") {
var o = await ytpl(unjson.videos);
unjson.videos = [];
o.items.forEach(e => {
unjson.videos.push(e.shortUrl);
})
}
function download(url) {
return new Promise(async (resolve, reject) => {
var info = await ytdl.getBasicInfo(url);
var title = info.player_response.videoDetails.title;
var author = info.player_response.videoDetails.author;
var toFilename = string => string.replace(/\n/g," ").replace(/[<>:"/\\|?*\x00-\x1F]| +$/g,"").replace(/^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])$/, x => x + "_");
console.log(`\x1b[33mDownloading ${title}, By ${author}...\x1b[0m`);
if (unjson.type === "video") {
var a = require("./assents/download.js");
resolve(await a.dall(url, title, toFilename(title)))
} else {
var id = uuid.v4();
// var file = fs.createWriteStream(`songs/${toFilename(title)}.mp3`);
var file = fs.createWriteStream(`temp/${id}`);
var stream = ytdl(url, { filter: "audioonly" });
stream.on("data", data => {
file.write(data);
})
stream.on('end', () => {
file.end();
console.log(`\x1b[33mConverting ${title} from WebM to MP3...\x1b[0m`);
child_process.execSync(`ffmpeg -y -loglevel 0 -hide_banner -i "temp/${id}" -acodec libmp3lame -ab 128k -ar 44100 -ac 2 "songs/${toFilename(title)}.mp3"`);
fs.unlinkSync(`temp/${id}`);
console.log(`\x1b[32mDownloaded ${title}\x1b[0m`);
resolve(true);
});
stream.on('error', err => {
file.end();
fs.unlinkSync(`temp/${id}`);
console.log(`\x1b[31mDownload ${title} Failed: ${err}\x1b[0m`);
if (errVds.findIndex(e => e === url) === -1) {
unjson.videos.push(url);
}
errVds.push(url);
resolve(false);
});
}
});
}
var p = 0;
function dwn() {
var dsl = downloadProgessLimit;
if (unjson.type === "video") dsl = Math.floor(downloadProgessLimit / 2);
if (nowdwn < dsl) {
download(unjson.videos[p]).then(e => {
nowdwn--;
done++;
if (done === unjson.videos.length) {
if (errVds.length > 0) {
console.log(`\x1b[31mDownloading Failed: ${errVds.length} videos\x1b[0m`);
for (var video of errVds) {
console.log(`\x1b[31mFailed: ${video}\x1b[0m`);
}
}
console.log(`\x1b[32mDownloaded all file, in this session YouTube-Downloader downloaded ${p} file(s)\x1b[0m`);
}
});
nowdwn++;
p++;
}
if (p !== unjson.videos.length && done + 1 !== unjson.videos.length) {
setTimeout(dwn, 500)
}
}
dwn();
});
app.listen(port, () => {
// console.log(`yds app listening at http://localhost:${port}`);
})