const express = require("express"); const fs = require("fs"); const path = require("path"); const { exec } = require("child_process"); const app = express(); const PORT = 7860; const DOWNLOAD_DIR = path.join(__dirname, "downloads"); // Ensure the downloads directory exists if (!fs.existsSync(DOWNLOAD_DIR)) { fs.mkdirSync(DOWNLOAD_DIR, { recursive: true }); } // Function to extract filename from URL const getFileName = (fileUrl) => path.basename(new URL(fileUrl).pathname); // Function to delete file after a delay (default: 10 minutes) const scheduleFileDeletion = (filePath, delay = 600000) => { setTimeout(() => { fs.unlink(filePath, (err) => { if (!err) { console.log(`✅ Deleted file: ${filePath}`); } }); }, delay); }; // Function to download file using aria2c (super fast) const downloadFile = (fileUrl, filePath) => { return new Promise((resolve, reject) => { const aria2Cmd = `aria2c -x 16 -s 16 -k 1M -o "${filePath}" "${fileUrl}"`; console.log(`🚀 Starting fast download: ${aria2Cmd}`); exec(aria2Cmd, (error, stdout, stderr) => { if (error) { console.error(`❌ Download Error: ${error.message}`); return reject(error); } console.log(`✅ Download Complete: ${filePath}`); resolve(filePath); }); }); }; // Function to convert MKV to MP4 using FFmpeg (Fast Conversion) const convertToMp4 = (inputPath, outputPath) => { return new Promise((resolve, reject) => { const ffmpegCmd = `ffmpeg -i "${inputPath}" -c:v copy -c:a aac -b:a 192k -y "${outputPath}"`; console.log(`🚀 Running FFmpeg: ${ffmpegCmd}`); exec(ffmpegCmd, (error, stdout, stderr) => { if (error) { console.error(`❌ FFmpeg Error: ${error.message}`); return reject(error); } console.log(`✅ FFmpeg Output: ${stdout || stderr}`); resolve(outputPath); }); }); }; // API Route to download, convert, and serve .mp4 files app.get("/download", async (req, res) => { const fileUrl = req.query.url; if (!fileUrl) { return res.status(400).json({ error: "❌ Missing URL parameter" }); } try { console.log(`⬇️ Downloading: ${fileUrl}`); const originalFilename = getFileName(fileUrl); const originalFilePath = path.join(DOWNLOAD_DIR, originalFilename); const isMkv = originalFilename.toLowerCase().endsWith(".mkv"); const mp4Filename = isMkv ? originalFilename.replace(/\.mkv$/, ".mp4") : originalFilename; const mp4FilePath = path.join(DOWNLOAD_DIR, mp4Filename); // Start fast download with aria2c await downloadFile(fileUrl, originalFilePath); // Dynamically get the host URL from request headers const hostUrl = `${req.protocol}://${req.get("host")}`; if (isMkv) { // Convert MKV to MP4 try { await convertToMp4(originalFilePath, mp4FilePath); fs.unlinkSync(originalFilePath); // Remove original MKV after conversion } catch (error) { return res.status(500).json({ error: "❌ Conversion failed" }); } } const servedUrl = `${hostUrl}/files/${mp4Filename}`; scheduleFileDeletion(mp4FilePath); // Auto-delete after 10 minutes console.log(`✅ Download & conversion complete: ${servedUrl}`); return res.json({ message: "Download & conversion complete", fileUrl: servedUrl }); } catch (error) { console.error("❌ Error:", error.message); return res.status(500).json({ error: "Download or conversion failed" }); } }); // Serve files from downloads directory (publicly accessible) app.use("/files", express.static(DOWNLOAD_DIR)); // Start the server app.listen(PORT, () => { console.log(`🚀 Server running on port ${PORT}`); });