-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
48 lines (35 loc) · 1.42 KB
/
server.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
/* global require, process, __dirname */
const path = require("path");
const express = require("express");
const ytdl = require("ytdl-core");
const Logger = require("../util/logger");
const LOG = new Logger("youmuse");
const app = express();
const PORT = process.argv[2] || 8001;
app.use(express.static(__dirname + "/public"));
app.get("/getAudioUrl/:videoId", (req, res) => {
const url = `https://www.youtube.com/watch?v=${req.params.videoId}`;
// Download video information so that we can choose a format and get URL
ytdl.getInfo(url, (err, info) => {
if (err) return LOG.log("Error getting video info:", err);
const format = info.formats.reduce(function (acc, c) {
// Isn"t an audio format
if (!c.mimeType || c.mimeType.indexOf("audio") < 0) return acc;
// mp4 best
if (c.container === "mp4") {
if (acc.container !== "mp4") return c;
if (c.audioBitrate > acc.audioBitrate && c.audioBitrate < 128) return c;
// webm second best
} else if (c.container === "webm" && acc.container !== "mp4") {
if (c.audioBitrate > acc.audioBitrate) return c;
}
return acc;
}, { container: "false", audioBitrate: 0 });
res.send({
"url": format.url,
"duration": info.length_seconds
});
});
});
app.get("*", (req, res) => res.sendFile(path.resolve(__dirname, "public/index.html")));
app.listen(PORT, () => LOG.log(`Server running on port ${PORT}`));