?
Solved

Bitrate from a mp3

Posted on 2003-03-17
21
Medium Priority
?
353 Views
Last Modified: 2010-04-04
How can i get the biterate from a mp3?
0
Comment
Question by:jurgentje
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 6
  • 4
21 Comments
 
LVL 1

Expert Comment

by:rvicentiu
ID: 8155160
this is the format of the mpeg 1/2 layer 1/2/3.
if you need an example I tell me and I will write it

bits name              comments
--------------------------------------------------
12   sync              0xFFF
1    version           1=mpeg1.0, 0=mpeg2.0
2    lay               4-lay = layerI, II or III
1    error protection  0=yes, 1=no
4    bitrate_index     see table below
2    sampling_freq     see table below
1    padding
1    extension         see table below
2    mode              see table below
2    mode_ext          used with "joint stereo" mode
1    copyright         0=no 1=yes
1    original          0=no 1=yes
2    emphasis          see table below
--------------------------------------------------

- bitrate_index

. mpeg1.0
        1  2  3   4   5   6   7   8   9  10  11  12  13  14
layer1 32 64 96 128 160 192 224 256 288 320 352 384 416 448
layer2 32 48 56  64  80  96 112 128 160 192 224 256 320 384
layer3 32 40 48  56  64  80  96 112 128 160 192 224 256 320

. mpeg2.0
        1  2  3   4   5   6   7   8   9  10  11  12  13  14
layer1 32 48 56  64  80  96 112 128 144 160 176 192 224 256
layer2  8 16 24  32  40  48  56  64  80  96 112 128 144 160
layer3  8 16 24  32  40  48  56  64  80  96 112 128 144 160
0
 

Author Comment

by:jurgentje
ID: 8155245
Mmm, yes I need an example :s
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 8155327
Hi jvicentiu,

Could you please provide all of the tables?  The emphasis, extension, mode, and sampling frequence tables are missing from your post.

jurgentje,

Hint: Open the MP3.  Read the first 32 bytes.  Seek in 13 bytes.  Read one byte - that's your MPEG type.  Read in next two bytes, that's your layer.  Etc.  It's pretty easy.

Stu
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jurgentje
ID: 8155577
How do I see the Bytes?


if sysutils.findfirst('mp3path', faAnyFile, Rec)=0 then
begin
Rec....
...
end;
0
 
LVL 1

Expert Comment

by:rvicentiu
ID: 8155654
they are bits not bytes, anyway this should work:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes, Windows;

const MP3_FILE='D:\MP3\Mariah Carey - Through The Rain (CD Rip) (daz.mp3wma.net).mp3';

type HEADER=record
       sync: byte;
       info: array[0..3] of byte;
     end;

var MP3:TFileStream;
    HD:HEADER;
    X:array[1..3,1..14] of word = ((32,64,96,128,160,192,224,256,288,320,352,384,416,448),
                                   (32,48,56,64,80,96,112,128,160,192,224,256,320,384),
                                   (32,40,48,56,64,80,96,112,128,160,192,224,256,320));
    S:String;
    c:char;
begin
  MP3:=TFileStream.Create(MP3_FILE,fmOpenRead);
  setlength(S,3);
  MP3.Read(S[1],3);{jump over ID3v2 tag}
  if S='ID3' then MP3.Seek(1210,soFromBeginning);
  MP3.Read(HD, sizeof(hd));
  writeln(X[(HD.info[0] and 3), (HD.info[1] shr 4)]);
  read(c);
end.
0
 
LVL 1

Expert Comment

by:rvicentiu
ID: 8155665
That was only for MPEG 1 LAYER 1/2/3, for MPEG 2 define another array like X and fill it. It will write the bitrate on the console.

all the tables:

* HEADER


bits name              comments
--------------------------------------------------
12   sync              0xFFF
1    version           1=mpeg1.0, 0=mpeg2.0
2    lay               4-lay = layerI, II or III
1    error protection  0=yes, 1=no
4    bitrate_index     see table below
2    sampling_freq     see table below
1    padding
1    extension         see table below
2    mode              see table below
2    mode_ext          used with "joint stereo" mode
1    copyright         0=no 1=yes
1    original          0=no 1=yes
2    emphasis          see table below
--------------------------------------------------

- bitrate_index

. mpeg1.0

            1  2  3   4   5   6   7   8   9  10  11  12  13  14
layer1     32 64 96 128 160 192 224 256 288 320 352 384 416 448
layer2     32 48 56  64  80  96 112 128 160 192 224 256 320 384
layer3     32 40 48  56  64  80  96 112 128 160 192 224 256 320

. mpeg2.0

            1  2  3   4   5   6   7   8   9  10  11  12  13  14
layer1     32 48 56  64  80  96 112 128 144 160 176 192 224 256
layer2      8 16 24  32  40  48  56  64  80  96 112 128 144 160
layer3      8 16 24  32  40  48  56  64  80  96 112 128 144 160


- sampling_freq

. mpeg1.0

    0     1     2    

44100 48000 32000

. mpeg2.0

    0     1     2    

22050 24000 16000


- mode:

0 "stereo"
1 "joint stereo"
2 "dual channel"
3 "single channel"


- mode extension:
 
0      MPG_MD_LR_LR
1      MPG_MD_LR_I
2      MPG_MD_MS_LR
3      MPG_MD_MS_I

jsbound :

   mode_ext     0  1   2   3
layer
1               4  8  12  16
2               4  8  12  16
3               0  4   8  16


- emphasis:

0 "none"
1 "50/15 microsecs"
2 "reserved"            must not be used !
3 "CCITT J 17"




* TRAILER

at end of file - 128 bytes

offset  type  len   name
--------------------------------------------
0       char  3                   "TAG"
3       char  30    title
33      char  30    artist
63      char  30    album
93      char  4     year
97      char  30    comments
127     byte  1     genre
--------------------------------------------

- genre :

 0    "Blues"
 1    "Classic Rock"
 2    "Country"
 3    "Dance"
 4    "Disco"
 5    "Funk"
 6    "Grunge"
 7    "Hip-Hop"
 8    "Jazz"
 9    "Metal"
10    "New Age"
11    "Oldies"
12    "Other"
13    "Pop"
14    "R&B"
15    "Rap"
16    "Reggae"
17    "Rock"
18    "Techno"
19    "Industrial"
20    "Alternative"
21    "Ska"
22    "Death Metal"
23    "Pranks"
24    "Soundtrack"
25    "Euro-Techno"
26    "Ambient"
27    "Trip-Hop"
28    "Vocal"
29    "Jazz+Funk"
30    "Fusion"
31    "Trance"
32    "Classical"
33    "Instrumental"
34    "Acid"
35    "House"
36    "Game"
37    "Sound Clip"
38    "Gospel"
39    "Noise"
40    "AlternRock"
41    "Bass"
42    "Soul"
43    "Punk"
44    "Space"
45    "Meditative"
46    "Instrumental Pop"
47    "Instrumental Rock"
48    "Ethnic"
49    "Gothic"
50    "Darkwave"
51    "Techno-Industrial"
52    "Electronic"
53    "Pop-Folk"
54    "Eurodance"
55    "Dream"
56    "Southern Rock"
57    "Comedy"
58    "Cult"
59    "Gangsta"
60    "Top 40"
61    "Christian Rap"
62    "Pop/Funk"
63    "Jungle"
64    "Native American"
65    "Cabaret"
66    "New Wave"
67    "Psychadelic"
68    "Rave"
69    "Showtunes"
70    "Trailer"
71    "Lo-Fi"
72    "Tribal"
73    "Acid Punk"
74    "Acid Jazz"
75    "Polka"
76    "Retro"
77    "Musical"
78    "Rock & Roll"
79    "Hard Rock"
80    "Unknown"



- frame length :

. mpeg1.0

layer1 :
 (48000*bitrate)/sampling_freq + padding
layer2&3:
 (144000*bitrate)/sampling_freq + padding

. mpeg2.0

layer1 :
 (24000*bitrate)/sampling_freq + padding
layer2&3 :
 (72000*bitrate)/sampling_freq + padding
0
 
LVL 1

Expert Comment

by:rvicentiu
ID: 8155676
That was only for MPEG 1 LAYER 1/2/3, for MPEG 2 define another array like X and fill it. It will write the bitrate on the console.

all the tables:

* HEADER


bits name              comments
--------------------------------------------------
12   sync              0xFFF
1    version           1=mpeg1.0, 0=mpeg2.0
2    lay               4-lay = layerI, II or III
1    error protection  0=yes, 1=no
4    bitrate_index     see table below
2    sampling_freq     see table below
1    padding
1    extension         see table below
2    mode              see table below
2    mode_ext          used with "joint stereo" mode
1    copyright         0=no 1=yes
1    original          0=no 1=yes
2    emphasis          see table below
--------------------------------------------------

- bitrate_index

. mpeg1.0

            1  2  3   4   5   6   7   8   9  10  11  12  13  14
layer1     32 64 96 128 160 192 224 256 288 320 352 384 416 448
layer2     32 48 56  64  80  96 112 128 160 192 224 256 320 384
layer3     32 40 48  56  64  80  96 112 128 160 192 224 256 320

. mpeg2.0

            1  2  3   4   5   6   7   8   9  10  11  12  13  14
layer1     32 48 56  64  80  96 112 128 144 160 176 192 224 256
layer2      8 16 24  32  40  48  56  64  80  96 112 128 144 160
layer3      8 16 24  32  40  48  56  64  80  96 112 128 144 160


- sampling_freq

. mpeg1.0

    0     1     2    

44100 48000 32000

. mpeg2.0

    0     1     2    

22050 24000 16000


- mode:

0 "stereo"
1 "joint stereo"
2 "dual channel"
3 "single channel"


- mode extension:
 
0      MPG_MD_LR_LR
1      MPG_MD_LR_I
2      MPG_MD_MS_LR
3      MPG_MD_MS_I

jsbound :

   mode_ext     0  1   2   3
layer
1               4  8  12  16
2               4  8  12  16
3               0  4   8  16


- emphasis:

0 "none"
1 "50/15 microsecs"
2 "reserved"            must not be used !
3 "CCITT J 17"




* TRAILER

at end of file - 128 bytes

offset  type  len   name
--------------------------------------------
0       char  3                   "TAG"
3       char  30    title
33      char  30    artist
63      char  30    album
93      char  4     year
97      char  30    comments
127     byte  1     genre
--------------------------------------------

- genre :

 0    "Blues"
 1    "Classic Rock"
 2    "Country"
 3    "Dance"
 4    "Disco"
 5    "Funk"
 6    "Grunge"
 7    "Hip-Hop"
 8    "Jazz"
 9    "Metal"
10    "New Age"
11    "Oldies"
12    "Other"
13    "Pop"
14    "R&B"
15    "Rap"
16    "Reggae"
17    "Rock"
18    "Techno"
19    "Industrial"
20    "Alternative"
21    "Ska"
22    "Death Metal"
23    "Pranks"
24    "Soundtrack"
25    "Euro-Techno"
26    "Ambient"
27    "Trip-Hop"
28    "Vocal"
29    "Jazz+Funk"
30    "Fusion"
31    "Trance"
32    "Classical"
33    "Instrumental"
34    "Acid"
35    "House"
36    "Game"
37    "Sound Clip"
38    "Gospel"
39    "Noise"
40    "AlternRock"
41    "Bass"
42    "Soul"
43    "Punk"
44    "Space"
45    "Meditative"
46    "Instrumental Pop"
47    "Instrumental Rock"
48    "Ethnic"
49    "Gothic"
50    "Darkwave"
51    "Techno-Industrial"
52    "Electronic"
53    "Pop-Folk"
54    "Eurodance"
55    "Dream"
56    "Southern Rock"
57    "Comedy"
58    "Cult"
59    "Gangsta"
60    "Top 40"
61    "Christian Rap"
62    "Pop/Funk"
63    "Jungle"
64    "Native American"
65    "Cabaret"
66    "New Wave"
67    "Psychadelic"
68    "Rave"
69    "Showtunes"
70    "Trailer"
71    "Lo-Fi"
72    "Tribal"
73    "Acid Punk"
74    "Acid Jazz"
75    "Polka"
76    "Retro"
77    "Musical"
78    "Rock & Roll"
79    "Hard Rock"
80    "Unknown"



- frame length :

. mpeg1.0

layer1 :
 (48000*bitrate)/sampling_freq + padding
layer2&3:
 (144000*bitrate)/sampling_freq + padding

. mpeg2.0

layer1 :
 (24000*bitrate)/sampling_freq + padding
layer2&3 :
 (72000*bitrate)/sampling_freq + padding
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 8155678
Sorry :)  My brain was on vacation this morning (bits/bytes).
0
 
LVL 1

Expert Comment

by:rvicentiu
ID: 8155716
one tiny mistake:

atfer -->> if S='ID3' then MP3.Seek(1210,soFromBeginning)
add   -->> else MP3.Seek(0,soFromBeginning);

0
 

Author Comment

by:jurgentje
ID: 8155806
That Code doesn't work :(
0
 
LVL 1

Expert Comment

by:rvicentiu
ID: 8155878
it works in Delphi 6
what is the error?
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 8155899
It work fine for me on Delphi 5 and Delphi 7.
0
 
LVL 1

Expert Comment

by:rvicentiu
ID: 8155919
you should paste the code in a console application....
first press FILE -> NEW -> OTHER and select console application, then replace all the text in the project.
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 8155928
...and don't forget to chance the MP3 filename :)
0
 

Author Comment

by:jurgentje
ID: 8155947
The Code Doesn't use a form. I Try it on a form but an error appears:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

const MP3_FILE='D:\MP3\Mariah Carey - Through The Rain (CD Rip) (daz.mp3wma.net).mp3';

type HEADER=record
      sync: byte;
      info: array[0..3] of byte;
    end;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var MP3:TFileStream;
   HD:HEADER;
   X:array[1..3,1..14] of word = ((32,64,96,128,160,192,224,256,288,320,352,384,416,448),                              (32,48,56,64,80,96,112,128,160,192,224,256,320,384),
                                  (32,40,48,56,64,80,96,112,128,160,192,224,256,320));
   S:String;
   c:char;
begin
 MP3:=TFileStream.Create(MP3_FILE,fmOpenRead);
 setlength(S,3);
 MP3.Read(S[1],3);{jump over ID3v2 tag}
 if S='ID3' then MP3.Seek(1210,soFromBeginning)
 else MP3.Seek(0,soFromBeginning);
 MP3.Read(HD, sizeof(hd));
 writeln(X[(HD.info[0] and 3), (HD.info[1] shr 4)]);
 read(c);
end;

end.
0
 
LVL 1

Accepted Solution

by:
rvicentiu earned 200 total points
ID: 8155960
and replace the MP3_FILE constant with the file you want or use this function:

//......

uses
  SysUtils, Classes, Windows;

type HEADER=record
       sync: byte;
       info: array[0..3] of byte;
     end;

var MPEG1:array[1..3,1..14] of word = ((32,64,96,128,160,192,224,256,288,320,352,384,416,448),
                                   (32,48,56,64,80,96,112,128,160,192,224,256,320,384),
                                   (32,40,48,56,64,80,96,112,128,160,192,224,256,320));

//......

function MPEG1_BR(MP3_FILE:string):word;
var MP3:TFileStream;
    HD:HEADER;
    S:String;
begin
  try
    MP3:=TFileStream.Create(MP3_FILE,fmOpenRead);
    setlength(S,3);
    MP3.Read(S[1],3);
    if S='ID3' then MP3.Seek(1210,soFromBeginning) else MP3.Seek(0,soFromBeginning);
    MP3.Read(HD, sizeof(hd));
    Result:=MPEG1[(HD.info[0] and 3), (HD.info[1] shr 4)];
    MP3.Free;
  except
    Result:=0;
  end;
end;

//......
0
 
LVL 1

Expert Comment

by:rvicentiu
ID: 8155974
you can't initialize a local variable -> X has to be a global variable - put it next to:
var Form1:TForm;
etc...
0
 

Author Comment

by:jurgentje
ID: 8156001
Result = Word

How can I Change a Word in a String, to add it in a label
0
 

Author Comment

by:jurgentje
ID: 8156004
Isn't there a function to show the first 128 bits in a label as a string
0
 
LVL 1

Expert Comment

by:rvicentiu
ID: 8156070
use inttostr or val
0
 
LVL 1

Expert Comment

by:rvicentiu
ID: 8156091
use inttostr or val
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question