pythonspeech-recognitionpydubaudiosegment

pydub.AudioSegment.from_file not finding file


Hi I am trying to use Google Web Speech API for Speech Recognition, I want to convert a audio file to text. I have been able to compile this code on my raspberry pi 4 but when I compile it on my Windows computer I get an error of file not found.

Here is the error:

File "C:/Users/pc/Desktop/py_scripts/omg.py", line 15, in <module>
    sound = pydub.AudioSegment.from_file('sample.mp3')
File "C:\Users\pc\AppData\Local\Programs\Python\Python38\lib\site-packages\pydub\audio_segment.py", line 651, in from_file
    file, close_file = _fd_or_path_or_tempfile(file, 'rb', tempfile=False)
File "C:\Users\pc\AppData\Local\Programs\Python\Python38\lib\site-packages\pydub\utils.py", line 60, in _fd_or_path_or_tempfile
    fd = open(fd, mode=mode)
FileNotFoundError: [Errno 2] No such file or directory: 'sample.mp3'

And here is the code I been using:

import ffmpy, urllib, pydub

sound = pydub.AudioSegment.from_file('sample.mp3')    
sound.export('sample.wav', format="wav")
sample_audio = sr.AudioFile('sample.wav')
print("audio saved")

r = sr.Recognizer()
   
r.energy_threshold = 500  

   
with sample_audio as source:
    r.adjust_for_ambient_noise(source, 0.25)
    audio = r.record(source)

key = r.recognize_google(audio)
   
print("key is : ", end="")
print(key)

Solution

  • is the sample.mp3 file in the working directory of the code?