File size: 1,091 Bytes
e84523b
 
 
 
 
 
 
2890325
e84523b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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}`);
});