1

I face a particular challenge that I can not solve. I have a remote URL (ex. https://www.somesite.com/file.mp3) and I would like it to be streamed to a WebSocket.

Some may say I can just stream from the request, but there are particular settings on the createReadStream that need to be set.

Another challenge is that the WebSocket only accepts .wav files, so I must convert the .mp3 to a .wav somewhere on the line. My main goal is speed and performance, and to conserve time and resources is crucial.

const ws = new WebSocket('wss://ws.somesite.com/websocket');

var readStream = fs.createReadStream('./audio_file.wav', {
  flags: 'r',
  highWaterMark: 4096
});

readStream.on('data', async chunk => {
  ws.send(chunk, {
    binary: true, 
    mask: true
  })
})

Optimally, in a perfect world I could do something like this (yes, I know this code does not work and is an example/psuedocode).

const ws = new WebSocket('wss://ws.anothersite.com/websocket');

// Made source a remote URL.
var readStream = fs.createReadStream('https://www.somesite.com/file.mp3', {
  flags: 'r',
  highWaterMark: 4096,
  convert: 'wav' // Added this.
});

readStream.on('data', async chunk => {
  ws.send(chunk, {
    binary: true, 
    mask: true
  })
})

Thank you for ANY input/help you can make!

Abdel Seaf
  • 39
  • 8
  • You just need a converter that supports streams – Anatoly Dec 10 '20 at 20:22
  • @Anatoly That would be ideal, yes. But what is something I can use? That is the point of the question. – Abdel Seaf Dec 10 '20 at 20:28
  • Does https://stackoverflow.com/a/40233702/1376618 suit you? – Anatoly Dec 10 '20 at 20:51
  • @Anatoly I am going to do some research on how to use that for remote files, but that should suit that conversion process. But the idea is that I do not need to touch the filesystem at all, so I don't know how I would accomplish such a task. – Abdel Seaf Dec 10 '20 at 20:59
  • Don't examples on a package page convince you? `ffmpeg(fs.createReadStream('/path/to/file.avi'))` for instance – Anatoly Dec 10 '20 at 21:08
  • @Anatoly I understand how to convert files from a stream, but how do I copy the options "highWaterMark" being the most important of them all. – Abdel Seaf Dec 11 '20 at 01:25
  • I just shown that `ffmpeg` can take data from a stream (it can be any stream and not necessary a file stream). So you can pass your stream from a remote URL and then use a stream from `ffmpeg` to send data to web socket – Anatoly Dec 11 '20 at 06:20

0 Answers0