webForumDet fria alternativet

Läsa mp3 filer

Program & apparur Operativsystem & Program - Övrigt

4 svar · 333 visningar · startad av RanXeroX

RanXeroXMedlem sedan maj 2000829 inlägg
#1

Tja ba...

Jag har en liten fråga .. kan man läsa mp3 filer.. alltså .. de e väl binära filer.. så finns det någon header som säger att de är just mp3 filer...

Så om man läser filem med en hex editor, kan man då se på något sätt att det är en mp3 fil ???

// xerox

fiddlerMedlem sedan juli 20023 617 inlägg
#2

Hej Xerox!

Det är sant att MP3-filer har en header, men det är nästan omöjligt att med en hexeditor avgöra om det är en MP3-fil eller inte.

Undantaget är filer som är kodade med LAME, där det står LAME i klartext, och filer som har XING:s VBR-header, där man kan se ordet "XING" i klartext.

Du kan även se om filen är "taggad". Taggen ligger antingen först eller sist beroende på version.

Den "riktiga" MP3-headern är binär och föregår varje "frame" (vad heter det på svenska?) och är svår att se med en hexeditor. Det är inte ens säkert att den ligger först i filen.
Headern är 32 bit lång varav de första 11 alltid är satta till 1, så vad du isåfall skulle leta efter i hexeditorn är "FF".

/Ove

sawMedlem sedan mars 20011 880 inlägg
#3

Nja jag håller nog inte med förra inlägget riktigt.
I headen så skrivs det en TAG(inte tagg).
Din ligger sparad på dom sista 127 byten.
Dom kan ganska så lätt avläsas, om du t ex bygger ett programm i VB, så är det inte alltför svårt att läsa, eller skriva om din TAG.

fiddlerMedlem sedan juli 20023 617 inlägg
#4

En "ID3-TAG" i en MP3-fil skrivs antingen först eller sist i filen.
ID3 version 1 skrivs sist och ID3 version 2 först.

Blanda inte ihop detta med MP3-headern som används för att informera decodern hur den skall avkoda filen.

sawMedlem sedan mars 20011 880 inlägg
#5

Så härläser man headen:

''''''Read MP3 Header BEGIN''''''
Public Function ReadMP3Header(sPassFileName As String)

Dim z, i
Dim BinaryString As String
Dim byteArray(4) As Byte    'array that store first four bytes
Dim bin As String           'string that store binary number converted from readed bytes
Dim BinString As String     'containing binary string
Dim DecString As Integer  'containing decimal extracted from BinString
'''''''''''''''end of declarations'''''''

Open sPassFileName For Binary Access Read As #1  'open file #1
 for read
   For z = 1 To 4                           'step through four bytes
   Get #1, z, byteArray(z)                  'store every(z)byte  in array position z
   Next z                                   'back for next byte
 Close #1                                   'close file
 bin = ""                                   'reset and build the desired binary number in this string
   For z = 1 To 4                           'convert all bytes to binary
     For i = 0 To 7 Step 1                  'Here comes the decimal=>binary conversion
         If byteArray(z) And (2 ^ i) Then   'Use the logical "AND" operator.
            bin = bin + "1"
            Else
            bin = bin + "0"
         End If
         Next i                             'End of binary conversion
Next z
BinaryString = bin
'''''''''check MP3HeaderInfo.Frequency''''
DecString = 0
BinString = Mid(bin, 19, 2)         'take 19 to 21
For i = 1 To Len(BinString)         'convert to decimal
If Mid(BinString, i, 1) = 1 Then
DecString = DecString + 2 ^ (Len(BinString) - i)
End If
Next i
Select Case DecString
Case 0
MP3HeaderInfo.Frequency = 44100
Case 1
MP3HeaderInfo.Frequency = 32000
Case 2
MP3HeaderInfo.Frequency = 48000
Case 3
End Select
'''''check MP3HeaderInfo.Layer''''
DecString = 0
BinString = Mid(bin, 10, 2)
For i = 1 To Len(BinString)
If Mid(BinString, i, 1) = 1 Then
DecString = DecString + 2 ^ (Len(BinString) - i)
End If
Next i
Select Case DecString
Case 0
MP3HeaderInfo.Layer = ""
Case 1
MP3HeaderInfo.Layer = 2
Case 2
MP3HeaderInfo.Layer = 3
Case 3
MP3HeaderInfo.Layer = 1
End Select
''''check MP3HeaderInfo.Mode''''
DecString = 0
BinString = Mid(bin, 31, 2)
For i = 1 To Len(BinString)
If Mid(BinString, i, 1) = 1 Then
DecString = DecString + 2 ^ (Len(BinString) - i)
End If
Next i
Select Case DecString
Case 0
MP3HeaderInfo.Mode = "Stereo"
Case 1
MP3HeaderInfo.Mode = "Dual Channel"
Case 2
MP3HeaderInfo.Mode = "Joint stereo"
Case 3
MP3HeaderInfo.Mode = "Mono"
End Select
''''check MP3HeaderInfo.MpegVersion
If Mid(bin, 12, 1) = 0 Then
MP3HeaderInfo.MpegVersion = 2
Else
MP3HeaderInfo.MpegVersion = 1
End If
'''''check MP3HeaderInfo.Bitrate''''
DecString = 0
BinString = Mid(bin, 21, 4)
For i = 1 To Len(BinString)
If Mid(BinString, i, 1) = 1 Then
DecString = DecString + 2 ^ (Len(BinString) - i)
End If
Next i
Select Case DecString
Case 0
MP3HeaderInfo.Bitrate = 0
Case 1
MP3HeaderInfo.Bitrate = 112
Case 2
MP3HeaderInfo.Bitrate = 56
Case 3
MP3HeaderInfo.Bitrate = 224
Case 4
MP3HeaderInfo.Bitrate = 40
Case 5
MP3HeaderInfo.Bitrate = 160
Case 6
MP3HeaderInfo.Bitrate = 80
Case 7
MP3HeaderInfo.Bitrate = 320
Case 8
MP3HeaderInfo.Bitrate = 32
Case 9
MP3HeaderInfo.Bitrate = 128
Case 10
MP3HeaderInfo.Bitrate = 64
Case 11
MP3HeaderInfo.Bitrate = 256
Case 12
MP3HeaderInfo.Bitrate = 48
Case 13
MP3HeaderInfo.Bitrate = 192
Case 14
MP3HeaderInfo.Bitrate = 96
Case 15
MP3HeaderInfo.Bitrate = 0
If MP3HeaderInfo.Layer = 1 Then
    Select Case DecString
    Case 0
MP3HeaderInfo.Bitrate = 0
    Case 1
  MP3HeaderInfo.Bitrate = 128
    Case 2
   MP3HeaderInfo.Bitrate = 64
    Case 3
MP3HeaderInfo.Bitrate = 256
    Case 4
MP3HeaderInfo.Bitrate = 48
    Case 5
MP3HeaderInfo.Bitrate = 192
    Case 6
MP3HeaderInfo.Bitrate = 96
    Case 7
    MP3HeaderInfo.Bitrate = 384
    Case 8
MP3HeaderInfo.Bitrate = 32
    Case 9
MP3HeaderInfo.Bitrate = 160
    Case 10
    MP3HeaderInfo.Bitrate = 80
    Case 11
MP3HeaderInfo.Bitrate = 320
    Case 12
MP3HeaderInfo.Bitrate = 56
    Case 13
MP3HeaderInfo.Bitrate = 224
    Case 14
  MP3HeaderInfo.Bitrate = 112
    Case 15
MP3HeaderInfo.Bitrate = 0
End Select
End If
End Select
'''''MP3HeaderInfo.Emphasis''''
DecString = 0
BinString = Mid(bin, 25, 2)
For i = 1 To Len(BinString)        'go from first
If Mid(BinString, i, 1) = 1 Then
DecString = DecString + 2 ^ (Len(BinString) - i)
End If
Next i
Select Case DecString
Case 0
MP3HeaderInfo.Emphasis = "No"
Case 1
MP3HeaderInfo.Emphasis = "-?-"
Case 2
MP3HeaderInfo.Emphasis = "50/15"
Case 3
MP3HeaderInfo.Emphasis = "CITT j. 17"
End Select

With MP3HeaderInfo
    Dim min, sec
    .Bitrate = Int(.Bitrate)
    .mFileSize = FileSizeMP3(sPassFileName) 'ThisFile
'    .FPlayTime = ((.mFileSize * 8) / (.Bitrate * 1000))
'    min = .FPlayTime \ 60         'minutes
'    sec = .FPlayTime - (min * 60) 'seconds
 '   .FPlayTime = Format(min, "#0#") & ":" & Format(sec, "0#") 'format time to 00:00
End With

End Function
''''''Read MP3 Header END''''''

Så här skriver man TAG (OBS dom sista 127 byten)

Public Function WriteMP3Tag(sPassFileName As String, TAG As
 String, SongName As String, Artist As String, Album As String,
 Year As String, Comment As String, Genre As String) As Boolean

On Error GoTo Errorcheck
 Dim wTag As String * 3     ' First 3 Chars of 128 byte Tag Info - 'TAG'
 Dim wSongname As String * 30
 Dim wArtist As String * 30
 Dim wAlbum As String * 30
 Dim wYear As String * 4
 Dim wComment As String * 30
 Dim wGenre As String * 1
 
    wTag = TAG
    wSongname = RTrim(SongName)
    wArtist = RTrim(Artist)
    wAlbum = RTrim(Album)
    wYear = Left(Year, 4)
    wComment = RTrim(Comment)
    wGenre = Chr(Genre + 1)
    
    Open sPassFileName For Binary Access Write As #1
    Seek #1, FileLen(sPassFileName) - 127
    Put #1, , wTag
    Put #1, , wSongname
    Put #1, , wArtist
    Put #1, , wAlbum
    Put #1, , wYear
    Put #1, , wComment
    Put #1, , wGenre
    WriteMP3Tag = True
        Close #1
Exit Function
Errorcheck:
    WriteMP3Tag = False
Exit Function
End Function

Så här läser man TAG:

Public Function GetMP3Tag(ByVal sPassFileName As String) As Boolean 'ThisFile
    Dim iFreefile As Integer
    Dim lFilePos As Long
    Dim sData As String * 128
    Dim sGenreMatrix As String
    Dim sGenre() As String
    
  
 sGenreMatrix = "A Capella|Acid Jazz|Acid Punk|Acid|" + _
"Acoustic|Alt. Rock|Alternative|Ambient|Anime|Avant-
garde|Ballad|" + _
"Bass|Beat|Bebob|Big Band|Black Metal|Blue Grass|Blues|Booty 
Bass|" + _
"Brit Pop|Cabaret|Celtic|Chamber Music|Chanson|Chismas
 Song|Chorus|" + _
"Christian Gangsta Rap|Christian Rap|Christian Rock|Classic
 Rock|" + _
"Classical|Club|Club-House|Comedy|Comteporary
 Christian|Country|" + _
"Crossover|Cult|Dance Hall|Dance|Darkwave|Death Metal|Disco|"
 + _
"Dream|Drum & Bass|Drum Solo|Duet|Easy Listening|Electronic|" 
+ _
"Ethnic|Eurodance|Euro-House|Euro-Techno|Fast-
Fusion|Folk/Rock|" + _
"Folk|Folklore|Freestyle|Funk|Fusion|Game|Gangsta 
Rap|Goa|Gospel|" + _
"Gothic Rock|Gothic|Grunge|Hard Rock|Hardcore|Heavy Metal|Hip-
Hop|" + _
"House|Humour|Industrial|Instrumental Rock|Instrumental|Instrumental|" + _
"Jazz|Jazz+Funk|JPop|Jungle|Latin|Lo-
Fi|Meditative|Merengue|Metal|" + _
"Musical|National Folk|Native American|Negerpunk|New Age|New Wave|" + _
"Noise|Oldies|Opera|Other|Phychedelic|Polka|Polsk Punk|Pop/Punk|Pop|" + _
"Pop-Folk|Porn Groove|Power Ballad|Pranks|Primus|Progressive Rock|" + _
"Psychedelic Rock|Punk Rock|Punk|R&B|Rap|Rave|Reggae|Retro|Revival|" + _
"Rhythmic Soul|Rock & Roll|Rock|Salsa|Samba|Satire|Show tunes|Ska|" + _
"Slow Jam|Slow Rock|Sonata|Soul|Sound Clip|Soundtrack|Southern Rock|Space|" + _
"Speech|Swing|Symphonic Rock|Symphony|Synth Pop|Tango|Techno|Techno-Industrial|" + _
"Terrorindie|Top 40|Trailer|Trance|Trash Metal|Tribal|Trip Hop|Vocal"
    sGenre = Split(sGenreMatrix, "|")
On Error Resume Next
    ' Clear the info variables
    MP3Info.sTitle = ""
    MP3Info.sArtist = ""
    MP3Info.sAlbum = ""
    MP3Info.sYear = ""
    MP3Info.sComment = ""
    ' Ensure the MP3 file exists
    If Dir(sPassFileName) = "" Then GetMP3Tag = False: GoTo CloseMe
    ' Retrieve the info data from the MP3
    GetMP3Tag = True
    iFreefile = FreeFile
    lFilePos = FileLen(sPassFileName) - 127
    Open sPassFileName For Binary As #iFreefile
    Get #iFreefile, lFilePos, sData
    Close #iFreefile
    ' Populate the info variables
    
    If Left(sData, 3) = "TAG" Then
        MP3Info.sTitle = RTrim(Mid(sData, 4, 30))
        MP3Info.sArtist = RTrim(Mid(sData, 34, 30))
        MP3Info.sAlbum = RTrim(Mid(sData, 64, 30))
        MP3Info.sYear = RTrim(Mid(sData, 94, 4))
        MP3Info.sComment = RTrim(Mid(sData, 98, 30))
        MP3Info.sGenre = RTrim(sGenre(Asc(Mid(sData, 128, 1))))
    End If
    
CloseMe:
Close #iFreefile
End Function

Jag hoppas att detta kan vara till nytta.
Sägas skall, det gäller VB 6.0
Referenser ActiveX 8.0

143 ms totalt · 3 externa anrop · v20260731065814-full.29ac60f6
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
140 ms — hämta tråd, inlägg och bilagor (db)