-
Notifications
You must be signed in to change notification settings - Fork 993
/
Copy pathyoutube.js
56 lines (49 loc) · 1.58 KB
/
youtube.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
"use strict";
// Important: to use this you must also install ytdl-core as a dependency
const ytdl = require("ytdl-core");
// handles youtube video pages and replaces them with a custom page that just streams the video
function processRequest(data) {
if (ytdl.validateURL(data.url)) {
const res = data.clientResponse;
// if we write headers, unblocker will detect that and stop trying to process this request
res.writeHead(200, { "content-type": "text/html; charset=utf-8" });
// todo: use config.prefix instead of hard-coding '/proxy/' into the url
ytdl
.getInfo(data.url)
.then((info) => {
// only use formats with combined audio and video (note these tend to be lower resolution)
const formats = ytdl.filterFormats(info.formats, "audioandvideo");
const thumb = info.videoDetails.thumbnails.pop();
res.end(
`<!DOCTYPE html>
<head>
<title>${info.videoDetails.title}</title>
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW"/>
</head>
<body>
<h1>${info.videoDetails.title}</h1>
<video controls poster="/proxy/${thumb.url}" style="width: 100%">
${formats
.map(
(format) =>
` <source type="${format.mimeType
.split(";")
.shift()}" src="/proxy/${format.url.replace(/&/g, "&")}">`
)
.join("\n")}
</video>
<p>${info.videoDetails.description.replace(/[\n]/g, "\n<br>")}</p>
</body>
</html>
`
);
})
.catch((err) => {
console.error(`Error getting info for ${data.url}`, err);
res.end("Error retrieving video info");
});
}
}
module.exports = {
processRequest,
};