zhuker / lamejs

mp3 encoder in javascript
Other
820 stars 200 forks source link

Working With MediaRecorder API #68

Open Deveosys opened 4 years ago

Deveosys commented 4 years ago

Hi,

I'm trying to encode the webm audio result from MediaRecorder API in Chrome into mp3. But all I got is a short noisy audio.

Without encoding, the given webm audio is good. I can write it to file or play in a audio element.

this.mediaRecorder.ondataavailable = e => {
    this.chunks.push(e.data);
};
let fileReader = new FileReader();
fileReader.onload = () => {
    let mp3Data = [];
    let samples = new Int16Array(fileReader.result as ArrayBuffer);
    let mp3encoder = new lamejs.Mp3Encoder(1, 48000, 128);
    let mp3Buffer = mp3encoder.encodeBuffer(samples);
    if (mp3Buffer.length > 0) mp3Data.push(mp3Buffer);
    mp3Buffer = mp3encoder.flush();
    if (mp3Buffer.length > 0) mp3Data.push(mp3Buffer);
    // mp3Data should be ready here ? 
};
fileReader.readAsArrayBuffer(new Blob(this.chunks, { type: "audio/webm" }));

Could you help me figuring out where I am wrong ? Thanks !

geeee commented 4 years ago

Encoder accepts PCM audio only. You need to decode webm first.

seafronthu commented 2 months ago

Encoder accepts PCM audio only. You need to decode webm first.

How to decode webm?