http://localhost:3000/download?url=https://www.youtube.com/watch?v=VIDEO_ID
npm install ytdl-core@latest
const express = require("express");
const ytdl = require("ytdl-core");
const app = express();
const PORT = 3000;
app.get("/download", async (req, res) => {
try {
const videoUrl = req.query.url;
if (!videoUrl) {
return res.status(400).json({ error: "Missing 'url' parameter" });
}
if (!ytdl.validateURL(videoUrl)) {
return res.status(400).json({ error: "Invalid YouTube URL" });
}
const info = await ytdl.getInfo(videoUrl);
const format = ytdl.chooseFormat(info.formats, { quality: "highest" });
if (!format) {
return res.status(500).json({ error: "No downloadable format found" });
}
res.json({
title: info.videoDetails.title,
download_link: format.url,
format: format.mimeType,
quality: format.qualityLabel
});
} catch (error) {
console.error("Error retrieving video:", error);
res.status(500).json({ error: "Error retrieving video information", details: error.message });
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Comments
Post a Comment