音频文件无法读取为PCM WAV,AIFF / AIFF-C或本机FLAC

时间:2020-03-02 07:42:08

标签: javascript django python-3.x speech-recognition speech-to-text

我正在使用.wav格式的javascript录制语音:

        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)
              }
            }
          }

发送文件以使用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('/')


错误:-

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。音频文件正在保存,我可以清楚地听到声音/声音

音频文件:https://drive.google.com/open?id=17ucX9xRG0x5-JEtZDFaotSNLlcRs0jZc

1 个答案:

答案 0 :(得分:1)

尽管文件扩展名为wav,但文件格式为WebM,而不是wav。您可以使用file命令检查文件类型:

$ file name.wav 
name.wav: WebM

您需要设置MediaRecorder的mimeType来记录wav:

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

任何浏览器均可能不支持它。