最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

node.js - How to setup codec, samplerate and bitrate on an audio blob in javascript? - Stack Overflow

matteradmin4PV0评论

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.

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.

Share Improve this question edited Nov 29, 2018 at 9:54 Ravi Yadav asked Nov 27, 2018 at 13:08 Ravi YadavRavi Yadav 7372 gold badges10 silver badges21 bronze badges 7
  • You can't have a 256kbps bitrate on 16kHz unpressed WAV audio. Assuming mono and 16-bit samples (the usual), your bitrate would be 16000 samples/sec * 2 bytes/sample = 32000 bytes/sec. – AKX Commented Nov 29, 2018 at 8:44
  • 3 Anyway, please show us more code; where does audioChunks e from? – AKX Commented Nov 29, 2018 at 8:46
  • @AKX just added a link for front end code on stackblitz – Ravi Yadav Commented Nov 29, 2018 at 9:54
  • 1 You need to look at the AudioRecorder's mimeType attribute. You can't just assume the chunks of data you get in that dataavailable callback are something that can be interpreted as PCM WAV, and create a blob with that type. For instance, I see audio/webm;codecs=opus, i.e. OPUS in a WebM container. Once you have data in a well-known format, you can use a transcoder such as ffmpeg to convert it to another. – AKX Commented Nov 29, 2018 at 11:00
  • Which mimetype should I use to get a wav output instead of opus? audio/wav or audio/x-wav is not supported it says. – Ravi Yadav Commented Nov 29, 2018 at 12:56
 |  Show 2 more ments

1 Answer 1

Reset to default 9 +50

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 patible

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

Post a comment

comment list (0)

  1. No comments so far