-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
84 lines (73 loc) · 2.73 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
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
const contentDisposition = require("content-disposition");
const express = require("express");
const ffmpegPath = require("@ffmpeg-installer/ffmpeg").path;
const ffmpeg = require("fluent-ffmpeg");
const path = require("path");
const ytdl = require("ytdl-core");
ffmpeg.setFfmpegPath(ffmpegPath);
const app = express();
const port = 8881;
app.use(express.json());
app.use("/", express.static(path.join(__dirname), {
setHeaders: (res, path, stat) => {
if (path.endsWith(".css")) {
res.setHeader("Content-Type", "text/css");
}
}
}));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.post("/download", async (req, res) => {
log("Получен POST запрос на /download");
const { url, audioSpeed } = req.body;
log(`Получены аргументы url: ${url}, audioSpeed: ${audioSpeed}`);
try {
const info = await ytdl.getBasicInfo(url);
const stream = ytdl(url, { quality: "highestaudio" });
const title = info.videoDetails.title;
log(`Получен заголовок видео по url: ${title}`);
var contentDispositionResult = contentDisposition(`${title}.mp3`);
res.set("Content-Disposition", contentDispositionResult);
log(`Установлен Content-Disposition: ${contentDispositionResult}`);
ffmpeg(stream)
.audioBitrate(128)
.audioFilter(`atempo=${audioSpeed}`)
.format("mp3")
.outputOptions([
"-preset ultrafast",
"-movflags faststart"
])
.on("start", function (commandLine) {
log("Файл начал свою загрузку");
})
.on("progress", function (p) {
log(`Файл загружен до ${p.timemark} (${p.targetSize}kb)`)
})
.on("end", function (stdout, stderr) {
log("Файл успешно отправлен пользователю");
})
.pipe(res);
} catch (error) {
errorLog(`Ошибка: ${error}`);
res.status(500).send("Произошла ошибка при обработке запроса");
}
});
app.listen(port, () => {
log(`Сервер запущен на порту ${port}`);
});
process.on("uncaughtException", function (err) {
errorLog(err);
});
function log(message) {
var time = getTime();
console.log(`${time} ${message}`);
}
function errorLog(message) {
var time = getTime();
console.error(`${time} ${message}`);
}
function getTime(s) {
var date = new Date();
return `[${("0" + date.getHours()).slice(-2)}:${("0" + date.getMinutes()).slice(-2)}:${("0" + date.getSeconds()).slice(-2)}]`;
}