-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
54 lines (45 loc) · 1.18 KB
/
app.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
const express = require("express");
const request = require("request");
const cors = require("cors");
const fs = require("fs");
const ytdl = require("ytdl-core");
const { redirect } = require("express/lib/response");
const app = express();
app.use(express.urlencoded({ extended: false }));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.post("/", (req, res) => {
try {
const url = String(req.body.link);
const format = req.body.format;
if (url.length == 0) {
res.redirect('/');
}
if (format === "mp4") {
res.header(
"Content-Disposition",
'attachment; filename="video.' + format + '"'
);
ytdl(url, {
format: "mp4",
filter: "audioandvideo",
quality: 'highestvideo'
}).pipe(res);
} else if (format === "mp3") {
res.header(
"Content-Disposition",
'attachment; filename="audio.' + format + '"'
);
ytdl(url, {
filter: "audioonly",
quality: "highestaudio"
}).pipe(res);
}
} catch (err) {
console.log(err.message);
}
});
app.listen(process.env.PORT || 3000, () => {
console.log("Success.");
});