-1

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

Naman
  • 64
  • 9

3 Answers3

0

i think you should use one of this examples:

newAudio = AudioSegment.from_wav(r"C:\Users\naman.sharma\Desktop\1.wav")

newAudio = AudioSegment.from_file(r"C:\Users\naman.sharma\Desktop\1.wav", "wav")

documentation

Update:

Please try this first (you can do it in python console):

import os
print(os.listdir(r"C:\Users\naman.sharma\Desktop"))

if output contains your filename '1.wav', try this:

from pathlib import Path
path_to_wav = Path('C:', 'Users', 'naman.sharma', 'Desktop', '1.wav')
newAudio = AudioSegment.from_wav(path_to_wav)
555Russich
  • 354
  • 2
  • 10
  • I have tried both of the examples but still the same error. To give you extra info I am working on a AI module that will make audio using the other .wav file so this wav file this i am opening is made by that AI model could that be the source of this error??? But that .wav file can be openid using windows groove and windows media player. – Naman Aug 08 '22 at 10:50
  • @Naman updated answer. Please, try it. To be sure for hundred percents that file existing in this directory and python can get him, you can put output from print(os.listdir(r"C:\Users\naman.sharma\Desktop\) to your answer – 555Russich Aug 08 '22 at 11:00
  • i have uploaded the image please check . and i have tried your updated code same error . :--((. i guess the file dosent want to be read – Naman Aug 08 '22 at 11:12
  • 1
    both of your answers are correct. The problem was that .wav file was made by my model. So it was not reading them. I downloaded a random file and it was read. Thanks to both of you. – Naman Aug 08 '22 at 11:31
  • @Naman i'm was eating...happy that you deal with it. why did you mark answer above is correct if problem is not solving with described there solution? If you are interested in it, would be better if you post your answer and mark it like correct. That would be cool for other people who will search same problems – 555Russich Aug 08 '22 at 11:44
  • For normal audiofile your solution worked so i marked your answer your answer correct. @555Ruaaich but will post the correct answer when i will figure how to open that file as well . Cheers – Naman Aug 08 '22 at 11:51
0

try using forward slash.
newAudio = AudioSegment.from_wav('C:/Users/naman.sharma/Desktop/1.wav','wav')
i am using python version 3.9.2

bipin_s
  • 455
  • 3
  • 15
0

@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

Naman
  • 64
  • 9