Python speech recognition error converting mp3 file
Question: My first try on audio to text.

    
    
    import speech_recognition as sr
    r = sr.Recognizer()
    with sr.AudioFile("/path/to/.mp3") as source:
         audio = r.record(source)
    

When I execute the above code, the following error occurs,

    
    
    <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]
    

I don't know what I'm going wrong. Can someone say me what I'm wrong in the
above code?


Answer: `Speech recognition` supports WAV file format. Here is a sample WAV to text
program using `speech_recognition`:

### Sample code (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))
    

### Output:

    
    
    Text: to administer medicine to animals is frequency of very difficult matter and yet sometimes it's necessary to do so
    

Used WAV File URL: <http://www-
mobile.ecs.soton.ac.uk/hth97r/links/Database/woman1_wb.wav>

