Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check audio's sample rate using python

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?

like image 989
ash Avatar asked Apr 19 '17 08:04

ash


3 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 ....
like image 182
ehudk Avatar answered Sep 19 '22 07:09

ehudk


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
like image 45
Leah2015 Avatar answered Sep 20 '22 07:09

Leah2015


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

like image 34
DSBLR Avatar answered Sep 23 '22 07:09

DSBLR