2

I'm getting to stream ytdl audio to my express server using ffmpeg but I'm always getting "Output stream closed"

there's my code

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 })
})

2 Answers2

0

In my case, I had the timeout problem:

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

I accessed the fluent-ffmpeg processor.js file and the following function appeared:

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

I commented out the whole function. I don't know if it's the best solution, but it worked great for me.

0

Setting Content-Disposition header might help you:

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

Reference: https://github.com/fluent-ffmpeg/node-fluent-ffmpeg/issues/470#issuecomment-160200925

But this is not a great solution

Manuchehr
  • 11
  • 2