Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: File duration calculation for a VBR mp3 file (Read 9922 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

File duration calculation for a VBR mp3 file

Hi, I want to calculate file duration for a VBR mp3 file. I can calculate the file duration for CBR by simply using extracted bitrate and size of the file. But i didn't find any method to calculate the file duration of a vbr mp3 file. I didn't find any special headers for the VBR. Can anyone help me?

File duration calculation for a VBR mp3 file

Reply #1
If you're trying to calculate the duration of a VBR file that's already been encoded, you would have to open the file in a program like MediaInfo that can tell you the average bitrate. Once you know that, you can predict the duration using that and the file size like you would with ABR and CBR files. There is no way to look at a simple header and find out what the average bitrate of a VBR file is-- the entire file must be scanned to find it.

File duration calculation for a VBR mp3 file

Reply #2
The number of mp3 frames and the audio size of the source are found in the xing and VBRI header.
Once you have that combined with the type of mp3 you're working with a number of calculations can be made.


File duration calculation for a VBR mp3 file

Reply #3
There is no way to look at a simple header and find out what the average bitrate of a VBR file is-- the entire file must be scanned to find it.

Sorry?  So you mean that these last 10 years of XING header and fraunhofer VBRi have not existed?



Pity.. i don't have it anymore.. I did one such implementation around year 2000 in mIRC scripting.

This may help:
http://www.codeproject.com/KB/audio-video/...aspx#XINGHeader

File duration calculation for a VBR mp3 file

Reply #4
I stand corrected then. Does that mean his files didn't have the header? The TC sounded like he looked already and didn't find one.

File duration calculation for a VBR mp3 file

Reply #5
The Xing header is not a separated header, but a frame that is formatted in a special way so that in contains this info. So if he looked for something like an ID3 tag, he wouldn't find it.

Of course, i am hypothetizing, since he hasn't replied.

File duration calculation for a VBR mp3 file

Reply #6
Thank you for your replies and sorry for delay in replying.
I want to write a generalized code to find the file duration of any mp3 file. I cannot use any 3rd party tool to find the average bitrate.
So i am planning to do like below
1) Check for XING header by parsing the 1st frame of mp3 file. If it is available then find the file duration
2) otherwise, then parse entire file (max of 1000 frames for big mp3 files), calculate average bitrate, then find the file duration.

I have to do above 2 steps even for CBR because we cannot tell whether the stream is belongs to CBR or VBR by just parsing the 1st frame.

Please correct me if i am wrong.

File duration calculation for a VBR mp3 file

Reply #7
I can calculate the file duration for CBR by simply using extracted bitrate and size of the file.


Just to be clear, that kind of calculation, based on the first frame's bitrate and the size of the file (not counting tags, I hope) will still be just an estimate. It will be very close, but not exact.

To get the exact duration, you need to look at the header of every frame. If I understand correctly, the type of frame (MPEG-1 Layer 3 or MPEG-2 Layer 3) determines how many samples are in it (1152 or 576, respectively). The sample frequency is also in the header of each frame. The # of samples divided by the sample frequency = the actual duration of that frame. The frame type and the sample frequency will probably be the same throughout the file, so you could just look at the first one and assume the rest are the same; this will make the calculation easier.

File duration calculation for a VBR mp3 file

Reply #8
A couple of years ago I attempted something very similar. I'm not sure I remember all the fine details now, but...

I had previously found the website provided by JAZ in post #6, and using this info I was able to calculate the times quickly and accurately.

Unfortunately, most of the mp3 files I had lacked a XING header, so I then used a very similar approach to that described by Database4 (steps 1 and 2, post #7), step 2 (in my case at least) being as described by mjb2006. This was slow and accurate. Too slow to be worth the wait in this instance.

The thing is, when the same files are loaded into mp3tag, it calculates the times quickly and accurately. An order of magnitude faster, about as quickly as if there had been XING headers IIRC. How does it do that?

I programmed this using dev-c, I believe my method was very efficient, but I found no way to compete with mp3tag's speed.

Anyone have any further insight?
Cheers,
Alan

File duration calculation for a VBR mp3 file

Reply #9
Well, I suppose if one sampled the bitrate at a few hundred places in the file, then calculated an average bitrate for the file, the results wouldn't be exact, but they would probably be pretty close.

File duration calculation for a VBR mp3 file

Reply #10
I programmed this using dev-c, I believe my method was very efficient, but I found no way to compete with mp3tag's speed.

Anyone have any further insight?


Did you use buffered reading? I.e. it is much faster to read 200KB's at once than to read 200 1KB blocks.


Also, it can only be accurate if the whole file is read. Depending on the music type, the end can easily need a higher bitrate than the beginning, or have other non-linearities.


@pdq: The problem with that approach is that you don't know were you have to read. If doing that, the first thing you need is seek the start of the frame (because you don't know where it is).
Precisely, one of the things that the Xing header contains is 100 seek points into the file, one for each 1% of file duration.

File duration calculation for a VBR mp3 file

Reply #11

@pdq: The problem with that approach is that you don't know were you have to read. If doing that, the first thing you need is seek the start of the frame (because you don't know where it is).
Precisely, one of the things that the Xing header contains is 100 seek points into the file, one for each 1% of file duration.

I thought that mp3 was suitable for streaming because you can easily resynchronize starting at any random location?

 

File duration calculation for a VBR mp3 file

Reply #12
Did you use buffered reading? I.e. it is much faster to read 200KB's at once than to read 200 1KB blocks.

Also, it can only be accurate if the whole file is read. Depending on the music type, the end can easily need a higher bitrate than the beginning, or have other non-linearities.

Yes I did, also tried reading the entire file into an array.

pdq's approach may be worth a try. Nobody's claiming it to be perfect, but as a compromise it might work.
Cheers,
Alan