How to convert MediaRecorder stream in Django into wav without disk storage?
I am trying to convert the bytes_data received by the channels receive() method using a WebsocketConsumer. This is the JavaCode:
navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
const mediaRecorder = new MediaRecorder(stream, {mimeType: 'audio/webm' })
console.log(stream)
chatSocket.onopen = () => {
mediaRecorder.addEventListener("dataavailable", (event) => {
if (event.data.size > 0 && chatSocket.readyState == 1) {
chatSocket.send(event.data)
}
}) // chatSocket.onopen close
mediaRecorder.start(250)
}
On the server side I now want to convert the bytes_data into a wav file so that I can send these short audio snippets via speech_recognition to the recognize_google api. I tried to use pydub to convert the bytes_data but so far I had no success.
The channels GeneralConsumer(WebsocketConsumer) class now receives the bytes_data and uses this function to convert and send it to google:
def transcriber(bytes_data: bytes):
r = sr.Recognizer()
audio = AudioSegment.from_raw(
bytes_data, sample_width=2, frame_rate=16000, channels=1)
buffer = io.BytesIO()
audio.export(buffer, format="wav")
buffer.seek(0)
with sr.AudioFile(buffer) as source:
audio = sr.AudioData(source.stream.read(),
sample_rate=16000, sample_width=2)
try:
transcription = r.recognize_google(audio, language="en-USA").lower()
except sr.UnknownValueError as e:
print(e)
return transcription
But that does not work. I have tried some variations and others I simply don't understand. A comprehensive solution would be appreciated.