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?
Asked
Active
Viewed 2.8k times
14
-
what format are the audio files? – ehudk Apr 19 '17 at 09:09
-
WAV file. mono channel – ash Apr 19 '17 at 09:15
5 Answers
17
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 ....

ehudk
- 510
- 5
- 13
-
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](https://docs.python.org/2.7/library/wave.html). Anyway, good luck with the rest of your work :-) – ehudk Apr 19 '17 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 in `contextlib.closing()`. – Matthias Apr 20 '17 at 14:02
3
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

Leah2015
- 33
- 5
1
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

DSBLR
- 555
- 5
- 9
-
This I have done this on windows. Installing sox in Linux could be a nightmare, because of dependency package and compatibility with Linux version. – DSBLR Oct 31 '19 at 01:51
-
works perfectly well on linux. Just install sox lib first: '$ sudo apt install sox' – markling May 19 '20 at 18:16
0
!pip install pydub
- from pydub.utils import mediainfo
- info=mediainfo("abc.wav")
- print(info)

swati sharma
- 11
- 4
-
2I assume info contains more info than sample rate. could you add which specific method to fetch sample rate? – fuyi Aug 12 '21 at 19:08
0
I use the code given below whenever I want to find the sample rate.
import torchaudio
metadata = torchaudio.info('path/to/audio/file.extension')
print(metadata)
The output will look something like this
AudioMetaData(sample_rate=8000, num_frames=625920, num_channels=1, bits_per_sample=16, encoding=PCM_S)

Sneha T S
- 21
- 6