我正在使用ffmpeg将ytdl音频流式传输到我的express服务器,但我总是得到"输出流关闭";
这是我的代码
const express = require('express')
const ffmpeg = require('fluent-ffmpeg')
const ytdl = require('ytdl-core')
const app = express()
app.set('/:id', async (req, res) => {
res.set('Content-Type', 'audio/mp3')
ffmpeg(await ytdl(req.params.id, { format: 'audioonly', quality: 'highestaudio' }))
.toFormat('mp3')
.pipe(res, { end: true })
})
在我的案例中,我遇到了超时问题:
Error: Output stream closed
at Timeout._onTimeout (C:MYPATHnode_modulesfluent-ffmpeglibprocessor.js:491:25)
我访问了fluent ffmpegprocessor.js
文件,出现了以下功能:
setTimeout(function() {
emitEnd(new Error('Output stream closed'));
ffmpegProc.kill();
}, 20);
我把整个功能都注释掉了。我不知道这是否是最好的解决方案,但对我来说效果很好。
设置Content-Disposition标头可能会对您有所帮助:
res.set('Content-Disposition', `attachment; filename=${fileName}`),
参考:https://github.com/fluent-ffmpeg/node-fluent-ffmpeg/issues/470#issuecomment-160200925
但这不是一个很好的解决方案