MP3 - VBR - Frame length in time

Lonnie Princehouse finite.automaton at gmail.com
Wed Dec 8 17:19:14 EST 2004


It might be much easier to use an external program to figure out the
length.
Here's an example that uses sox to convert just about any audio file
into a raw format, and then determines duration by dividing length of
the raw output by number of bytes per frame.

I don't know if they make sox for Windows, but there must be something
analogous.
This takes a few seconds to run on a P4/2.8, depending of course on the
input mp3.

-------------------
import os

# return length of audio file, in seconds
def audio_file_duration(filename):
if not os.path.exists(filename):
raise Exception("file not found")
# convert into 8000Hz mono 8-bit
output_stream = os.popen('sox %s -t raw -r 8000 -U -b -c 1 -p -' %
filename)
bytes_read = 0
block_size = 2048
while True:
chunk = output_stream.read(block_size)
if not chunk:
break
bytes_read += len(chunk)
return float(bytes_read) / 8000

-----------------------
I apologize for the hosed indentation.  Google Groups is mangling my
posts.




More information about the Python-list mailing list