Page 1 of 2 12 LastLast
Results 1 to 40 of 45

Thread: MP3 bitrate and duration

  1. #1
    zchoyt
    Guest

    MP3 bitrate and duration

    I can already read an MP3's ID3 tags. I need to know how to calculate the bitrate and duration as it is not included in ID3 tags. If anyone can give examples or point me in the right direction that would be most helpful.

  2. #2
    Dreamlax
    Guest
    http://www.wotsit.org/ will have a documentation on an MP3 file for sure, hopefully one of them will give you information about the header of an MP3.

  3. #3
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I know how to get... through winamp. I don't know if that helps.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  4. #4
    Fanatic Member Vanguard-MnC's Avatar
    Join Date
    Apr 2002
    Location
    Inactive for like ever.
    Posts
    628
    hey how do you open a file with winamp? like your mp3 thing???

  5. #5
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by Vanguard-MnC
    hey how do you open a file with winamp? like your mp3 thing???
    Do you mean how I get it played with Winamp?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  6. #6
    zchoyt
    Guest
    I would rather not use winamp. I am writing a dll that will read ID3 tags, duration and bitrate (for use in an asp script). It would be a lame dll if it depended on winamp, you know?

    I know that there is a way that duration and bitrate can be calculated.

  7. #7
    Lively Member formulav8's Avatar
    Join Date
    Mar 2002
    Location
    Orlando
    Posts
    116
    You can easily calculate the bitrate. I've never tried to calcualate the duration but I'm sure its possible. But you do need the duration before you can calculate the bitrate. You would need to find it in the mp3 header. Well, here is the mp3 bitrate calculation. When you have the length of the mp3 in seconds.


    VB Code:
    1. Dim ThePath As String
    2.     Dim TheSize As Long
    3.     Dim DurationInSec as Long
    4.     Dim GetMP3Bitrate as Long
    5.  
    6.     ThePath = "c:\yourfile.mp3"
    7.     TheSize = FileLen(ThePath)
    8.     TheSize = TheSize * 8
    9.  
    10.     GetMP3Bitrate = TheSize \ DurationInSec

  8. #8
    Addicted Member The Red One's Avatar
    Join Date
    Dec 2001
    Posts
    232
    You can use the mciSendString API

  9. #9
    Lively Member formulav8's Avatar
    Join Date
    Mar 2002
    Location
    Orlando
    Posts
    116
    I and he knows that(I'm assuming he does). He was wanting to calculate it.

  10. #10
    zchoyt
    Guest
    hey thanks formulav8 for the formula, useful. I will try it out tonight.

    So I can find the duration in the MP3 header?

  11. #11
    Lively Member formulav8's Avatar
    Join Date
    Mar 2002
    Location
    Orlando
    Posts
    116
    I thought you wanted to be able to calculate it. You can get the freq., bitrate, ect. from the header file.

  12. #12
    zchoyt
    Guest
    I was under the impression that it had to be calculated. I was not aware that it could be read from the header. Do you have any resources? Thanks for your help.

  13. #13
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Info! Info! Info!

    Here you have something to start with:

    The private life of MP3 frames or it's main page
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  14. #14
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Help?

    Here's what I "saved" (from a bigger project -the one on my signature-) ...

    VB Code:
    1. Option Explicit
    2.  
    3. Private Type MP3Header
    4.     Lenght As Long
    5.     Position As Long
    6.     ID As String
    7.     Layer As String
    8.     Frequency As String
    9.     Mode As String
    10.     Emphasis As String
    11.     Padding As Boolean
    12.     FrameSize As Long
    13.     CopyRighted As Boolean
    14.     Original As Boolean
    15.     BitRate As String
    16. End Type
    17.  
    18. Public Version As Byte
    19.  
    20. Private Sub ReadFile(strFilename As String, mHeader As MP3Header)
    21.  
    22.     Static BitRate(11 To 23, 1 To 14) As String
    23.     Dim fn As Integer
    24.     Dim lngHeaderPosition As Long
    25.     Dim lngFilesize As Long
    26.     Dim BitLine As Integer
    27.     Dim i As Byte
    28.    
    29.     Dim Tag2 As String
    30.     Dim TagSize As String * 4
    31.     Dim ID3Tag As String * 3
    32.     Dim sHeader As String * 4
    33.  
    34. 'BitRate    MPEG-1,     MPEG-1,     MPEG-1,     MPEG-2,     MPEG-2,     MPEG-2,
    35. 'Value      layer I     layer II    layer III   layer I     layer II    Layer III
    36. '0 0 0 0
    37. '0 0 0 1    32          32          32          32          32          8
    38. '0 0 1 0    64          48          40          64          48          16
    39. '0 0 1 1    96          56          48          96          56          24
    40. '0 1 0 0    128         64          56          128         64          32
    41. '0 1 0 1    160         80          64          160         80          64
    42. '0 1 1 0    192         96          80          192         96          80
    43. '0 1 1 1    224         112         96          224         112         56
    44. '1 0 0 0    256         128         112         256         128         64
    45. '1 0 0 1    288         160         128         288         160         128
    46. '1 0 1 0    320         192         160         320         192         160
    47. '1 0 1 1    352         224         192         352         224         112
    48. '1 1 0 0    384         256         224         384         256         128
    49. '1 1 0 1    416         320         256         416         320         256
    50. '1 1 1 0    448         384         320         448         384         320
    51. '1 1 1 1
    52.  
    53.     If BitRate(11, 1) = "" Then
    54.         BitRate(11, 1) = "32"
    55.         BitRate(11, 2) = "64"
    56.         BitRate(11, 3) = "96"
    57.         BitRate(11, 4) = "128"
    58.         BitRate(11, 5) = "160"
    59.         BitRate(11, 6) = "192"
    60.         BitRate(11, 7) = "224"
    61.         BitRate(11, 8) = "256"
    62.         BitRate(11, 9) = "288"
    63.         BitRate(11, 10) = "320"
    64.         BitRate(11, 11) = "352"
    65.         BitRate(11, 12) = "384"
    66.         BitRate(11, 13) = "416"
    67.         BitRate(11, 14) = "448"
    68.         BitRate(12, 1) = "32"
    69.         BitRate(12, 2) = "48"
    70.         BitRate(12, 3) = "56"
    71.         BitRate(12, 4) = "64"
    72.         BitRate(12, 5) = "80"
    73.         BitRate(12, 6) = "96"
    74.         BitRate(12, 7) = "112"
    75.         BitRate(12, 8) = "128"
    76.         BitRate(12, 9) = "160"
    77.         BitRate(12, 10) = "192"
    78.         BitRate(12, 11) = "224"
    79.         BitRate(12, 12) = "256"
    80.         BitRate(12, 13) = "320"
    81.         BitRate(12, 14) = "384"
    82.         BitRate(13, 1) = "32"
    83.         BitRate(13, 2) = "40"
    84.         BitRate(13, 3) = "48"
    85.         BitRate(13, 4) = "56"
    86.         BitRate(13, 5) = "64"
    87.         BitRate(13, 6) = "80"
    88.         BitRate(13, 7) = "96"
    89.         BitRate(13, 8) = "112"
    90.         BitRate(13, 9) = "128"
    91.         BitRate(13, 10) = "160"
    92.         BitRate(13, 11) = "192"
    93.         BitRate(13, 12) = "224"
    94.         BitRate(13, 13) = "256"
    95.         BitRate(13, 14) = "320"
    96.         BitRate(21, 1) = "32"
    97.         BitRate(21, 2) = "64"
    98.         BitRate(21, 3) = "96"
    99.         BitRate(21, 4) = "128"
    100.         BitRate(21, 5) = "160"
    101.         BitRate(21, 6) = "192"
    102.         BitRate(21, 7) = "224"
    103.         BitRate(21, 8) = "256"
    104.         BitRate(21, 9) = "288"
    105.         BitRate(21, 10) = "320"
    106.         BitRate(21, 11) = "352"
    107.         BitRate(21, 12) = "384"
    108.         BitRate(21, 13) = "416"
    109.         BitRate(21, 14) = "448"
    110.         BitRate(22, 1) = "32"
    111.         BitRate(22, 2) = "48"
    112.         BitRate(22, 3) = "56"
    113.         BitRate(22, 4) = "64"
    114.         BitRate(22, 5) = "80"
    115.         BitRate(22, 6) = "96"
    116.         BitRate(22, 7) = "112"
    117.         BitRate(22, 8) = "128"
    118.         BitRate(22, 9) = "160"
    119.         BitRate(22, 10) = "192"
    120.         BitRate(22, 11) = "224"
    121.         BitRate(22, 12) = "256"
    122.         BitRate(22, 13) = "320"
    123.         BitRate(22, 14) = "384"
    124.         BitRate(23, 1) = "8"
    125.         BitRate(23, 2) = "16"
    126.         BitRate(23, 3) = "24"
    127.         BitRate(23, 4) = "32"
    128.         BitRate(23, 5) = "64"
    129.         BitRate(23, 6) = "80"
    130.         BitRate(23, 7) = "56"
    131.         BitRate(23, 8) = "64"
    132.         BitRate(23, 9) = "128"
    133.         BitRate(23, 10) = "160"
    134.         BitRate(23, 11) = "112"
    135.         BitRate(23, 12) = "128"
    136.         BitRate(23, 13) = "256"
    137.         BitRate(23, 14) = "320"
    138.     End If
    139.  
    140.     fn = FreeFile
    141.     Open strFilename For Binary As #
    142.     lngFilesize = LOF(fn)                                  
    143.  
    144. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    145. ' Check for a Header
    146. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    147.            
    148.     Get #fn, 1, ID3Tag
    149.    
    150.     If ID3Tag = "ID3" Then
    151.         lngHeaderPosition = 1
    152.         Dim R As Long
    153.         Get #fn, 4, Version
    154.         Get #fn, 7, TagSize
    155.  
    156.         R = CVL(TagSize, 128)
    157.         If R > lngFilesize Or R > 2147483647 Then
    158.             Exit Sub
    159.         End If
    160.         Tag2 = Space$(R)
    161.         Get #fn, 11, Tag2
    162.         lngHeaderPosition = R + 11
    163.     End If
    164.    
    165.     Get #fn, 11 + Len(Tag2), sHeader
    166.     Close
    167.    
    168.     With mHeader
    169.         .Lenght = FileLen(strFilename)
    170.         .Position = 10 + Len(Tag2)
    171.         'Second Byte
    172.         sHeader = Mid$(sHeader, 2)
    173.         i = (Asc(sHeader) And 8) / 8
    174.         .ID = IIf(i = 0, "MPEG-2", "MPEG-1")
    175.         BitLine = 10 * i
    176.        
    177.         i = (Asc(sHeader) And 6) / 2
    178.         .Layer = IIf(i = 0, "Not defined", IIf(i = 1, "Layer 3", IIf(i = 2, _
    179.    "Layer 2", "Layer 1")))
    180.         BitLine = BitLine + (4 - i)
    181.        
    182.         'Third Byte
    183.         sHeader = Mid$(sHeader, 2)
    184.         i = (Asc(sHeader) And 240) / 16
    185.         On Error Resume Next
    186.         .BitRate = "Not defined"
    187.         .BitRate = BitRate(BitLine, i) & "kbit"
    188.         On Error GoTo 0
    189.        
    190.         i = (Asc(sHeader) And 12) / 4
    191.         If .ID = "MPEG-1" Then
    192.             .Frequency = IIf(i = 0, "44100 Hz", IIf(i = 1, "48000 Hz", _
    193.    "32000 Hz"))
    194.         Else
    195.             .Frequency = IIf(i = 0, "22050 Hz", IIf(i = 1, "24000 Hz",  _
    196.    "16000 Hz"))
    197.         End If
    198.        
    199.         i = (Asc(sHeader) And 2) / 2
    200.         .Padding = i
    201.        
    202.         'Forth Byte
    203.         sHeader = Mid$(sHeader, 2)
    204.         i = (Asc(sHeader) And 192) / 64
    205.         .Mode = IIf(i = 0, "Stereo", IIf(i = 1, "Joint Stereo", IIf(i = 2, _
    206.    "Dual channel", "Mono")))
    207.  
    208.         i = (Asc(sHeader) And 8) / 8
    209.         .CopyRighted = i
    210.        
    211.         i = (Asc(sHeader) And 4) / 4
    212.         .Original = i
    213.        
    214.         i = (Asc(sHeader) And 3)
    215.         .Emphasis = IIf(i = 0, "None", IIf(i = 1, "50/15ms", IIf(i = 2, _
    216.    "", "CCITT j.17")))
    217.        
    218.         .FrameSize = (144 * Val(.BitRate) * 1000 / Val(.Frequency)) - .Padding
    219.     End With
    220. End Sub
    221.  
    222. Public Function CVL(ByVal Cadena As String, Optional Valor As Integer = 256) _
    223.   As Long
    224.     Dim i As Integer
    225.     CVL = 0
    226.    
    227.     For i = 3 To 0 Step -1
    228.         CVL = CVL + (Valor ^ i) * Asc(Cadena)
    229.         Cadena = Mid$(Cadena, 2)
    230.     Next i
    231.    
    232. End Function
    233.  
    234. Private Sub Form_Load()
    235.     Dim mHeader As MP3Header
    236.     ReadFile "D:\Downloaded Files\MP3\4 Non Blondes - What's Up.mp3", mHeader
    237.  
    238.     With mHeader
    239.         lblMPEGInfo.Caption = "Size: " & .Lenght & " bytes"
    240.         lblMPEGInfo.Caption = lblMPEGInfo.Caption & vbCr & _
    241.    "Header found at: " & .Position
    242.         lblMPEGInfo.Caption = lblMPEGInfo.Caption & vbCr & .ID & _
    243.    " " & .Layer & vbCr & .BitRate & vbCr & .Frequency & " " _
    244.    & .Mode
    245.         lblMPEGInfo.Caption = lblMPEGInfo.Caption & vbCr _
    246.    & "Copyrihted: " & IIf(.CopyRighted, "Yes", "No")
    247.         lblMPEGInfo.Caption = lblMPEGInfo.Caption & vbCr _
    248.    & "Original: " & IIf(.Original, "Yes", "No")
    249.         lblMPEGInfo.Caption = lblMPEGInfo.Caption & vbCr _
    250.    & "Emphasis: " & .Emphasis
    251.     End With
    252. End Sub


    However, it's not working 10 points.... some MP3s are not showing the right ID or layer (according to WinAmp). Is there anything I'm missing. Besides... I can get the FrameSize, how do you get the number of frames?? What's "CRCs"? And, how do you get the lenght of the song?
    Last edited by Mc Brain; May 11th, 2002 at 01:35 PM.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  15. #15
    Addicted Member Bazza81's Avatar
    Join Date
    Apr 2001
    Location
    Nottingham, UK
    Posts
    203
    Take a look at my activeX dll. It can retrieve everything that Winamp can, only thing is it does not include support for IDv2 tags yet!

    I have also added functionality to retrive the MP3's forst audio frame position, the number of frames in the mp3 and the duration. In fact, theres bloody loads of stuff in here. Before you use it though read the documentation.
    Attached Files Attached Files
    Who needs rhetorical questions anyway?


    Bazza NET - The place you want to be!

  16. #16
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Ok... I did manage to get the CRC, the duration and the number of frames. However, I still can't understand why on some MP3s the information is not correct. Any ideas?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  17. #17
    Lively Member formulav8's Avatar
    Join Date
    Mar 2002
    Location
    Orlando
    Posts
    116
    Probably because it is a vbr mp3 file.

  18. #18
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    What do you mean?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  19. #19
    Addicted Member Bazza81's Avatar
    Join Date
    Apr 2001
    Location
    Nottingham, UK
    Posts
    203
    He means its a variable bitrate MP3. Every MP3 is made up of frames, and the frames have the bitrate encoded into them. VBR MP3's have frames which have different bitrates. This had the advantage of being able to use lower bitrates for lower quality sound in the song. In most cases it can make the MP3 a fair bit smaller. The reason you cant read the info properly from a VBR MP3 is becuase you are only readin the first frame.
    Who needs rhetorical questions anyway?


    Bazza NET - The place you want to be!

  20. #20
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I don't get it. I'm only reading the header
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  21. #21
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Am I missing something? Shouln't I have to be able to read the whole information from the header regardless if it's vbr or not??
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  22. #22
    Addicted Member Bazza81's Avatar
    Join Date
    Apr 2001
    Location
    Nottingham, UK
    Posts
    203
    This may help:

    1st frame is encoded at 128kbps
    2nd frame 256kbps
    3rd frame 128kbps
    4th frame 96kbps
    .
    .
    .
    15890th frame 192kbps
    15891st frame 128kbps

    You read the first frame and take it that the MP3 = 128kbps. But most of the other frames are not. Therefore you calculate the mp3 length bu useing the kbps in the 1st frame, when in fact it should be the average of all the frames kbps. Understand now? Thats why Winamp gets it right becuase it reads ALL the frames then displays the length!
    Who needs rhetorical questions anyway?


    Bazza NET - The place you want to be!

  23. #23
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    The problem is not with the lenght... it's with the ID and layer, to start with... then, all the rest might be wrong.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  24. #24
    Addicted Member Bazza81's Avatar
    Join Date
    Apr 2001
    Location
    Nottingham, UK
    Posts
    203
    Exaxtly, any calculations you make using the bitrate will be wrong within a VBR MP3. If you want precise calculations your going to have to read all the frames to make sure its correct
    Who needs rhetorical questions anyway?


    Bazza NET - The place you want to be!

  25. #25
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I did understand that already. But, let's get graphical...



    Both dialog boxes are showing the same information. However, Winamp says it's a MPEG-1 Layer 3, and my app thinks it's a MPEG-2.5 not defined Layer. Also, the headers seems to start on a different position. What could be the difference with this MP3??
    Attached Images Attached Images  
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  26. #26
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    any ideas?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  27. #27
    Lively Member formulav8's Avatar
    Join Date
    Mar 2002
    Location
    Orlando
    Posts
    116
    A mp3 has more then one header file. It has a header for each frame. If it is a variable bitrate mp3 then certain frames will be encoded at a different bitrate. Of course I could be wrong



    Jason

  28. #28
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I believe there should exist only one HEADER for the MP3, and not multiple frame's headers. I mean, there should be one main header where you can extract all the information needed without the need to seek all the frames to access to the information. What would be the use of a MP3's header, if you have to seek all the frame's header to get the information? Of course... I could be wrong as well.

    Am I that far from truth??
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  29. #29
    Lively Member formulav8's Avatar
    Join Date
    Mar 2002
    Location
    Orlando
    Posts
    116
    The thing is that if the mp3 is a vbr mp3 then each frame will be encoded at a different bitrate. How could just the first frame header have the correct bitrate for all of the frames if it is a vbr mp3?


    Jason

  30. #30
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I understand the bitrate difference. But, if an MP3 is coded as MPEG-1, all the frames should be coded the same way. The same thing should happen with the layer. This information should be on a main header, as it is common for all the frames. Why am I getting differences (about the MPEG coding and layer) Besides... Why WinAmp says the header starts some bytes "later"... what's the difference with other MP3s?? (I mean, how can I detect this difference, for example)
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  31. #31
    Addicted Member Bazza81's Avatar
    Join Date
    Apr 2001
    Location
    Nottingham, UK
    Posts
    203
    Ok, I have the solution. While I was kaing VBAMP I stumbled accross the same problem. Basically, your looking for the header by checking the sync. If ID2 is pesent then the sync frame is going to be some way down the file e.g. 2306 bytes into the file. Now, the ID2 information will probably have a few bytes that represent the same information as a sync header. It makes it look like you've found a sync header when you havent, your just milling around in the ID2 tag. What you have to do is extract all the ID2 information and record where the end of the ID2 tag is in the file, then start your sync header checking after the ID2 tag. Hope this helps!

    From the looks of your pic, you apparently found the first header a little bit before Winamp did, this is why I think you are looking in the ID2 tag still.
    Who needs rhetorical questions anyway?


    Bazza NET - The place you want to be!

  32. #32
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Ok... If I force the program to check at 2339 (I wonder why it didn't work in 2338) I get the right values. However, how can I detect if I'm not reading the right value. Anyway, I don't know why I would be reading over the ID3v2's Tag. I'm getting the 4 bytes just after it.
    VB Code:
    1. Get #fn, 11 + Len(Tag2), sHeader
    So, what could be between the ID3v2's tag and the header? What are those 32 bytes extra in the middle?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  33. #33
    Addicted Member Bazza81's Avatar
    Join Date
    Apr 2001
    Location
    Nottingham, UK
    Posts
    203
    REMEMBER, CRAPPY BYTES OF INFO CAN BE SHOVED ANYWHERE IN ANY FILE

    What winamp does is use roughly the same algorithm as you to check for the first header. If it finds what appears to be a header it will check another (roughly) 10000 bytes into the file to find a few more headers. If the layer and mpeg version is consistant throughout those several headers then Winamp assumes that it has found a correct set of headers and continues as normal. If there are differencs within those headers then it will miss out the first header it found and check the 2nd 3rd and 4th headers it found. If they differ from each other then it will delete the second and check the 3rd, 4th and 5th headers. It will keep doing this until it finds a matching header selection. The information you check inside the headers is up to you, but I would go all bits that will remain constant throughout the headers. I think it would be safe to assume that you can rely on the layer, the mpeg version and a couple of others like the copyright and original bits to verify the headers. It would be something like this.

    • Pass beginning tags (ID2)
    • Record end of Id2 tags
    • search for 1st header
    • found 1st header
    • check for 2nd 3rd and 4th header
    • all found
    • check for consistency
    • 1st header info does not match 2nd or 3rd or 4th
    • skip 1st header
    • get 5th header
    • 2nd 3rd 4th and 5th header info matches
    • FOUND GOOD HEADER INFO
    • continue!


    Hope this helps you, if not, throw me over your source code and I'll take a good look at it for you
    Who needs rhetorical questions anyway?


    Bazza NET - The place you want to be!

  34. #34
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    This is the code I'm using:
    VB Code:
    1. Option Explicit
    2.  
    3. Public Type MP3Header
    4.     MP3Length As Long
    5.     Position As Long
    6.     Duration As Integer
    7.     ID As String
    8.     Layer As String
    9.     SampleRate As String
    10.     Mode As String
    11.     Emphasis As String
    12.     Padding As Boolean
    13.     FrameSize As Long
    14.     FrameCount As Long
    15.     CopyRighted As Boolean
    16.     Original As Boolean
    17.     BitRate As String
    18.     CRC As Boolean
    19. End Type
    20.  
    21.  
    22. Public Version As Byte
    23.  
    24. Private Sub ReadFile(strFilename As String, mHeader As MP3Header)
    25.  
    26.     Static BitRate(11 To 23, 1 To 14) As String
    27.     Dim fn As Integer
    28.     Dim lngHeaderPosition As Long
    29.     Dim lngFilesize As Long
    30.     Dim BitLine As Integer
    31.     Dim i As Byte
    32.    
    33.     Dim Tag2 As String
    34.     Dim TagSize As String * 4
    35.     Dim ID3Tag As String * 3
    36.     Dim sHeader As String * 4
    37.  
    38.     If BitRate(11, 1) = "" Then
    39.         BitRate(11, 1) = "32"
    40.         BitRate(11, 2) = "64"
    41.         BitRate(11, 3) = "96"
    42.         BitRate(11, 4) = "128"
    43.         BitRate(11, 5) = "160"
    44.         BitRate(11, 6) = "192"
    45.         BitRate(11, 7) = "224"
    46.         BitRate(11, 8) = "256"
    47.         BitRate(11, 9) = "288"
    48.         BitRate(11, 10) = "320"
    49.         BitRate(11, 11) = "352"
    50.         BitRate(11, 12) = "384"
    51.         BitRate(11, 13) = "416"
    52.         BitRate(11, 14) = "448"
    53.         BitRate(12, 1) = "32"
    54.         BitRate(12, 2) = "48"
    55.         BitRate(12, 3) = "56"
    56.         BitRate(12, 4) = "64"
    57.         BitRate(12, 5) = "80"
    58.         BitRate(12, 6) = "96"
    59.         BitRate(12, 7) = "112"
    60.         BitRate(12, 8) = "128"
    61.         BitRate(12, 9) = "160"
    62.         BitRate(12, 10) = "192"
    63.         BitRate(12, 11) = "224"
    64.         BitRate(12, 12) = "256"
    65.         BitRate(12, 13) = "320"
    66.         BitRate(12, 14) = "384"
    67.         BitRate(13, 1) = "32"
    68.         BitRate(13, 2) = "40"
    69.         BitRate(13, 3) = "48"
    70.         BitRate(13, 4) = "56"
    71.         BitRate(13, 5) = "64"
    72.         BitRate(13, 6) = "80"
    73.         BitRate(13, 7) = "96"
    74.         BitRate(13, 8) = "112"
    75.         BitRate(13, 9) = "128"
    76.         BitRate(13, 10) = "160"
    77.         BitRate(13, 11) = "192"
    78.         BitRate(13, 12) = "224"
    79.         BitRate(13, 13) = "256"
    80.         BitRate(13, 14) = "320"
    81.         BitRate(21, 1) = "32"
    82.         BitRate(21, 2) = "64"
    83.         BitRate(21, 3) = "96"
    84.         BitRate(21, 4) = "128"
    85.         BitRate(21, 5) = "160"
    86.         BitRate(21, 6) = "192"
    87.         BitRate(21, 7) = "224"
    88.         BitRate(21, 8) = "256"
    89.         BitRate(21, 9) = "288"
    90.         BitRate(21, 10) = "320"
    91.         BitRate(21, 11) = "352"
    92.         BitRate(21, 12) = "384"
    93.         BitRate(21, 13) = "416"
    94.         BitRate(21, 14) = "448"
    95.         BitRate(22, 1) = "32"
    96.         BitRate(22, 2) = "48"
    97.         BitRate(22, 3) = "56"
    98.         BitRate(22, 4) = "64"
    99.         BitRate(22, 5) = "80"
    100.         BitRate(22, 6) = "96"
    101.         BitRate(22, 7) = "112"
    102.         BitRate(22, 8) = "128"
    103.         BitRate(22, 9) = "160"
    104.         BitRate(22, 10) = "192"
    105.         BitRate(22, 11) = "224"
    106.         BitRate(22, 12) = "256"
    107.         BitRate(22, 13) = "320"
    108.         BitRate(22, 14) = "384"
    109.         BitRate(23, 1) = "8"
    110.         BitRate(23, 2) = "16"
    111.         BitRate(23, 3) = "24"
    112.         BitRate(23, 4) = "32"
    113.         BitRate(23, 5) = "64"
    114.         BitRate(23, 6) = "80"
    115.         BitRate(23, 7) = "56"
    116.         BitRate(23, 8) = "64"
    117.         BitRate(23, 9) = "128"
    118.         BitRate(23, 10) = "160"
    119.         BitRate(23, 11) = "112"
    120.         BitRate(23, 12) = "128"
    121.         BitRate(23, 13) = "256"
    122.         BitRate(23, 14) = "320"
    123.     End If
    124.  
    125.     fn = FreeFile
    126.     Open strFilename For Binary As #
    127.     lngFilesize = LOF(fn)                                  
    128.          
    129.     Get #fn, 1, ID3Tag
    130.    
    131.     If ID3Tag = "ID3" Then
    132.         lngHeaderPosition = 1
    133.         Dim R As Long
    134.         Get #fn, 4, Version
    135.         Get #fn, 7, TagSize
    136.  
    137.         R = CVL(TagSize, 128)
    138.         If R > lngFilesize Or R > 2147483647 Then
    139.             Exit Sub
    140.         End If
    141.         Tag2 = Space$(R)
    142.         Get #fn, 11, Tag2
    143.         lngHeaderPosition = R + 11
    144.     End If
    145.    
    146.     Get #fn, 11 + Len(Tag2), sHeader
    147.        
    148.         With mHeader
    149.             .MP3Length = FileLen(strFilename)
    150.             .Position = 11 + Len(Tag2)
    151.            
    152.             '*** Second Byte ***
    153.             sHeader = Mid$(sHeader, 2)
    154.            
    155.             'MPEG Audio version ID
    156.             '00 - MPEG Version 2.5
    157.             '01 - reserved
    158.             '10 - MPEG Version 2 (ISO/IEC 13818-3)
    159.             '11 - MPEG Version 1 (ISO/IEC 11172-3)
    160.             i = (Asc(sHeader) And 24) / 8
    161.             .ID = IIf(i = 0, "MPEG-2.5", IIf(i = 1, "reserved", _
    162.                  IIf(i = 2, "MPEG-2", "MPEG-1")))
    163.             BitLine = IIf(i = 3, 10, 20)
    164.            
    165.             'Layer description
    166.             '0 0 Not defined
    167.             '0 1 Layer III
    168.             '1 0 Layer II
    169.             '1 1 Layer I
    170.             i = (Asc(sHeader) And 6) / 2
    171.             .Layer = IIf(i = 0, "Not defined", IIf(i = 1, "Layer 3", _
    172.                  IIf(i = 2, "Layer 2", "Layer 1")))
    173.             BitLine = BitLine + (4 - i)
    174.            
    175.             'CRC
    176.             '0 = No
    177.             '1 = Yes
    178.             i = (Asc(sHeader) And 1)
    179.             .CRC = Not (-i)
    180.            
    181.             ' *** Third Byte ***
    182.             sHeader = Mid$(sHeader, 2)
    183.            
    184.              i = (Asc(sHeader) And 240) / 16
    185.             On Error Resume Next
    186.             .BitRate = "Not defined"
    187.             .BitRate = BitRate(BitLine, i) & "kbit"
    188.             On Error GoTo errorhandler
    189.            
    190.             'Frequency
    191.             'value MPEG-1 MPEG-2
    192.             '0 0 44100 Hz 22050 Hz
    193.             '0 1 48000 Hz 24000 Hz
    194.             '1 0 32000 Hz 16000 Hz
    195.             '1 1
    196.             i = (Asc(sHeader) And 12) / 4
    197.             If .ID = "MPEG-1" Then
    198.                 .SampleRate = IIf(i = 0, "44100 Hz", _
    199.                      IIf(i = 1, "48000 Hz", "32000 Hz"))
    200.             Else
    201.                 .SampleRate = IIf(i = 0, "22050 Hz", _
    202.                      IIf(i = 1, "24000 Hz", "16000 Hz"))
    203.             End If
    204.            
    205.             'Padding Bit
    206.             '0 - frame is not padded
    207.             '1 - frame is padded with one extra slot
    208.             i = (Asc(sHeader) And 2) / 2
    209.             .Padding = i
    210.            
    211.             ' *** Forth Byte ***
    212.             sHeader = Mid$(sHeader, 2)
    213.            
    214.             'Mode value mode
    215.             '0 0 Stereo
    216.             '0 1 Joint stereo
    217.             '1 0 Dual channel
    218.             '1 1 Mono
    219.             i = (Asc(sHeader) And 192) / 64
    220.             .Mode = IIf(i = 0, "Stereo", IIf(i = 1, "Joint Stereo", _
    221.                  IIf(i = 2, "Dual channel", "Mono")))
    222.  
    223.             'Copyright
    224.             '0 - Audio is not copyrighted
    225.             '1 - Audio is copyrighted
    226.             i = (Asc(sHeader) And 8) / 8
    227.             .CopyRighted = i
    228.            
    229.             'Original
    230.             '0 - Copy of original media
    231.             '1 - Original media
    232.             i = (Asc(sHeader) And 4) / 4
    233.             .Original = i
    234.            
    235.             'Emphasis value Emphasis method
    236.             '0 0 none
    237.             '0 1 50/15ms
    238.             '1 0
    239.             '1 1 CCITT j.17
    240.             i = (Asc(sHeader) And 3)
    241.             .Emphasis = IIf(i = 0, "None", IIf(i = 1, "50/15ms", _
    242.                  IIf(i = 2, "reserved", "CCITT j.17")))
    243.            
    244.             .FrameSize = IIf(.Layer = "Layer 1", _
    245.                12 * Val(.BitRate) * 1000 \ Val(.SampleRate) - .Padding, _
    246.                144 * Val(.BitRate) * 1000 \ Val(.SampleRate) - .Padding)
    247.                
    248.             .FrameCount = (.MP3Length - .Position - 4) \ .FrameSize
    249.             .Duration = ((.MP3Length - .Position - 4) * 8 \ Val(.BitRate)) / 1000
    250.         End With
    251.         Close
    252. End Sub
    253.  
    254. Public Function CVL(ByVal Cadena As String, Optional Valor As Integer = 256) _
    255.   As Long
    256.     Dim i As Integer
    257.     CVL = 0
    258.    
    259.     For i = 3 To 0 Step -1
    260.         CVL = CVL + (Valor ^ i) * Asc(Cadena)
    261.         Cadena = Mid$(Cadena, 2)
    262.     Next i
    263.    
    264. End Function
    265.  
    266. Private Sub Form_Load()
    267.     Dim mHeader As MP3Header
    268.     ReadFile "D:\Downloaded Files\MP3\4 Non Blondes - What's Up.mp3", mHeader
    269.  
    270.     With mHeader
    271.         lblMPEGInfo.Caption = "Size: " & .MP3Length & " bytes" & _
    272.             vbCr & "Header found at: " & .Position & " bytes" & _
    273.             vbCr & "Length: " & .Duration & " seconds" & _
    274.             vbCr & .ID & " " & .Layer & _
    275.             vbCr & .BitRate & ", " & .FrameCount & " frames" & _
    276.             vbCr & .SampleRate & " " & .Mode & _
    277.             vbCr & "CRCs: " & IIf(.CRC, "Yes", "No") & _
    278.             vbCr & "Copyrighted: " & IIf(.CopyRighted, "Yes", "No") & _
    279.             vbCr & "Original: " & IIf(.Original, "Yes", "No") & _
    280.             vbCr & "Emphasis: " & .Emphasis    
    281.     End With
    282. End Sub
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  35. #35
    Addicted Member Bazza81's Avatar
    Join Date
    Apr 2001
    Location
    Nottingham, UK
    Posts
    203
    This is the code I use and it works very well...

    VB Code:
    1. For lCount = 1 To 5000
    2.         Get #1, lCount, bHead(0)
    3.         If bHead(0) = 255 Then
    4.             Get #1, lCount + 1, bHead(1)
    5.             If bHead(1) = 250 Or bHead(1) = 251 Then
    6.                 FirstFrame = lCount
    7.                 Exit For
    8.             End If
    9.         End If
    10.     Next lCount

    I can alter that little loop to make it search from wherever I want, so mabey if you could use it to search for the say first 5 headers
    Who needs rhetorical questions anyway?


    Bazza NET - The place you want to be!

  36. #36
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Thanks a lot!!

    This is what I got.



    BTW, are you sure that the first byte of the header would always be 255 and the second 250 or 251?
    Attached Images Attached Images  
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  37. #37
    Addicted Member Bazza81's Avatar
    Join Date
    Apr 2001
    Location
    Nottingham, UK
    Posts
    203
    Positive, its definatly 255 then 251 or 250, thats the algorithm that every commercial MP3 player uses
    Who needs rhetorical questions anyway?


    Bazza NET - The place you want to be!

  38. #38
    Addicted Member Bazza81's Avatar
    Join Date
    Apr 2001
    Location
    Nottingham, UK
    Posts
    203
    BTW, if you need help with anything else on the project then gimme a bell, no problems
    Who needs rhetorical questions anyway?


    Bazza NET - The place you want to be!

  39. #39
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Great!!! I will if I need

    BTW, you're invited (if U want) to download the last version of my app (posted in my signature)
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  40. #40
    Lively Member matt3011's Avatar
    Join Date
    May 2002
    Location
    France
    Posts
    82
    Hello, Can you tell me how you're writing the ID3v2 Tag at the begining of the file without overwriting.
    I need to write tag but i don't know how :
    - not to overwrite existing data if it's not a id3v2 tag
    - Erase the previous id3v2 Tag
    Thank you for your answer.

    BTW, the 250 or 251 behind the 255 byte in the header is only for mpeg1 layer3. If you want to have the info for other mpeg format, or other layer, it will not work (I see you're searching the layer and mpeg format in your code)
    Last edited by matt3011; May 15th, 2002 at 05:59 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width