QUOTE(kirkm @ Sep 5 2003, 02:28 AM)
The VBR files are giving a lot of trouble - I'm only reading 5 headers (thinking this should be enough) but sometimes
a CBR match is confirmed even when the file is VBR.
Is there a better way to determine if an mp3 is VBR, or not ?
If you want to know for sure if an MP3 file is VBR or CBR you have to look at ALL frames! When you find that all frames have the same size (bitrate indicator in the frame header) then the file can be called CBR, otherwise it is a VBR file.
A nice but inofficial and purely optional feature for VBR files is the so-called "Xing header". It contains some additional info about the file, including the total number of frames and a table of seek points. If you find such a Xing header at the beginning of an MP3 file you can be almost sure that it is VBR and use the info from it for calculation of the duration, for instance.
Note: although it is called "Xing header" (because it was introduced by the Xing encoder) it is widely supported - LAME also generates it for VBR files.
QUOTE
2nd question - is there any way to calculate the duration of an mp3 file
without playing it? Even approximately ?
You have to find out the number of MPEG frames in the file. The most reliable way to do this is to count them all, seeking from one frame header to the next by using the frame sizes (bitrate indicators) until the end of file. If you have a CBR file then you can divide the file size by the frame size to calculate the number of frames.
Each MPEG audio frame will be decoded to a fixed number of samples (1152 for MPEG-1 or 576 for MPEG-2 files).
So the equation is:
duration[seconds] = number_of_frames * samples_per_frame / sampling_frequency[Hz]
Have fun!