| const express = require('express'); | |
| const { exec } = require('child_process'); | |
| const app = express(); | |
| const PORT = 7860; | |
| app.get('/', (req, res) => { | |
| res.send('🎬 Welcome to yt-dlp API'); | |
| }); | |
| app.get('/download', async (req, res) => { | |
| const url = req.query.url; | |
| if (!url) return res.status(400).json({ error: 'Missing url parameter' }); | |
| const command = `yt-dlp -J "${url}"`; | |
| exec(command, { maxBuffer: 1024 * 1000 }, (err, stdout, stderr) => { | |
| if (err) return res.status(500).json({ error: stderr || err.message }); | |
| try { | |
| const data = JSON.parse(stdout); | |
| res.json({ | |
| title: data.title, | |
| uploader: data.uploader, | |
| duration: data.duration, | |
| formats: data.formats.map(f => ({ | |
| format_id: f.format_id, | |
| ext: f.ext, | |
| resolution: f.resolution || f.height + 'p', | |
| url: f.url | |
| })) | |
| }); | |
| } catch (e) { | |
| res.status(500).json({ error: 'Failed to parse yt-dlp output' }); | |
| } | |
| }); | |
| }); | |
| app.listen(PORT, () => { | |
| console.log(`API server running on http://localhost:${PORT}`); | |
| }); |