-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
36 lines (27 loc) · 929 Bytes
/
server.ts
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
import express from 'express';
import ytdl from 'ytdl-core';
import cors from 'cors';
const app = express();
const port = 3000;
app.use(cors());
// Download video
app.get('/download/video', async (req, res) => {
const videoUrl = req.query.url as string;
if (!ytdl.validateURL(videoUrl)) {
return res.status(400).send('Invalid YouTube URL');
}
res.header('Content-Disposition', 'attachment; filename="video.mp4"');
ytdl(videoUrl, { format: 'mp4' }).pipe(res);
});
// Download audio
app.get('/download/audio', async (req, res) => {
const videoUrl = req.query.url as string;
if (!ytdl.validateURL(videoUrl)) {
return res.status(400).send('Invalid YouTube URL');
}
res.header('Content-Disposition', 'attachment; filename="audio.mp3"');
ytdl(videoUrl, { filter: 'audioonly', format: 'mp3' }).pipe(res);
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});