Working V1

This commit is contained in:
MasterAcnolo
2025-06-27 15:02:35 +02:00
parent 7b93413050
commit 95701bf56e
2 changed files with 51 additions and 47 deletions

View File

@@ -3,16 +3,26 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title> <title>Freedom Loader</title>
</head> </head>
<body> <body>
<form action="http://localhost:8080/download" method="POST">
<input type="text" name="url" placeholder="Colle une URL YouTube" required> <form action="/download" method="POST">
<input type="checkbox" name="format" value="mp3" id="format"> MP3? <input name="url" placeholder="URL YouTube">
<input type="checkbox" name="audioOnly" value="1"> MP3 seulement
<select name="quality">
<option value="best">Meilleure qualité</option>
<option value="worst">Qualité minimale</option>
</select>
<button type="submit">Télécharger</button> <button type="submit">Télécharger</button>
</form> </form>
<!--Il faut utiliser un formulaire, parce que on doit envoyer a un serveur
les données à traiter. Pour cela on définit une route vers /download, ca va
être l'espèce de "canal" attribué a ça. Ca va faire que si jamais on veut ensuite recup
les infos du form, il faut aller sur ce canal précis (/download). La méthode POST dit
que l'on envoie des données.-->
</body> </body>
</html> </html>

View File

@@ -1,67 +1,61 @@
const express = require("express"); const express = require("express");
const { exec } = require("child_process"); const { exec } = require("child_process"); // Requis pour écrire dans un terminal
const fs = require("fs"); const fs = require("fs");
const path = require("path");
const app = express(); const app = express();
app.use(express.urlencoded({ extended: true })); // Pour pouvoir recevoir plein de truc
app.use(express.urlencoded({ extended: true })); if (!fs.existsSync("downloads")) { // Si jamais il n'y a pas de dossier downloads
app.use(express.json()); fs.mkdirSync("downloads"); // On le crée
const downloadDir = path.join(__dirname, "downloads");
if (!fs.existsSync(downloadDir)) {
fs.mkdirSync(downloadDir);
} }
const allowedFormats = ["best", "mp3", "mp4"];
app.post("/download", (req, res) => { app.post("/download", (req, res) => {
const videoUrl = req.body.url; const options = {
let format = req.body.format || "best"; url: req.body.url,
audioOnly: req.body.format === "mp3", // coché mp3
quality: req.body.quality || "best", // par défaut best
subtitles: req.body.subs === "1", // imaginons un checkbox
//FAUT RAJOUTER DES ARGUMENTS SOUS CETTE FORME
};
if (!videoUrl) { if (!options.url) {
return res.status(400).send("❌ Il faut envoyer une URL !"); return res.status(400).send("❌ URL manquante !");
} }
if (!allowedFormats.includes(format)) { // construction progressive de la commmande
format = "best";
// A SAVOIR QUE L'ON VA DISTINGUER DEUX TYPES D'ARGUMENTS SI j'AI BIEN COMPRIS, GENRE
// LES ARGUMENTS OBLIGATOIRES ET LES ARGUMENTS FALCUTATIFS
// GENRE LES SOUS TITRE OU L'AUDIO ONLY QUI SONT NON ESSENTIEL
let command = `yt-dlp`;
if (options.audioOnly) {
command += " --extract-audio --audio-format mp3"; // AJOUT DE l'ONLY AUDIO SI JAMAIS ON LE CHECK, SINON CA PREND LA VALEUR PAR DEFAUT A SAVOIR VIDEO
} }
const safeUrl = `"${videoUrl.replace(/"/g, '\\"')}"`; if (options.subtitles) {
command += " --write-subs --sub-lang en";
let command = "";
if (format === "mp3") {
command = `yt-dlp --extract-audio --audio-format mp3 -o "downloads/%(title)s.%(ext)s" ${safeUrl}`;
} else {
command = `yt-dlp -f ${format} -o "downloads/%(title)s.%(ext)s" ${safeUrl}`;
} }
console.log("▶️ Commande exécutée :", command); command += ` -f ${options.quality}`; //QUALITE DU TELECHARGEMENT
command += ` -o "downloads/%(title)s.%(ext)s"`; // DOSSIER DE SORTIE
command += ` "${options.url}"`; // L'URL DE BASE
exec(command, (error, stdout, stderr) => { // ON COOK LA COMMANDE FINALE ICI:
if (stdout) console.log("=== SORTIE ===", stdout); console.log("🔧 Commande finale :", command);
if (stderr) console.error("=== STDERR ===", stderr);
exec(command, (error, stdout, stderr) => { // Error = Erreur Node JS || stdout = sortie de la commande du terminal || STDerr c'est les erreurs interne au terminal
if (error) { if (error) {
console.error("=== ERREUR ===", error); console.error("Erreur yt-dlp :", stderr || error.message);
return res.status(500).send(`❌ Problème pendant le téléchargement : ${stderr || error.message}`); return res.status(500).send("❌ Erreur pendant le téléchargement.");
} }
res.send("✅ Téléchargement lancé !"); console.log("yt-dlp terminé :", stdout);
res.send("✅ Téléchargement terminé !");
}); });
}); });
app.get("/downloads", (req, res) => { app.listen(8080, () => {
fs.readdir(downloadDir, (err, files) => { console.log("🟢 Serveur prêt sur http://localhost:8080");
if (err) {
return res.status(500).send("Erreur de lecture du dossier");
}
res.json(files);
});
});
const PORT = 8080;
app.listen(PORT, () => {
console.log(`🟢 Serveur prêt : http://localhost:${PORT}`);
}); });