Finding the length of an mp3 file Finding the length of an mp3 file python python

Finding the length of an mp3 file


You can use mutagen to get the length of the song (see the tutorial):

from mutagen.mp3 import MP3audio = MP3("example.mp3")print(audio.info.length)


You can use FFMPEG libraries:

    args=("ffprobe","-show_entries", "format=duration","-i",filename)    popen = subprocess.Popen(args, stdout = subprocess.PIPE)    popen.wait()    output = popen.stdout.read()

and the output will be:

[FORMAT]duration=228.200515[/FORMAT]


You can also get this using eyed3, if that's your flavor by doing:

import eyed3duration = eyed3.load('path_to_your_file.mp3').info.time_secs

Note however that this uses sampling to determine the length of the track. As a result, if it uses variable bit rate, the samples may not be representative of the whole, and the estimate may be off by a good degree (I've seen these estimates be off by more than 30% on court recordings).

I'm not sure that's much worse than other options, but it's something to remember if you have variable bit rates.