pythonwavwave

How do I get the actual duration of .wav audio in python using .wave library?


I have a .wav audio which is of 5 seconds.

But after I imported the audio and calculated the time with this code using wave library:

import wave

wav1=wave.open("mixkit-flock-of-wild-geese-20.wav","r")
raw1=wav1.readframes(-1)
raw1=np.frombuffer(raw1,"int16")

samprate1=wav1.getframerate()

T1=len(raw1)/samprate1

I am getting duration of audio T1 to around 10 seconds. How do I get correct audio duration of 5 seconds using .wave library.


Solution

  • I believe channels can throw off this calculation.

    Try:

    frame_count = wav1.getnframes()
    channel_count = wav1.getnchannels()
    frame_rate = wav1.getframerate()
    T1 = frame_count / float(frame_rate * channel_count)