Search code examples
pythonaudiowavfile-not-foundpydub

FileNotFoundError: [WinError 2] The system cannot find the file specified using pydub


I am working on a code to extract data of every seconds data a wav file using pydub.

But whenever i am running the code :-

from pydub import AudioSegment
t1 = 1 
t2 = 2
t1 = t1 * 1000 #Works in milliseconds
t2 = t2 * 1000
newAudio = AudioSegment.from_wav(r"C:\Users\naman.sharma\Desktop\1.wav","wav")
newAudio = newAudio[t1:t2]
newAudio.export('newSong.wav', format="wav")

it gives this error :-

FileNotFoundError: [WinError 2] The system cannot find the file specified

I know that this file exist in that location. Please help stackoverflow demigods.

and the above code is for only 1 second i will spun a for loop around it to get data till the end. If you feel the info i have given is less pls tell in commments i am new to this, will give more info.

Another thing I have also used this code but I still gives the same error:-

from pydub import AudioSegment
from pydub.utils import make_chunks

myaudio = AudioSegment.from_file(r"C:\Users\naman.sharma\Desktop\1.wav" ,format =  "wav") 
chunk_length_ms = 1000 # pydub calculates in millisec
chunks = make_chunks(myaudio, chunk_length_ms) #Make chunks of one sec

#Export all of the individual chunks as wav files

for i, chunk in enumerate(chunks):
    chunk_name = "chunk{0}.wav".format(i)
    print ("exporting", chunk_name)
    chunk.export(chunk_name, format="wav")

enter image description here


Solution

  • @all this is the answer

    #Importing the audio data
    import librosa
    import IPython.display as ipd
    import matplotlib.pyplot as plt
    import numpy as np
    from librosa.display import specshow
    audio_path = "1.wav "
    lib_data,lib_sample_rate = librosa.load(audio_path)
    plt.figure(figsize=(12,4))
    plt.plot(lib_data)
    

    Output - enter image description here