错误:尝试使用 ffmpeg 流式传输到 Express 服务器时输出流已关闭

问题描述 投票:0回答:2

我正在使用 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 })
})
express ffmpeg streaming fluent-ffmpeg ytdl
2个回答
0
投票

就我而言,我遇到了超时问题:

    Error: Output stream closed
    at Timeout._onTimeout (C:\MYPATH\node_modules\fluent-ffmpeg\lib\processor.js:491:25)

我访问了 Fluent-ffmpeg

processor.js
文件,出现了以下功能:

    setTimeout(function() {
      emitEnd(new Error('Output stream closed'));
      ffmpegProc.kill();
    }, 20);

我注释掉了整个函数。我不知道这是否是最好的解决方案,但它对我来说非常有用。


0
投票

设置 Content-Disposition 标头可能会帮助您:

res.set('Content-Disposition', `attachment; filename=${fileName}`),

参考:https://github.com/ Fluent-ffmpeg/node- Fluent-ffmpeg/issues/470#issuecomment-160200925

但这不是一个很好的解决方案

© www.soinside.com 2019 - 2024. All rights reserved.