mp3bitrate

How to determine .mp3 bit rate without downloading it?


I have a list of .mp3 files over the web and I would like to get the highest quality file. Quality in multimedia files equals the bit rate of them.

The bit rate itself should be found in the file's headers. If not, length of the audio track could be used too. (Filesize / Track Length = Bit Rate)

These things would be easy if I would have these files locally, but I would like to fetch this information over HTTP and determine which file has the highest quality.

Can I get an audio track's length out of HTTP headers? If not, is it possible to fetch only the bits that describes the length/bit rate instead of downloading the whole file?

I'm writing the code in python, but the question is quite general so I'm not tagging it as a python question.


Solution

  • Assuming that the remote server is behaving nicely, you could issue a HEAD request to the file and check the contents of the Content-Length header field. It doesn't give you track length or bit rate but you can get the size of the file.

    EDIT: MP3s consist of multiple frames, each of which can be of a different bit rate (VBR). Track length is calculated from the bit rate of each of these frames, rather than the length itself being stored. If you want the bit rate reliably, you'd need two get the whole file and get the bit rate of each of the frames. It may be possible to grab the first few KB of the file and read the bit rate from the first frame, but this is not always at the same point in the file (e.g. due to position of ID3 tag etc.).