This question is similar to this: Get .wav file length or duration
You can find the duration of a .wav or .mp3 by knowing the number of samples in the file along with the sample rate (samples per second). The equation to find duration in seconds from these is: duration = (samples/sample_rate).
I found one of the solutions from the above question particularly simple and useful (answer by Dave C). Although, first you must install the soundfile
module using pip install soundfile
, along with numpy
if you are not using Anaconda. After you do this, you can simply import the soundfile
module and use its built in functions to find the sample rate and number of samples.
import soundfile as sf
f = sf.SoundFile('your_file.wav')
print('samples = {}'.format(len(f)))
print('sample rate = {}'.format(f.samplerate))
print('seconds = {}'.format(len(f) / f.samplerate))