-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroutes.js
61 lines (50 loc) · 2.5 KB
/
routes.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
const router = require('express').Router();
const fs = require('fs')
, ytdl = require('ytdl-core')
, path = require('path')
, ffmpeg = require('fluent-ffmpeg-extended')
, rs = require('randomstring');
router.get('/', async (req, res) => {
res.render('index', { error: null });
})
router
.post('/', async (req, res) => {
try {
const code = `${rs.generate(12)}`;
const video_url = req.body.url;
const format_chosen = req.body['format'];
if (!video_url)
return res.render('index', { error: { type: 'error', msg: 'No video URL found, provide a youtube.com or youtu.be video link.' } });
if (format_chosen !== 'mp3' && format_chosen !== 'mp4' && !format_chosen && format_chosen === '')
return res.render('index', { error: { type: 'error', msg: 'No format supplied, please select "mp3" or "mp4".' } });
if (format_chosen === 'mp4') {
const s = ytdl(`${video_url}`, { filter: (format) => format.container === `mp4` })
.pipe(fs.createWriteStream(path.resolve(`${code}.mp4`)));
s.on('end', () => {
return res.render('index', { error: { type: 'success', msg: `Video downloaded to ${code}.mp4, <a href="/download/${code}.mp4">click here</a> to download it.` } });
});
}
if (format_chosen === 'mp3') {
const stream = ytdl(`${video_url}`, { filter: (format) => format.container === `mp4` });
ffmpeg(stream)
withAudioBitrate('128k').toFormat('mp3').savetoFile(`${__dirname}/${code}.mp3`, async(stdout, stderr) => {
return res.render('index', { error: { type: 'success', msg: `Video downloaded to ${code}.mp3, <a href="/download/${code}.mp3">click here</a> to download it.` } });
});
}
}
catch (err) {
console.error(err);
return res.render('index', { error: { type: 'error', msg: 'An error occured: "'+err+'"' } });
}
});
router.get('/download/:id', async (req, res) => {
let thing = req.params.id;
if (!thing) return res.redirect('/');
try {
res.sendFile(path.resolve(__dirname + `/${thing}`));
}
catch (err) {
return res.render('index', { error: { type: 'error', msg: 'Unable to find that download: the file doesn\'t exist, was deleted, or you don\'t have access.' } });
}
});
module.exports = router;