Extracting the length from MP3 files with Python
4th December 2003
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.
More recent articles
- My AI/LLM predictions for the next 1, 3 and 6 years, for Oxide and Friends - 10th January 2025
- Weeknotes: Starting 2025 a little slow - 4th January 2025
- I still don't think companies serve you ads based on spying through your microphone - 2nd January 2025