how can i get bitrate
of a MP3 File ?
-
2mp3 can be with dynamical bitrate... – TheHorse Apr 22 '11 at 19:53
-
What's your mean ? so how can you see the `bitrate` property in the properties dialog of mp3 files in the windows ? – Kermia Apr 22 '11 at 20:15
-
2Kermia - for VBR (variable bitrate) the best you can do is take the size of the mp3 data stream and divide it by its audio length in seconds, and multiply by 8 (to convert bytes to bits). In other words, it would be an average bitrate. – Barry Kelly Apr 23 '11 at 00:54
3 Answers
MP3 Bitrate is stored in the 3rd byte of frame header so an option would be to search for the first byte with a value 255 (in theory there's should be no other bytes with all bits set to 1 before that) and bitrate should be stored two bytes after that. Following code does this:
program Project1;
{$APPTYPE CONSOLE}
uses
Classes, SysUtils;
const
BIT_RATE_TABLE: array [0..15] of Integer =
(0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0);
var
B: Byte;
begin
with TFileStream.Create(ParamStr(1), fmOpenRead) do begin
try
Position := 0;
repeat
Read(B, 1);
until B = 255;
Position := Position + 1;
Read(B, 1);
Writeln(BIT_RATE_TABLE[B shr 4]);
finally
Free;
end;
end;
end.
Note that this only finds bitrate of the first frame.
You can find more detailed info from here
![](../../users/profiles/721309.webp)
- 1,493
- 1
- 12
- 20
Have a look at TAudioFile.GetMp3Info
in Read MP3 info (just ignore the german description)
![](../../users/profiles/166432.webp)
- 2,333
- 15
- 16
You'll have to create a Delphi structure to read the MP3 file format.
That format is defined here:
http://en.wikipedia.org/wiki/MP3#File_structure
This link: http://www.3delite.hu/Object%20Pascal%20Developer%20Resources/id3v2library.html
appears to contain Delphi code for reading the format as well.
More basically, every file has a format, and generally you need to create a data structure to map that format. You then use file reading code to map the data in the file on top of structure that defines the file format.
![](../../users/profiles/2044.webp)
- 15,957
- 9
- 59
- 120