-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
143 lines (129 loc) · 3.69 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const express = require("express");
const cors = require("cors");
const ytdl = require("ytdl-core");
const ffmpeg = require("ffmpeg-static");
const ffmpegPath = require("@ffmpeg-installer/ffmpeg").path;
const fluent_ffmpeg = require("fluent-ffmpeg");
const contentDisposition = require("content-disposition");
const cp = require("child_process");
const app = express();
const port = 3000;
const path = require('path');
///////////////////////////////////////////////////////////
// code made by @megsystem on github, plz give credit :) //
///////////////////////////////////////////////////////////
//setup ffmpeg fluent
fluent_ffmpeg.setFfmpegPath(ffmpegPath);
// setup server
app.use(cors());
app.use(express.json());
app.listen(port, () => console.log(`Server is running on port ${port}`));
// empty page // for testing //
app.get('/', function(req, res) {
try {
res.send('Working in progress - @giovanni_giannone_ / megsystem');
} catch (err) {
return res
.status(400)
.json({ success: false, error: err });
}
});
// download mp4
app.get("/mp4", async (req, res) => {
const { url: url } = req.query;
if (!ytdl.validateID(url) && !ytdl.validateURL(url)) {
return res
.status(400)
.json({ success: false, error: "No valid YouTube Id!" });
}
try {
// get name video
let download = "video.mp4";
await ytdl.getInfo(url).then((info) => {
download = info.videoDetails.title + ".mp4";
});
res.setHeader("Content-disposition", contentDisposition(download));
// get youtube video
const audio = await ytdl(url, { quality: "highestaudio" });
const video = await ytdl(url, { quality: "highestvideo" });
// download in browser
const ffmpegProcess = cp.spawn(
ffmpeg,
[
"-i",
`pipe:3`,
"-i",
`pipe:4`,
"-map",
"0:v",
"-map",
"1:a",
"-c:v",
"copy",
"-c:a",
"libmp3lame",
"-crf",
"27",
"-preset",
"veryfast",
"-movflags",
"frag_keyframe+empty_moov",
"-f",
"mp4",
"-loglevel",
"error",
"-",
],
{
stdio: ["pipe", "pipe", "pipe", "pipe", "pipe"],
}
);
await video.pipe(ffmpegProcess.stdio[3]);
await audio.pipe(ffmpegProcess.stdio[4]);
await ffmpegProcess.stdio[1].pipe(res);
let ffmpegLogs = "";
ffmpegProcess.stdio[2].on("data", (chunk) => {
ffmpegLogs += chunk.toString();
});
ffmpegProcess.on("exit", (exitCode) => {
if (exitCode === 1) {
console.error(ffmpegLogs);
}
});
} catch (err) {
console.log("error ", err);
return res
.status(400)
.json({ success: false, error: err });
}
});
// download mp3
app.get("/mp3", async (req, res) => {
const { url: url } = req.query;
if (!ytdl.validateID(url) && !ytdl.validateURL(url)) {
return res
.status(400)
.json({ success: false, error: "No valid YouTube Id!" });
}
try {
// get youtube video
let stream = await ytdl(url, {
quality: "highestaudio",
});
// set info
let download = "song.mp3";
await ytdl.getInfo(url).then((info) => {
download = info.videoDetails.title + ".mp3";
});
res.setHeader("Content-disposition", contentDisposition(download));
// download in browser
await fluent_ffmpeg(stream).audioBitrate(128).format("mp3").pipe(res, {
end: true,
});
} catch (err) {
console.log("error ", err);
return res
.status(400)
.json({ success: false, error: err });
}
});