Extracting the length from MP3 files with Python
Ned Batchelder recently wrote about the difficulties involved in extracting the length from an MP3 file. We’re going to need to solve this problem soon at work; luckily, it seems that the answer may lie in the Python bindings for mpgedit, an audio file editing library available for both Windows and Linux.
After installing the Windows package and experimenting for a while, I managed to extract the time from one of my test files using the following:
>>> import mpgedit >>> play = mpgedit.Play('example.mp3') >>> play.total_time() (213, 129) >>> secs, msecs = play.total_time() >>> mins = secs / 60 >>> secs = secs - mins * 60 >>> print "%d:%02d minutes" % (mins, secs) 3:33 minutes
However, for other files
total_time() is returning
(-1, -1). I’m sure there’s a solution to this but I haven’t stumbled across it yet.