首页 > 解决方案 > Python:如何使用 speech_recognition 或其他模块将 base64 音频字符串转换为文本?

问题描述

我有类似的 base64 音频字符串data:audio/mpeg;base64,//OAxAAAAANIAAAAABhqZ3f4StN3gOAaB4NAUBYZLv......,我试图在 Python 中使用 base64 模块将 base64 转换为 wav 文件:

    decode_bytes = base64.b64decode(encoding_str)
    with open(file_name + '.wav', "wb") as wav_file:
        wav_file.write(decode_bytes)

然后我尝试使用 Speech_recognition 模块将音频转换为文本,它给出了以下错误:

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

这个问题有解决方案吗?

标签: python-3.xaudiobase64speech-recognition

解决方案


似乎您的音频文件是来自 mime-type - 的 mp3 audio/mpeg。您需要将其保存为 mp3

decode_bytes = base64.b64decode(encoding_str)
    with open(file_name + '.mp3', "wb") as wav_file:
        wav_file.write(decode_bytes)

并使用pydub或 FFmpeg 将 mp3 转换为 wav 格式,然后将此 wav 文件提供给 Speech_recognition 模块。


推荐阅读