On Thu, Jul 28, 2005 at 08:26:08PM -0700, Rob Leslie wrote:
- Assuming the file has a constant bitrate (not VBR), divide the
file size in bytes (less any ID3 tags) by (125 * kbps) to obtain the playing time in seconds.
However, there's no simple way to find out if a file is VBR. If there's a Xing tag, it is. If there's an INFO tag, it's probably CBR. Otherwise, the only real way to figure it out is to look at every header (in which case you may as well do #4). I scan to the middle of the file and look at some headers, and compare the bitrate against the bitrate of the beginning of the file; this catches almost every file I've tried, but isn't foolproof. (Most MP3s have silence at the beginning, which is usually a lower bitrate than the body of a file, which helps improve the success rate.)
Also note that if you want an accurate length, you need to be sure to get the "less any ID3 tags" part right: watch out for headers at the beginning *and* end of the file (ID3v2 can be at both), and watching out for any other tag formats (eg. "APEv2" tags, apparently supported by fb2k).
- Scan every frame header of the file and accumulate the playing
time of each to arrive at a total. This does not require a full decode of each frame, so it can be done relatively quickly.
It still ends up reading the whole file from disk, though, so if you're processing hundreds of files it'll still be very slow.
I hate MP3. :)