I have over a thousand audio files, and I want to check if their sample rate is 16kHz. To do it manually would take me forever. Is there a way to check the sample rate using python?
4 Answers
Python has a builtin module dealing with WAV files.
You can write a simple script that will iterate over all files in some directory. something along the general lines of:
import os
import wave
for file_name in os.listdir(FOLDER_PATH):
with wave.open(file_name, "rb") as wave_file:
frame_rate = wave_file.getframerate()
.... DO WHATEVER ....
-
I'm glad to hear that! Though your comment about Python 3.x strike me a bit weird since Python 2.7 has a WAV library as well. Anyway, good luck with the rest of your work :-)– ehudkApr 19, 2017 at 13:11
-
Below Python version 3.4,
wave.open()
doesn't return a context manager. For older Python versions, the call can be wrapped incontextlib.closing()
.– MatthiasApr 20, 2017 at 14:02
For .wav files the solution might be:
from scipy.io.wavfile import read as read_wav
import os
os.chdir('path') # change to the file directory
sampling_rate, data=read_wav("filename.wav") # enter your filename
print sampling_rate
I end up getting unknow file format error with the wave package from python. wave-error
Alternatively the sox wrapper in python works for me. pysox
!pip install sox
import sox
sox.file_info.sample_rate("file1.wav")
Hope it helps
!pip install pydub
- from pydub.utils import mediainfo
- info=mediainfo("abc.wav")
- print(info)
-
2I assume info contains more info than sample rate. could you add which specific method to fetch sample rate?– fuyiAug 12, 2021 at 19:08