Get .wav file length or duration

后端 未结 9 731
暗喜
暗喜 2020-12-04 10:17

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, pymedi

9条回答
  •  一整个雨季
    2020-12-04 11:04

    A very simple method is to use pysoundfile, https://github.com/bastibe/PySoundFile

    Here's some example code of how to do this:

    import soundfile as sf
    f = sf.SoundFile('447c040d.wav')
    print('samples = {}'.format(len(f)))
    print('sample rate = {}'.format(f.samplerate))
    print('seconds = {}'.format(len(f) / f.samplerate))
    

    The output for that particular file is:

    samples = 232569
    sample rate = 16000
    seconds = 14.5355625
    

    This aligns with soxi:

    Input File     : '447c040d.wav'
    Channels       : 1
    Sample Rate    : 16000
    Precision      : 16-bit
    Duration       : 00:00:14.54 = 232569 samples ~ 1090.17 CDDA sectors
    File Size      : 465k
    Bit Rate       : 256k
    Sample Encoding: 16-bit Signed Integer PCM
    

提交回复
热议问题