Files
archived-youtubeDownloader/Server/index.js
2020-09-03 22:07:53 -03:00

61 lines
1.3 KiB
JavaScript

const express = require('express');
const cors = require('cors');
const ytdl = require('ytdl-core');
const app = express();
const PORT = 4000;
app.use(cors());
app.listen(PORT, () => {
console.log(`Server Works !!! At port ${PORT}`);
});
app.get('/downloadmp3', async (req, res, next) => {
try {
var url = req.query.url;
if(!ytdl.validateURL(url)) {
return res.sendStatus(400);
}
let title = 'audio';
await ytdl.getBasicInfo(url, {
format: 'mp4'
}, (err, info) => {
if (err) throw err;
title = info.player_response.videoDetails.title.replace(/[^\x00-\x7F]/g, "");
});
res.header('Content-Disposition', `attachment; filename="${title}.mp3"`);
ytdl(url, {
format: 'mp3',
filter: 'audioonly',
}).pipe(res);
} catch (err) {
console.error(err);
}
});
app.get('/downloadmp4', async (req, res, next) => {
try {
let url = req.query.url;
if(!ytdl.validateURL(url)) {
return res.sendStatus(400);
}
let title = 'video';
await ytdl.getBasicInfo(url, {
format: 'mp4'
}, (err, info) => {
title = info.player_response.videoDetails.title.replace(/[^\x00-\x7F]/g, "");
});
res.header('Content-Disposition', `attachment; filename="${title}.mp4"`);
ytdl(url, {
format: 'mp4',
}).pipe(res);
} catch (err) {
console.error(err);
}
});