Ravi Yadav
Ravi Yadav

Reputation: 737

How to setup codec, samplerate and bitrate on an audio blob in javascript?

I just created a blob:

const audioBlob = new Blob(audioChunks, { 'type' : 'audio/wav; codecs=0' });

and sent it to the backend in base64 format. I saved this into a file named "test.wav" using the following code:

await writeFile('./temp/test.wav', Buffer.from(filename.replace('data:audio/wav; codecs=0;base64,', ''), 'base64'), 'base64');

On the output "test.wav" file, I get the codec as opus, bitrate=N/A and sample rate=48000. I want to change these values to codec=wav, bitrate=256kbps and sample rate=16000. How to achieve it in node(or angular)?

Here is a link for my frontend code.

Upvotes: 11

Views: 8067

Answers (1)

Andrii Muzalevskyi
Andrii Muzalevskyi

Reputation: 3329

This line just provides mime information but doesn't affect the actual output

const audioBlob = new Blob(audioChunks, { 'type' : 'audio/wav; codecs=0' });

To select correct audio codec and bitrate please start recording with following options

var options = {
  audioBitsPerSecond : 128000,
  mimeType : 'audio/ogg'
}
var mediaRecorder = new MediaRecorder(stream, options);

As far as I know ogg codec is supported by default in WebRTC, so it is cross browser compatible

Later, on the backend side, you will need to transform ogg audio stream to anything else you want using for example fluent-ffmpeg

Upvotes: 9

Related Questions