Extracting audio from a video file
Ok, I actually had the answer already. Raw audio data is huge, that's why it's even greater than the video itself in size.
var offlineAudioContext = new OfflineAudioContext(numberOfChannels, sampleRate * duration, sampleRate);
var soundSource = offlineAudioContext.createBufferSource();
...
reader.readAsArrayBuffer(blob); // video file
reader.onload = function () {
var videoFileAsBuffer = reader.result; // arraybuffer
audioContext.decodeAudioData(videoFileAsBuffer).then(function (decodedAudioData) {
myBuffer = decodedAudioData;
soundSource.buffer = myBuffer;
soundSource.connect(offlineAudioContext.destination);
soundSource.start();
offlineAudioContext.startRendering().then(function (renderedBuffer) {
console.log(renderedBuffer); // outputs audiobuffer
}).catch(function (err) {
console.log('Rendering failed: ' + err);
});
});
};
After that, I was able to convert the audiobuffer (renderedbuffer) into a wav file by using audiobuffer-to-wav library. OfflineAudioContext is just needed to modify the cropped audio.
Edit: Here is the js fiddle example. decodedAudioData method will suffice if you don't want to override the audio data.