Issue: Attempting to stream data to an express server using ffmpeg results in an error indicating that the output

I'm having some trouble streaming ytdl audio to my express server with ffmpeg as I keep receiving the error message "Output stream closed."

Here is the code snippet I am using:

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

Answer №1

My experience involved encountering a timeout issue:

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

Upon examining the fluent-ffmpeg processor.js file, I found the following function:

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

I decided to comment out the entire function as a potential solution. While not definitive, it successfully resolved the issue for me.

Answer №2

Using the Content-Disposition header can be beneficial:

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

Source: https://example.com/github-issue-470

However, this approach may not be ideal

Answer №3

For me, the issue arose when I attempted to retrieve an array of data by simultaneously running multiple instances of ffmpeg through piping. This error likely stems from conflicts caused by FFmpeg trying to generate multiple streams concurrently, resulting in interference.

To resolve this problem, I opted to make individual requests instead. While not a direct fix, it served as a workaround worth considering.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transferring information to the frontend using Express and retrieving it

Having just started working with express, I find myself a bit confused about how to handle data on the front end. Take a look at the code snippet below: exports.init = function(req, res){ if (req.isAuthenticated()) { //res.send(); var userId = ...

Sending an incomplete body in POST requests to a NodeJS API using Postman may lead to unexpected results, however, this issue may not occur

I am experiencing an issue with receiving empty bodies in POST requests through Postman in a NodeJS API. When I send the request as "form" or as "raw" with "text", I receive exactly this: {}, but if I send it as a "JSON" in raw, it simply freezes at "loadi ...

Deploy Node.js on a Debian server hosted on Google Compute Engine

Currently, I am operating a Debian server on Google Compute Engine using a host called example.com. My goal is to run a node.js app within a specific directory on this server, for instance, example.com/mynodeapp. Fortunately, the necessary components such ...

Importing Firebase outside of modules is not possible

I'm frustrated trying to follow the documentation precisely but ending up with unexpected results. My goal is to integrate Firebase into my express.js API so that the app can fetch data directly from the database. // Initialize Firebase const fu ...

Sending angularjs form data to nodejs Express (Failure in request)

Recently, I started learning AngularJS and decided to create a simple login feature using AngularJS on the front end and Nodejs on the server side. Security is not a priority for me at the moment, as I am focused on understanding how to make HTTP posts. I ...

Indicate whether all properties should be designated as mandatory or optional in Joi validations

Currently, I am creating an Express API and incorporating @hapi/joi for validation purposes. However, I have encountered a dilemma: when validating a new user, all properties in the schema must be mandatory. On the other hand, when a client wants to update ...

The Body Parser is having trouble reading information from the form

I'm struggling to understand where I'm going wrong in this situation. My issue revolves around rendering a form using a GET request and attempting to then parse the data into a POST request to display it as JSON. app.get('/search', (re ...

Tips for effectively structuring express routes

Struggling to understand how to effectively utilize the /:variable notation. My current setup includes this layout: router.route("/").get() router.route("/:id").get().put().post().delete() router.route("/auth").get().put().pos ...

How to manage rejections in async/await within the Array#map method

In my Node 8.1.2 project, I encountered a scenario where one file is calling another file's function within a map structure. While in a real example, I would normally use Promise.all on the map, that specific implementation is not the focus of this qu ...

"Encountering socket issues while making a post request with Express, Mongoose

I'm encountering an issue where my post request using Postman to localhost:5000/api/auth/register keeps getting hung up on the socket. Can anyone offer some insight into what might be causing this problem? Thank you in advance! (I am utilizing MongoDB ...

The Express request parameter ID throws an error due to the index expression not being of type 'number', causing the element to implicitly have an 'any' type

Is there a way to assign a type to an ID request parameter? It appears that the types of Express treat request params as any. This is the code snippet where I am trying to access the ID from the request: const repository: Repository = { ...reposit ...

Difficulty arises when trying to capture a node event emitter within a server-sent events stream, with the server generating a listener for each user currently active

I have implemented a function to continuously query a third-party API and I am using server-sent events in conjunction with an event emitter to send updates to user clients: // server.js const interval = 60 * 60 * 1000; setInterval(async () => { cons ...

Transferring information using express

Currently, my Express server is up and running and it's set to send an HTML file from the public folder of my project. The issue arises when I try to initiate a request from a client script linked in this HTML file to send data back to the server. Des ...

Can you insert a Pug anywhere within a document?

I've been following a tutorial on Jade (now Pug) at https://www.youtube.com/watch?v=l5AXcXAP4r8. The instructor placed an 'extends filename' in the middle of the index.jade file at 24:25. However, when I tried to execute it with Pug, I recei ...

Despite enabling CORS in Express, I am still encountering CORS errors

Utilizing both React and Express, below is the code snippet in Express: const express = require("express") const bodyParser = require("body-parser") const { connection } = require("./db/connection") const user = require(" ...

Using Node.js and Express to retrieve data from a MySQL database and update a table

My .ejs file contains a form that sends data to a MySQL database and then displays two tables with the retrieved data on the same page. However, I am facing an issue where the tables do not refresh after submitting the form, requiring me to restart the ser ...

Tips for properly implementing a bcrypt comparison within a promise?

Previously, my code was functioning correctly. However, it now seems to be broken for some unknown reason. I am using MariaDB as my database and attempting to compare passwords. Unfortunately, I keep encountering an error that says "Unexpected Identifier o ...

The issue with Mocha/ Supertest is that it fails to exit once the tests are completed

While using the mocha testing framework, I encountered an issue where the test does not exit after running. I have tried using Promises and async await without success. Including --exit at the end of the mocha command helps to exit, but I am determined to ...

What could be the reason for the esm loader not recognizing my import?

Running a small express server and encountering an issue in my bin/www.ts where I import my app.ts file like so: import app from '../app'; After building the project into JavaScript using: tsc --project ./ and running it with nodemon ./build/bin ...

What could be causing my node.js terminal to return duplicate responses for a single request?

// app.js code const express = require("express"); const app = express(); const bodyParser = require("body-parser"); app.set("view engine","ejs"); app.use(bodyParser.urlencoded({extended:true})) app.get("/",function(req,res){ res.render("home"); }); ...