node-ytdl-core
node-ytdl-core copied to clipboard
500 Internal Server Error When Streaming Audio from YouTube URL
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?