Pergunta

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.

Foi útil?

Solução

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top