node-ytdl-core icon indicating copy to clipboard operation
node-ytdl-core copied to clipboard

500 Internal Server Error When Streaming Audio from YouTube URL

Open fsholehan opened this issue 7 months ago • 3 comments

I have an endpoint that streams audio from a YouTube URL using ytdl-core. It works fine locally, but I encounter a 500 Internal Server Error on the server. Here's my code:

const express = require('express');
const ytdl = require('ytdl-core');

const app = express();

app.get('/audio', (req, res) => {
    const videoUrl = req.query.url;

    if (!videoUrl) {
        return res.status(400).send('Video URL is required');
    }

    res.header('Content-Type', 'audio/mpeg');

    ytdl(videoUrl, { filter: 'audioonly' })
        .on('error', (err) => {
            console.error('Error:', err);
            res.status(500).send('Error streaming audio');
        })
        .pipe(res);
});

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

Could someone help identify potential causes and solutions for this issue?

fsholehan avatar Jul 26 '24 03:07 fsholehan