Skip to content

Instantly share code, notes, and snippets.

@kiritocode1
Created November 25, 2024 16:14
Show Gist options
  • Save kiritocode1/5051d4f03cad9259bd8b5cc39341af1b to your computer and use it in GitHub Desktop.
Save kiritocode1/5051d4f03cad9259bd8b5cc39341af1b to your computer and use it in GitHub Desktop.
const { exec } = require("child_process");
const express = require("express");
const fs = require("fs");
const app = express();
const port = 3000;
app.get("/download", (req, res) => {
const url = "https://youtu.be/2guKYgtji84?si=_nfFhIHseFgIvJza";
const sanitizedTitle = "video";
const outputFile = `${__dirname}/${sanitizedTitle}`;
const command = `yt-dlp -o "${outputFile}" "${url}"`;
exec("pip install yt-dlp", (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${stderr}`);
return res.status(500).send("An error occurred while installing yt-dlp.");
}
console.log("yt-dlp installed successfully:", stdout);
});
exec(`touch ${sanitizedTitle}`, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${stderr}`);
return res.status(500).send("An error occurred while creating the video.txt file.");
}
console.log("video created successfully:", stdout);
});
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${stderr}`);
return res.status(500).send("An error occurred while downloading the video.");
}
console.log("Video downloaded successfully:", stdout);
res.download(outputFile, sanitizedTitle, (err) => {
if (err) console.error("Error sending file:", err);
fs.unlinkSync(outputFile); // Cleanup after download
});
});
});
app.listen(port, () => console.log(`App listening on port ${port}!`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment