Olá, estou fazendo um youtube downloader com javascript. Já fiz praticamente tudo, só falta colocar o ID do vídeo para aparecer o nome do vídeo quando faz o download.
Alguém poderia me ajudar? Estou usando o ytdl-core.
const express = require ('express');
// const cors = require ('cors');
const ytdl = require ('ytdl-core');
const app = express ();
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));
// app.use (cors ());
app.listen (4000, () => {
console.log ('Funciona no servidor !!! Na porta 4000');
});
app.post('/download', ( req , res ) => {
console.log(req.body);
var url = req.body.url;
var format = req.body.type;
res.header('Content-Disposition', 'attachment; filename = "video.'+format+'"');
ytdl (url, {
format: format
}).pipe(res);
/*
//SERVIDOR DO NODE!!!!!!!
*/
});
var URLinput = document.querySelector('.URL-input');
var form = document.getElementById("downloadform");
form.addEventListener('submit', (ev)=> {
ev.preventDefault();
var type = document.getElementById('formatInput').value;
var url = document.getElementById('urlInput').value;
var r = new XMLHttpRequest();
r.open("POST", "/downloadjson", true);
r.setRequestHeader("Content-Type", "application/json");
r.onreadystatechange = function () {
if (r.readyState != 4 || r.status != 200) return;
alert("Success: " + r.responseText);
};
r
r.send(JSON.stringify({url, type}));
//console.log(`URL: ${URLinput.value}`);
///sendURL(URLinput.value);
});
function sendURL(URL) {
window.location.href = `http://localhost:4000/download?URL=${URL}`;
}
//LOCAL DE DOWNLOAD