Skip to content
Advertisement

Get .wav file length or duration

I’m looking for a way to find out the duration of a audio file (.wav) in python. So far i had a look at python wave library, mutagen, pymedia, pymad i was not able to get the duration of the wav file. Pymad gave me the duration but its not consistent.

Advertisement

Answer

The duration is equal to the number of frames divided by the framerate (frames per second):

JavaScript

Regarding @edwards’ comment, here is some code to produce a 2-channel wave file:

JavaScript

If you play the resultant file in an audio player, you’ll find that is 40 seconds in duration. If you run the code above it also computes the duration to be 40 seconds. So I believe the number of frames is not influenced by the number of channels and the formula above is correct.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement