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

拥有回忆 提交于 2019-12-23 08:57:21

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/53500454/how-to-setup-codec-samplerate-and-bitrate-on-an-audio-blob-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!