Hi Guys,
I have written a Managed C++ wrapper for the libMad decoder, and I would like to know.... How can I get EITHER, the _decoded_ length in bytes, or the duration in seconds of the whole file, without decoding the whole file?
I need to just evaluate a set of mp3 files, and obtain their lengths (preferably in bytes, then I can calculate the length in seconds by the bytes-per-second)
Thanks!
- Adam
On Jul 28, 2005, at 3:48 PM, Langley, Adam wrote:
I have written a Managed C++ wrapper for the libMad decoder, and I would like to know…. How can I get EITHER, the _decoded_ length in bytes, or the duration in seconds of the whole file, without decoding the whole file?
I need to just evaluate a set of mp3 files, and obtain their lengths (preferably in bytes, then I can calculate the length in seconds by the bytes-per-second)
There is no official decoded length in bytes, as the number of output bits per sample is unspecified. Rather, you can determine the length in samples -- although it is often simpler to determine the length directly in units of time.
There are four possible methods for determining the length of an entire file:
1. Read the length from a 'TLEN' frame of an ID3v2 tag. If present, this is supposed to contain the length of the audio file in milliseconds (as a numeric string).
2. Read a Xing tag from the ancillary data of the first decoded frame. Multiply the playing time of one frame (given by frame-
header.duration) by the number of frames in the file as given by
the tag.
3. 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.
4. 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.
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. :)