I'm preprocessing datas to apply deep learning on audio files. I have a directory of audio file (.wav) with differents length and my goal is to use 0 padding in order to have only 10 secondes files duration. I also want to move move files that are longer than 10 secondes in an other directory:
from pydub import AudioSegment
import os
import shutil
path_to_sound = r'my\path\to\sound'
path_to_export = r'my\path\to\export'
path_for_too_long = r'my\path\to\other_directory'
pad_ms= 10000
file_names=os.listdir(path_to_sound)
print(file_names) # ['30368.wav', '41348.wav', '42900.wav', '42901.wav', '42902.wav']
for name in file_names:
audio= AudioSegment.from_wav(os.path.join(path_to_sound, name))
if pad_ms > len(audio):
shutil.move(name,path_for_too_long )
else:
silence = AudioSegment.silent(duration=pad_ms-len(audio)+1)
padded = audio + silence
padded.export(os.path.join(path_to_export, name), format = 'wav')
When running this I got the following errors:
FileNotFoundError: [WinError 2] The specified file can not be found : '41348.wav' -> 'C:\\Users\\path_for_too_long\\41348.wav'
...
FileNotFoundError: [Errno 2] No such file or directory: '41348.wav'
I think the error is due to the way i'm using shutil.move()