Paritosh Yadav
Paritosh Yadav

Reputation: 347

Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC

I am recording voice using javascript in .wav format:

        navigator.mediaDevices.getUserMedia({audio:true})
      .then(stream => {handlerFunction(stream)})

      function handlerFunction(stream) {
            rec = new MediaRecorder(stream);
            rec.ondataavailable = e => {
              audioChunks.push(e.data);
              if (rec.state == "inactive"){


                let blob = new Blob(audioChunks,{type:'audio/wav;codecs=0'});

                sendData(blob)
              }
            }
          }

sending the file to convert it to text using speech_recognition:

    filename = "name.wav"
    print(filename)
    data = request.body
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    uploadedFile = open(filename, "wb")
    uploadedFile.write(request.body)
    uploadedFile.close()
    #os.path.join(BASE_DIR,filename)
    r = sr.Recognizer()
    file = sr.AudioFile(filename)
    with file as source:
        audio = r.record(source)
    msg = r.recognize_google(audio)
    print(msg)
    return redirect('/')


Error:-

ValueError: Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in another format

P.S. audio file is saving and I can hear sound/voice clearly well

audio file:https://drive.google.com/open?id=17ucX9xRG0x5-JEtZDFaotSNLlcRs0jZc

Upvotes: 2

Views: 3815

Answers (1)

Nikolay Shmyrev
Nikolay Shmyrev

Reputation: 25220

The file format is WebM, not wav, despite having wav extension. You can check file type with file command:

$ file name.wav 
name.wav: WebM

You need to set mimeType of the MediaRecorder to record wav:

rec = new MediaRecorder(stream);
rec.mimeType = 'audio/wav';

It might not be supported in any browser.

Upvotes: 1

Related Questions