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}`);
});
在此服务器上转换期间出现错误。我尝试在 replit 上运行它,但始终给出错误。我对此很陌生,无法弄清楚这个问题。 CHATGPT-4 无法提供帮助。 有人可以帮我吗???
ChatGPT 表示 - “转换期间错误:输入流错误:状态代码:410”表示“已消失”错误,这意味着您尝试访问的资源在指定的 URL 不再可用。当 YouTube 内容已被删除或由于 YouTube 政策的更改或特定视频的可用性而无法再访问时,可能会发生这种情况。
你有什么解决办法吗?我也在尝试做同样的事情