Filip
Filip

Reputation: 11

Live stream mp3 in Node

So I'm trying to capture audio and then return a audio live stream in NodeJS. I want the response to be like this example. Notice how the browser opens up the file in a media player.

My current solution looks like this but it will render as "no video with supported format and mime type found"

const express = require("express");
const app = express();
const AudioRecorder = require("node-audiorecorder");


app.get("/stream.wav", function (req, res) {
  res.set({
    "Content-Type": "audio/wav",
  });
  audioRecorder.start().stream().pipe(res);
});

The live stream works if I write the stream to a file first (code below) and open said file with VLC. It will continuously write to the file and VLC wont have any problem to follow along.

const fileStream = fs.createWriteStream(fileName, { encoding: "binary" });

// Start and write to the file.
//audioRecorder.start().stream().pipe(fileStream);

Ive been scratching my head all day but I'm stuck. Any help would be greatly appreciated!

Upvotes: 1

Views: 2142

Answers (2)

Filip
Filip

Reputation: 11

I solved the error by adding fluent-ffmpeg like this:

var stream = ffmpeg().input(audioRecorder.start().stream()).toFormat("mp3");
app.get("/stream.mp3", function (req, res) {
  res.type("mp3");
  stream.pipe(res);
});

There is a delay around 10s that ill have to work on.

Upvotes: 0

marko-36
marko-36

Reputation: 1496

I am not sure about the output audio format. Is is MP3 or WAV? Your answers suggest both. In the first case, MIME type should be audio/mpeg.

Also should replacing res.set by any of the following help?

res.setHeader('Content-Type', contentType)

res.writeHead(statusCode,{'Content-Type': contentType}).

contentType is "audio/wav", statusCode should be 200.

Upvotes: 1

Related Questions