-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (55 loc) · 1.5 KB
/
index.js
File metadata and controls
69 lines (55 loc) · 1.5 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import express from "express";
import ytdl from "ytdl-core";
const PORT = process.env.PORT || 5192;
const index = express();
index.set("view engine", "ejs");
index.use(express.static("assets"));
index.use(express.urlencoded({ extended: true }));
index.get("/", (req, res) => {
res.render("index");
});
index.post("/getdata", (req, res) => {
ytdl
.getInfo(req.body.videoURL)
.then((info) => {
const title = info.videoDetails.title;
const options = info.formats
.filter((e) => e.hasAudio === true)
.sort((a, b) => (a.mimeType > b.mimeType ? 1 : -1));
res.render("download", {
thumbnail:
info.videoDetails.thumbnails[info.videoDetails.thumbnails.length - 1]
.url,
url: req.body.videoURL,
title: title,
options: options,
});
})
.catch((error) => {
console.log(error);
res.send("Error");
});
});
index.post("/download", (req, res) => {
ytdl
.getInfo(req.body.videoURL)
.then((info) => {
const format = ytdl.chooseFormat(info.formats, {
quality: req.body.avid,
});
const ext = format.hasVideo === true ? ".mp4" : ".mp3";
const fileName =
info.videoDetails.title +
(ext === ".mp4" ? ` ${format.qualityLabel}` : "") +
ext;
res.header("Content-Disposition", `attachment;\ filename="${fileName}"`);
ytdl.downloadFromInfo(info, { quality: req.body.avid }).pipe(res);
})
.catch((error) => {
console.log(error);
res.send("Error");
});
});
index.listen(PORT, () => {
console.log(`Server initiated at port ${PORT}`);
});