Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 740
Ravi Yadav Avatar asked Nov 27 '18 13:11

Ravi Yadav


1 Answers

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

like image 75
Andrii Muzalevskyi Avatar answered Nov 13 '22 10:11

Andrii Muzalevskyi