4

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.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
iTayb
  • 12,373
  • 24
  • 81
  • 135

1 Answers1

3

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.).

Community
  • 1
  • 1
cmbuckley
  • 40,217
  • 9
  • 77
  • 91
  • That brings me the filesize of the content. I would like to get the length of the audio track in seconds, not in bytes. – iTayb Jan 28 '12 at 15:47
  • MP3s can contain header / tag information that tell you the bit rate, but it doesn't necessarily contain the actual bit rate used in the file. It gets more complicated when you consider that they can be variable bit rates too. If the file is CBR, then maybe the [frame header](http://www.mp3-tech.org/programmer/frame_header.html) is the place to start looking. – cmbuckley Jan 28 '12 at 15:53
  • 2
    The ID3v2 tag, which is optional, may have an optional `TLE` or `TLEN` field, which contains the audio duration. Also, the first audio frame may be a metadata frame hidden in silent audio. This may be `Info` for CBR files, or `Xing` or `VBRI` for VBR files. You can calculate the duration if it's a CBR file, but not if it's a VBR file. For VBR files you have to sum the duration of all audio frames, minus the first frame if it was a metadata frame. – hippietrail Apr 29 '21 at 03:28