Reputation: 1
const express = require('express');
const ytdl = require('ytdl-core');
const ffmpeg = require('fluent-ffmpeg');
const ffmpegStatic = require('ffmpeg-static');
const app = express();
app.use(express.static('public'));
app.get('/videoInfo', async (req, res) => {
const { url } = req.query;
if (!ytdl.validateURL(url)) {
return res.status(400).send('Invalid URL');
}
const requestOptions = {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}
};
try {
const info = await ytdl.getInfo(url, requestOptions);
const audioFormats = ytdl.filterFormats(info.formats, 'audioonly');
const highestQualityAudio = audioFormats.reduce((prev, curr) => (prev.audioBitrate > curr.audioBitrate ? prev : curr));
res.json({
videoTitle: info.videoDetails.title,
videoThumbnail: info.videoDetails.thumbnails[info.videoDetails.thumbnails.length - 1].url,
audioFormat: highestQualityAudio
});
} catch (error) {
console.error('Error fetching video info:', error);
res.status(500).send('Failed to retrieve video info.');
}
});
app.get('/download', (req, res) => {
const { url } = req.query;
function downloadAndConvertAudio(retryCount = 0) {
const requestOptions = {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}
};
res.header('Content-Disposition', 'attachment; filename="audio.mp3"');
const videoStream = ytdl(url, { quality: 'highestaudio', requestOptions: requestOptions });
ffmpeg(videoStream)
.setFfmpegPath(ffmpegStatic)
.audioBitrate(128)
.toFormat('mp3')
.on('error', (error) => {
console.error('Error in ffmpeg conversion:', error);
if (error.message.includes('403') && retryCount < 3) {
console.log('Retrying download...');
setTimeout(() => downloadAndConvertAudio(retryCount + 1), 2000);
} else {
res.status(500).send(`Error during conversion: ${error.message}`);
}
})
.pipe(res);
}
downloadAndConvertAudio();
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
Getting Error during conversion on this server. I tried running it on replit but giving the error all the time. I'm new to this and can't figure out the issue. CHATGPT-4 couldn't help. Can someone help me with this???
ChatGPT said - The "Error during conversion: Input stream error: Status code: 410" indicates a "Gone" error, which means the resource you were trying to access is no longer available at the specified URL. This can occur in situations where YouTube content has been removed or is no longer accessible due to changes in YouTube's policy or the specific video's availability.
Upvotes: 0
Views: 498