python-3.x - 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
这个问题有解决方案吗?
解决方案
似乎您的音频文件是来自 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 模块。
推荐阅读
- java - 如何记录 JTable 单元格何时被编辑
- r - 如何在没有任何标识符的情况下在另一个数据框下方添加一个数据框
- sdk - Gigya removeLoginEmails 删除最后的登录 ID
- android - 使用其模型检索 FirebaseRecyclerAdapter 中的孩子
- c# - 如何从 ASP.NET 中的 Mysql 存储过程调用输出参数
- java - 使用 Java 中的 Gmail API 创建带有 PDF 文件作为附件的草稿
- python - 如何在不损坏文本的情况下去除图像中的背景噪音?
- ruby-on-rails - 自动提交表单 - 嵌套路由
- python - 按两列分组并使用第三列作为值而不使用 pivot_table
- ios - 如何使对象放置在正确的位置并根据屏幕大小自动调整大小?