Python语音识别错误转换mp3文件
Posted
技术标签:
【中文标题】Python语音识别错误转换mp3文件【英文标题】:Python speech recognition error converting mp3 file 【发布时间】:2017-01-07 00:19:33 【问题描述】:我第一次尝试音频到文本。
import speech_recognition as sr
r = sr.Recognizer()
with sr.AudioFile("/path/to/.mp3") as source:
audio = r.record(source)
当我执行上述代码时,出现以下错误,
<ipython-input-10-72e982ecb706> in <module>()
----> 1 with sr.AudioFile("/home/yogaraj/Documents/Python workouts/Python audio to text/show_me_the_meaning.mp3") as source:
2 audio = sr.record(source)
3
/usr/lib/python2.7/site-packages/speech_recognition/__init__.pyc in __enter__(self)
197 aiff_file = io.BytesIO(aiff_data)
198 try:
--> 199 self.audio_reader = aifc.open(aiff_file, "rb")
200 except aifc.Error:
201 assert False, "Audio file could not be read as WAV, AIFF, or FLAC; check if file is corrupted"
/usr/lib64/python2.7/aifc.pyc in open(f, mode)
950 mode = 'rb'
951 if mode in ('r', 'rb'):
--> 952 return Aifc_read(f)
953 elif mode in ('w', 'wb'):
954 return Aifc_write(f)
/usr/lib64/python2.7/aifc.pyc in __init__(self, f)
345 f = __builtin__.open(f, 'rb')
346 # else, assume it is an open file object already
--> 347 self.initfp(f)
348
349 #
/usr/lib64/python2.7/aifc.pyc in initfp(self, file)
296 self._soundpos = 0
297 self._file = file
--> 298 chunk = Chunk(file)
299 if chunk.getname() != 'FORM':
300 raise Error, 'file does not start with FORM id'
/usr/lib64/python2.7/chunk.py in __init__(self, file, align, bigendian, inclheader)
61 self.chunkname = file.read(4)
62 if len(self.chunkname) < 4:
---> 63 raise EOFError
64 try:
65 self.chunksize = struct.unpack(strflag+'L', file.read(4))[0]
我不知道我做错了什么。有人可以告诉我上面的代码有什么问题吗?
【问题讨论】:
看起来 Speech_recognition 不接受 mp3 文件。可以试试WAV文件看看是不是文件格式吗? 尝试使用WAV文件格式,出现同样的错误。我想将 mp3 转换为文本。我应该使用哪个模块? 【参考方案1】:Speech recognition
支持 WAV 文件格式。
这是一个使用 speech_recognition
的示例 WAV 到文本程序:
示例代码(Python 3)
import speech_recognition as sr
r = sr.Recognizer()
with sr.AudioFile("woman1_wb.wav") as source:
audio = r.record(source)
try:
s = r.recognize_google(audio)
print("Text: "+s)
except Exception as e:
print("Exception: "+str(e))
输出:
Text: to administer medicine to animals is frequency of very difficult matter and yet sometimes it's necessary to do so
使用的 WAV 文件 URL:http://www-mobile.ecs.soton.ac.uk/hth97r/links/Database/woman1_wb.wav
【讨论】:
在 python3 中运行良好。如果我想将 mp3 文件转换为文本,我应该使用什么模块? @Yogaraj 您找到将 mp3 文件转换为文本的方法了吗?如果可以,你能帮帮我吗? 这很好用,如果您使用pydub
(此答案:***.com/questions/3049572/…),您可以将各种音频格式转换为 WAV 以输入到speech_recognition
。我仍在努力让recognize_sphinx() 正常工作,因为谷歌的API 限制为每天50 次调用,每个文件最多60 次。
不是 mp3 文件【参考方案2】:
这是错的:
语音识别只支持WAV文件格式。
但这是关于如何获得 MP3 到文本的更完整的答案:
这是一个处理函数,它使用speech_recognition
和pydub
将 MP3 转换为 WAV,然后使用 Google 的 Speech API 转换为 TEXT。它将 MP3 文件分成 60 多个部分以适应谷歌的限制,并允许您在一天内播放大约 50 分钟的音频。但它会在 50 次 API 调用后阻止您。
from pydub import Audiosegment # uses FFMPEG
import speech_recognition as sr
from pathlib import Path
#from pydub.silence import split_on_silence
#import io
#from pocketsphinx import AudioFile, Pocketsphinx
def process(filepath, chunksize=60000):
#0: load mp3
sound = AudioSegment.from_mp3(filepath)
#1: split file into 60s chunks
def divide_chunks(sound, chunksize):
# looping till length l
for i in range(0, len(sound), chunksize):
yield sound[i:i + chunksize]
chunks = list(divide_chunks(sound, chunksize))
print(f"len(chunks) chunks of chunksize/1000s each")
r = sr.Recognizer()
#2: per chunk, save to wav, then read and run through recognize_google()
string_index =
for index,chunk in enumerate(chunks):
#TODO io.BytesIO()
chunk.export('/Users/mmaxmeister/Downloads/test.wav', format='wav')
with sr.AudioFile('/Users/mmaxmeister/Downloads/test.wav') as source:
audio = r.record(source)
#s = r.recognize_google(audio, language="en-US") #, key=API_KEY) --- my key results in broken pipe
s = r.recognize_google(audio, language="en-US")
print(s)
string_index[index] = s
break
return string_index
text = process('/Users/mmaxmeister/Downloads/UUCM.mp3')
我的测试 MP3 文件是来自 archive.org 的布道: https://ia801008.us.archive.org/24/items/UUCMService20190602IfWeBuildIt/UUCM%20Service%202019-06-02%20-%20If%20We%20Build%20It.mp3
这是返回的文本(每行是 60 秒的音频):
13 chunks of 60.0s each
please join me in a spirit of prayer Spirit of Life known in many ways by a million names gracious Spirit of Life unfolding never known in its fullness be with us hear our cries for deliverance dance with us in exultation hold us when we fall keep before us the reality that every day is a gift to be unwrapped a gift to help discover why we live why we are cast Here and Now
Austin teaches us that the days come and go like muffled veiled figures Sent From A Distant friendly party but they say nothing and if we do not use the gifts they bring us they will carry them away as silently as they came through buying source of all Bend us towards gratitude and compassion Modern Life demands much misery and woe get created all around us but there is more much more show us that much more belongs to us to light Dawns on those who live love and sing the truth Joy John's on those who humbly toiled
do what is just so you who can shine pass on your light when it Dawns on you and let us all find the space to see Life as a gift to see our days as blessings and let us return life's gift and promise with grateful hearts and acts of kindness in the name of all the each of us teams holiest within our hearts we pray Amon
my character at least when I was younger I'm sure I don't really do this anymore the most challenging aspect of my character is that I want wisdom yesterday I don't want to have to learn something now I should have known it already right I used to drive my poor parents crazy as they tried to help me with my homework my father with my math my mother with my spelling if I didn't know the answer as soon as the problem was in front of me I would get angry frustrated with myself how come I didn't already know that I'm supposed to be wise only child I wonder if that has anything to do around the room has been throughout my life
but I still see it manifest in one particular aspects of my being I want us all to know how to love one another perfectly with wisdom already we should have learned that yesterday I want the Beloved Community right now and it frustrates me to no end that it isn't here what was that song that we saying after the prayer response how could anyone ever tell you you were anything less than beautiful how do we do that how do we tell ourselves that and others that we are not all the time how do we do that how come we haven't figured that out yesterday there's been a great Salve and corrective
to this challenge of my personality that I found in Community First the bomb the South I find the in community in this started when I was a youth in my youth group when we were ten people sitting on a floor together on pillows telling one another about what we've been through that week and how much pain we were carrying and how much we needed one another I found in that youth group with just 10 of us are sitting on the floor that we could be the Beloved Community for one another if only just for one hour a week sometimes just for 5 minutes sometimes just for a moment that was the Sal that was the bomb I realize that maybe we can't do it all the time but we can do it in moments and in spaces and that only happen for me in the space
community that community that we created with one another and the corrective to my need to have things everything done yesterday that also happens in community because Community is the slowest place on Earth We're going to have our annual meeting later let's see how slow that's going to be but the truth of the matter is that even in that slowness when you're working really hard to set up or cleanup connection Cafe when you're trying to figure out how to set up membership so that we actually do talk to everybody who comes through the doors right when you're doing that work of the Care team and that big list of all the different people that we need to reach out to and and we have to figure out how we reached out to all of them and who's done it
when you're waiting for the sermon to be over in all of these waiting times and all of these phases of process what I've learned in that slowness something amazing something remarkable is happening we are dedicating ourselves over and over again to still being together cuz it's not always easy because we're all broken and we're all whole because sometimes this is incredibly difficult and painful but when we're in those times what we're doing is we're saying it's worth it something about this matters to me and to all of us and Becca's got a great story to illustrate the this comes from
used to have a radio show in Boston maybe you heard him at some point Unitarian Universalist Minister from Boston 66 driving lessons and five road test she was 70 years old and had never driven a car but on July 25th 1975 she went to the Rockland County driving school and took her first lesson her husband had already had heart trouble and might someday be unable to drive if that happened she wanted to be the one to do the shopping and Shake him to the doctor she began the slow and painful process of learning to start stop turn into traffic back up after 5 difficult month she took the driving test
before ever she wrote in her diary and was not nervous she just a test a month later and slumped again I did everything wrong she told her diary demonic in August of 1976 she resumed the lessons with the Eaton driving school and took her third road test in October with half the world praying for me
she took a double driving lesson the next day and parallel park 6 times after three more lessons she took her Fifth and final test on January 21st 1977 and passed she had spent $860 on 66 plus 5 road test and at the age of 71 she had her license good three years later he did for several months she was the one who drove to the hospital Supermarket Pharmacy in church when we were children
someone rafter do another instructed by the spider's persistence Robert brute Robert Bruce left the Hut gathered his men and defeated the dance my mother and body of the story but it was not just persistence that moved her but love for the man who was her other self do you want to know what love is it's 66 driving lessons and five road tests and a very tough lady
who won't give up because her love is that great thank you all for bringing this to this beloved in moments community
那是相当不错的免费。不幸的是,如果我想转录数小时的内容,google-cloud API 版本非常昂贵。
【讨论】:
以这种方式获取FileNotFoundError: [Errno 2] No such file or directory: 'ffprobe'
'ffprobe' 是 ffmpeg 库的一部分,python 使用但不是 python 包本身。在 Mac 或 Linux 上,尝试sudo apt-get install ffmpeg
安装它@MinkeshJain,或使用自制软件brew install ffmpeg
进行安装以上是关于Python语音识别错误转换mp3文件的主要内容,如果未能解决你的问题,请参考以下文章