Unable to segment audio captured from MediaStreams in the browser

14 Views Asked by At

I am using Web Audio API (with MediaRecorder or RecordRTC) to capture audio from the microphone. Let's take the case of MediaRecorder (although it works the same for RecordRTC). MediaRecorder's ondataavailable callback gives a single slice/chunk of audio data for the duration specified by the timeslice parameter to MedaiRecorder.start(). I get a continuous stream of these slices once MediaRecorder.start() is called. Instead of collecting 10mins worth of audio and then saving it to a file, I want to split it into 20second long segments while the audio is being captured (not after all the audio is captured). I am using the ogg format. The problem is that only the first slice/chunk has the relevant audio headers. As a result the first 20 sec segment works fine, but the subsequent 20 sec segments fail to be recognized as valid audio by ffmpeg (and other softwares).

My hacky workaround for this, which seems to work, is as follows: I set timeslice to 500ms (small enough that the first slice contains the header data, but not much audio data) I saved the first slice, which i prefix to every subsequent 20sec long array of slices. While this works fine, I feel like there should be a better way of doing this.

Any ideas or pointers would be greatly appreciated.

0

There are 0 best solutions below