5

I have a Tornado Python server which accepts a variable bitrate mp3 file one chunk at a time (the chunks are made up of a fixed number of frames).

All I am doing is passing that binary forward, however, I want to know the duration of the chunk. Because it is variable bitrate I cannot do a simple calculation. I was looking into pymedia but I develop on mac OS and it seems pymedia cannot install there (also, it has not been updated since 2006). I also tried pymad but could not install it (it has not been updated since 2007), it seems more file centric anyway.

Ideally, I would like to extract bitrate from each chunk in the way that mutagen does it for example. However, mutagen requires a file as an input while I wish to input an mp3 frame or a series of such frames.

def _on_read_frames(self, data):
  logging.info('read from input:\n%s', data)
  binary_audio = base64.b64decode(data)
  # need to find out how many miliseconds went by here

UPDATE: This is part of a near real-time streaming architecture so the mp3 is encoded in small chunks, so my current solution is to use constant bit rate which allows simple calculation, I realized that there is almost no benefit to using variable bit rate here.

JonathanC
  • 967
  • 11
  • 30
  • 1
    I doubt you'll find something that already works for your case. The mutagen source for MP3 handling doesn't seem to be that large maybe implement something yourself based on it? – entropy Feb 19 '13 at 14:16
  • 1
    Having realized that for streaming chunks of audio in real time, variable bitrate does not really provide any advantage. In fact, it provides a disadvantage. I understand now why no one has created tools for this, it's not really useful! I may create such a tool anyway, because even with constant bitrate it can be useful to measure bitrate on any frame of audio. – JonathanC Feb 25 '13 at 23:01

1 Answers1

1

There are 1,152 samples per a frame, so if your chunk is a fixed number of N frames, then your chunk is a fixed length of N*1152 samples. To turn that into milleseconds, you will need to find the sample rate from the frame header.

You just need a MP3 parser. Here is the source code for a full blown decoder: https://bitbucket.org/portalfire/pymp3 including frame header parsing code, which is really all you need.

Here is more documentation on the format: http://www.codeproject.com/Articles/8295/MPEG-Audio-Frame-Header

Joshua D. Boyd
  • 4,808
  • 3
  • 29
  • 44