Based on the analysis in the previous two articles, frames are divided into tag frames and data frames. The MP3 file types are divided based on the data frame types. The file types are shown in the following table:
Equal bit rate (constant bitrate) |
CBR MP3 file |
Bit Rate (variable bitrate) |
Xing MP3 files |
Vbri MP3 files |
I. How to determine the type of an MP3 file, CBR file, vbri file, or Xing File
In the form of a function flow chart:
Ii. Calculation of file playback duration.
You can calculate the playback duration of an MP3 file based on the file type.
1. CBR file duration calculation (duration)
For the playback time of CBR MP3, the value is constant bitrate, fixed bit rate, and the bit rate of each frame is also fixed to the same size. Therefore, it is relatively easy to get it, you can obtain the playback time of the file by using the file size and bit rate. You can use the following formula to calculate the playback time of the MP3 file:
CBR duration = file size (byte) × 8 bit/byte ratio (bitrate (K bit/s) × 1000 bit/kbit)
CBR playback time = file size (bytes) × 8 bits/byte bytes (bit rate: kilobytes/s × 1000 bits/kilobytes) [Formula 1]
Where:
? File Size: strictly speaking, it should be the total size of the MP3 file, minus the size of the MP3 tag, that is, the file size = the total size of the MP3 file-the tag size of the MP3; among them, MP3 tags are usually negligible compared with the total size of MP3 files. Therefore, you can directly use the total size of MP3 files for calculation: file size = total MP3 file size.
? Bit Rate: parse the mpeg frame header of the first frame of the MP3 file to obtain the bit rate index value. Then, check the bit rate index table to obtain the bit rate.
Therefore, we can see that for CBR files, we can use the above formula to obtain the MP3 file size and then parse the mpeg frame header of the first frame to obtain the bit rate index value, obtain the bit rate value, and then calculate the playback time of the entire CBR MP3 file.
2. VBR file duration calculation (duration)
Average bit rate of 2.1
This method is the same as CBR. for VBR MP3, if it is the same as CBR MP3, there is a similar bit rate with a fixed value for each frame, then, the playback time of the entire VBR can be calculated using the formula similar to CBR above. Therefore, we have the concept of average bit rate, that is, adding the bit rate values of all frames to get a total bit rate value, except for the total number of frames, an average bit rate is obtained. In this way, the VBR is equivalent to a CBR with the average bit rate.
However, we can see that before calculating the average bit rate, we need to get the bit rate value of each frame and the total number of frames before calculating the average bit rate value. To get the bit rate value of each frame, we need to traverse the entire VBR MP3 file to find all frames and parse the frame header of each frame, get the bit rate index value, and then look up the table to get the bit rate value. In doing so, the efficiency is obviously very low. This is only to calculate the playback time of the entire VBR MP3 file, but to traverse the entire file and parse the frame header of each frame.
Total number of frames in 2.2
The total number of frames method uses the total number of frames to calculate the playback time of the VBR.
The premise of this method is also the specification of MP3 files (the following two points are very important ):
(1) MP3, that is, MPEG-1, layer III, whether it is CBR, or VBR, the number of samples per frame is fixed 1152. That is, each frame is a fixed sampling of 1152.
(2) The fixed and variable values in CBR and VBR refer to bitrate rather than sample rate. The sampling rate is fixed for the same MP3 file, regardless of CBR or VBR.
After understanding these two prerequisites, we can see that for a VBR, although the bit rate of each frame is different, the time of each frame is fixed because
Time of each frame = number of samples of the frame * sampling rate of the frame = 1152 * Sampling Rate
Where:
? Number of samples: MPEG-1, layer III, that is, MP3, whether it is CBR or VBR, is a fixed 1152 sampling rate: for a single VBR file, are unified, fixed, 44100Hz is common. Sampling Rate
The sampling rate index is obtained by parsing the first frame header, and then the sampling rate is obtained through the table.
Since we know that the time of each frame is fixed, it is easy to think that if we know how many frames a VBR MP3 has, then we can use the total number of frames × the time of each frame = the total length of time. The formula is as follows:
VBR duration
= Total_frame_number * time_per_frame
= Total_frame_number * (sample_number * time_per_sample)
= Total_frame_number * (sample_number * (1/frame_sample_rate) ---------------- because it is MP3, sample_number = 1152
So the rest is to get the total number of frames and utilization of VBR MP3. Get the first data frame and parse the frame header. Do not forget to record the total number of frames in the "Xing" or "vbri" header of the VBR file, in this way, the total number of frames and the sampling rate can be easily obtained. However, if the frame header does not record the total number of frames, You have to traverse the entire file to calculate the total number of frames.
Iv. MP3 file types and determination