0

I need it to know how long (in time) is a .wav file in one of my programs.

I know I can get in through the properties of the but I have no idea of how to access them using python. I've tried to find commands with os and pathlib but I was unable to find one that would give me the time of the sound file or all of the properties.

TNTy100
  • 70
  • 8

1 Answers1

1

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))
Tyler B
  • 62
  • 10