Jump to content

Getting Mp3 Info With Autoit Script


Cape-City
 Share

Recommended Posts

Hi,

I found a VB script, which shows the MP3 information from

a MP3 file of my choice. I tried to write the same under AutoIt,

but the program didn't do what it shall do.

AutoIt Script :

Dim $GetMP3InformationID; ID
Dim $GetMP3InformationLayer; Layer
Dim $GetMP3InformationBitrate; Bitrate
Dim $GetMP3InformationSampleRate; Hz
Dim $GetMP3InformationFrames; Frame-Anzahl
Dim $GetMP3InformationDuration; Länge (in Sekunden)

Global $bitrate_lookup[8][16]

Dim $dIN
Dim $bitrate_data, $mp3bits_string
Dim $actual_bitrate
Dim $X, $Y
Dim $mp3_id, $mp3_layer, $mp3_prot
Dim $mp3_bitrate, $mp3_freq, $mp3_pad
Dim $framesize, $total_frames, $track_length

$MP3File = FileOpenDialog("Wähle die MP3 Datei aus :", "", "MP3 Datei (*.mp3)", 1 + 2)
   
; Bitraten-Informationen
$bitrate_data = $bitrate_data & "032,032,032,032,008,008,"
$bitrate_data = $bitrate_data & "064,048,040,048,016,016,"
$bitrate_data = $bitrate_data & "096,056,048,056,024,024,"
$bitrate_data = $bitrate_data & "128,064,056,064,032,032,"
$bitrate_data = $bitrate_data & "160,080,064,080,040,040,"
$bitrate_data = $bitrate_data & "192,096,080,096,048,048,"
$bitrate_data = $bitrate_data & "224,112,096,112,056,056,"
$bitrate_data = $bitrate_data & "256,128,112,128,064,064,"
$bitrate_data = $bitrate_data & "288,160,128,144,080,080,"
$bitrate_data = $bitrate_data & "320,192,160,160,096,096,"
$bitrate_data = $bitrate_data & "352,224,192,176,112,112,"
$bitrate_data = $bitrate_data & "384,256,224,192,128,128,"
$bitrate_data = $bitrate_data & "416,320,256,224,144,144,"
$bitrate_data = $bitrate_data & "448,384,320,256,160,160,"
    
For $Y = 1 To 14
    For $X = 7 To 5 Step -1
        $bitrate_lookup[$X][$Y] = StringLeft($bitrate_data, 3)
        $bitrate_data = StringRight($bitrate_data, StringLen($bitrate_data) - 4)
    Next
    For $X = 3 To 1 Step -1
        $bitrate_lookup[$X][$Y] = StringLeft($bitrate_data, 3)
        $bitrate_data = StringRight($bitrate_data, StringLen($bitrate_data) - 4)
    Next
Next

; 4k der Datei einlesen, um einen Frameheader zu finden
$File = FileOpen ($MP3File, 1)
$dIN = FileRead ($File, 4096)
FileClose ($File)

$filesize = FileGetSize($MP3File); Wird benötigt um die Tracklänge zu berechnen
   
; Frame-Header beginnt mit 12 Bitsätzen
$i = 0
While $i = 4095
    $i = $i + 1
    $d1 = Asc(StringMid($dIN, $i, 1))
    $d2 = Asc(StringMid($dIN, $i + 1, 1))
      
    If $d1 = Dec("FF") And ($d2 And Dec("F0")) = Dec("F0") Then
;       20 Header-Bits einlesen
;       $mp3bits_string = StringMid($dIN, $i + 1, 3)
        $mp3bits_string = ShiftBits(StringMid($dIN, $i + 1, 3))
        ExitLoop
    EndIf

    $dSHIFT = ShiftBits(StringMid($dIN, $i, 3))
    $dd1 = Asc(StringLeft($dSHIFT, 1))
    $dd2 = Asc(StringRight($dSHIFT, 1))
      
    If $dd1 = Dec("FF") And ($dd2 And Dec("F0")) = Dec("F0") Then
;       20 Header-Bits einlesen
        $mp3bits_string = StringMid($dIN, $i + 2, 3)
        ExitLoop
    EndIf
WEnd
   
; Die ersten 20 Bits sind Header-Infos für diesen Frame
; Das 1. Bit ist die ID; 0 = MPG-2, 1 = MPG-1
$mp3_id = (Dec("80") And Asc(StringLeft($mp3bits_string, 1))) / 128

; Die nächsten 2 Bits sind Layer
$mp3_layer = (Dec("60") And Asc(StringLeft($mp3bits_string, 1))) / 32

; Das nächste Bit ist Schutz
$mp3_prot = Dec("10") And Asc(StringLeft($mp3bits_string, 1))

; Die nächsten 4 Bits beschreiben die Bitrate
$mp3_bitrate = Dec("F") And Asc(StringLeft($mp3bits_string, 1))

; Die nächsten 2 Bits legen die Frequenz fest
$mp3_freq = Dec("C0") And Asc(StringMid($mp3bits_string, 2, 1))

; Das nächste Bit ist Padding
$mp3_pad = (Dec("20") And Asc(StringMid($mp3bits_string, 2, 1))) / 2
$actual_bitrate = 1000 * Int($bitrate_lookup[$mp3_layer][$mp3_bitrate])
   
If $mp3_id = 0 Then
    $GetMP3InformationID = "MPEG-2"
Else
    $GetMP3InformationID = "MPEG-1"
EndIf

If $mp3_layer = 0 Then
    $GetMP3InformationLayer = "Layer III"
ElseIf $mp3_layer = 1 Then
    $GetMP3InformationLayer = "Layer II"
ElseIf $mp3_layer = 2 Then
    $GetMP3InformationLayer = "Layer I"
EndIf

$GetMP3InformationBitrate = $actual_bitrate
   
If ($mp3_id * 4) = 0 Or $mp3_freq = 0 Then
    $sample_rate = "22050"
ElseIf ($mp3_id * 4) = 1 Or $mp3_freq = 1 Then
    $sample_rate = "24000"
ElseIf ($mp3_id * 4) = 2 Or $mp3_freq = 2 Then
    $sample_rate = "16000"
ElseIf ($mp3_id * 4) = 4 Or $mp3_freq = 4 Then
    $sample_rate = "44100"
ElseIf ($mp3_id * 4) = 5 Or $mp3_freq = 5 Then
    $sample_rate = "48000"
ElseIf ($mp3_id * 4) = 6 Or $mp3_freq = 6 Then
    $sample_rate = "32000"
EndIf

$GetMP3InformationSampleRate = $sample_rate

;Trackdauer berechnen
$framesize = ((144 * $actual_bitrate) / $sample_rate) + $mp3_pad
$total_frames = $filesize / $framesize
$track_length = $total_frames / 38.5;38.5 Frames pro Sekunde
   
$GetMP3InformationFrames = Int($total_frames)
$GetMP3InformationDuration = Int($track_length)

$Message = ""
$Message = $Message & "ID = " & $GetMP3InformationID & @CRLF
$Message = $Message & "Layer = " & $GetMP3InformationLayer & @CRLF
$Message = $Message & "Bitrate = " & $GetMP3InformationBitrate & @CRLF
$Message = $Message & "SimpleRate = " & $GetMP3InformationSampleRate & @CRLF
$Message = $Message & "Frames = " & $GetMP3InformationFrames & @CRLF
$Message = $Message & "Duration = " & $GetMP3InformationDuration & @CRLF
MsgBox(0, "Daten der MP3 Datei :",$Message)

Func ShiftBits($dIN)
   Dim $sd1, $sd2, $sd3
   Dim $do1, $do2
   
   $sd1 = Asc(StringLeft($dIN, 1))
   $sd2 = Asc(StringMid($dIN, 2, 1))
   $sd3 = Asc(StringRight($dIN, 1))
   
   $do1 = (($sd1 And Dec("F")) * 16) Or (($sd2 And Dec("F0")) / 16)
   $do2 = (($sd2 And Dec("F")) * 16) Or (($sd3 And Dec("F0")) / 16)

   Return Chr($do1) + Chr($do2)
EndFunc
---

Original VB Script :

Private Type MP3Information
   ID As String 'ID
   Layer As String 'Layer
   Bitrate As Long 'Bitrate
   SampleRate As Long 'Hz
   Frames As Long 'Frame-Anzahl
   Duration As Long 'Länge (in Sekunden)
End Type

Private Function GetMP3Information(ByVal MP3File As String) As MP3Information
   Dim dIN As String
   Dim bitrate_lookup(7, 15) As Integer
   Dim bitrate_data As String
   Dim actual_bitrate As Long
   Dim X As Integer, Y As Integer
   Dim mp3_id As Double, mp3_layer As Double, mp3_prot As Double
   Dim mp3_bitrate As Double, mp3_freq As Double, mp3_pad As Double
   Dim framesize As Double, total_frames As Double, track_length As Double
   
   'Bitraten-Informationen
   bitrate_data = bitrate_data & "032,032,032,032,008,008,"
   bitrate_data = bitrate_data & "064,048,040,048,016,016,"
   bitrate_data = bitrate_data & "096,056,048,056,024,024,"
   bitrate_data = bitrate_data & "128,064,056,064,032,032,"
   bitrate_data = bitrate_data & "160,080,064,080,040,040,"
   bitrate_data = bitrate_data & "192,096,080,096,048,048,"
   bitrate_data = bitrate_data & "224,112,096,112,056,056,"
   bitrate_data = bitrate_data & "256,128,112,128,064,064,"
   bitrate_data = bitrate_data & "288,160,128,144,080,080,"
   bitrate_data = bitrate_data & "320,192,160,160,096,096,"
   bitrate_data = bitrate_data & "352,224,192,176,112,112,"
   bitrate_data = bitrate_data & "384,256,224,192,128,128,"
   bitrate_data = bitrate_data & "416,320,256,224,144,144,"
   bitrate_data = bitrate_data & "448,384,320,256,160,160,"
    
   For Y = 1 To 14
      For X = 7 To 5 Step -1
         bitrate_lookup(X, Y) = Left(bitrate_data, 3)
         bitrate_data = Right(bitrate_data, Len(bitrate_data) - 4)
      Next
      For X = 3 To 1 Step -1
         bitrate_lookup(X, Y) = Left(bitrate_data, 3)
         bitrate_data = Right(bitrate_data, Len(bitrate_data) - 4)
      Next
   Next

   Open MP3File For Binary As #1
      '4k der Datei einlesen, um einen Frameheader zu finden
      dIN = Input(4096, #1)
      filesize = LOF(1) 'Wird benötigt um die Tracklänge zu berechnen
   Close #1
   
   'Frame-Header beginnt mit 12 Bitsätzen
   Do Until i = 4095
      i = i + 1
      d1 = Asc(Mid(dIN, i, 1))
      d2 = Asc(Mid(dIN, i + 1, 1))
      
      If d1 = &HFF And (d2 And &HF0) = &HF0 Then
         '20 Header-Bits einlesen
         temp_string = Mid(dIN, i + 1, 3)
         mp3bits_string = ShiftBits(Mid(dIN, i + 1, 3))
         Exit Do
      End If

      dSHIFT = ShiftBits(Mid(dIN, i, 3))
      dd1 = Asc(Left(dSHIFT, 1))
      dd2 = Asc(Right(dSHIFT, 1))
      
      If dd1 = &HFF And (dd2 And &HF0) = &HF0 Then
         '20 Header-Bits einlesen
         mp3bits_string = Mid(dIN, i + 2, 3)
         Exit Do
      End If
   Loop
   
   'Die ersten 20 Bits sind Header-Infos für diesen Frame
   'Das 1. Bit ist die ID; 0 = MPG-2, 1 = MPG-1
   mp3_id = (&H80 And Asc(Left(mp3bits_string, 1))) / 128
   'Die nächsten 2 Bits sind Layer
   mp3_layer = (&H60 And Asc(Left(mp3bits_string, 1))) / 32
   'Das nächste Bit ist Schutz
   mp3_prot = &H10 And Asc(Left(mp3bits_string, 1))
   'Die nächsten 4 Bits beschreiben die Bitrate
   mp3_bitrate = &HF And Asc(Left(mp3bits_string, 1))
   'Die nächsten 2 Bits legen die Frequenz fest
   mp3_freq = &HC0 And Asc(Mid(mp3bits_string, 2, 1))
   'Das nächste Bit ist Padding
   mp3_pad = (&H20 And Asc(Mid(mp3bits_string, 2, 1))) / 2
   actual_bitrate = 1000 * CLng((bitrate_lookup((mp3_id * 4) Or mp3_layer, mp3_bitrate)))
   
   If mp3_id = 0 Then
      GetMP3Information.ID = "MPEG-2"
   Else
      GetMP3Information.ID = "MPEG-1"
   End If
   
   Select Case mp3_layer
      Case 1
         GetMP3Information.Layer = "Layer III"
      Case 2
         GetMP3Information.Layer = "Layer II"
      Case 3
         GetMP3Information.Layer = "Layer I"
   End Select
   GetMP3Information.Bitrate = actual_bitrate
   
   Select Case (mp3_id * 4) Or mp3_freq
      Case 0
         sample_rate = 22050
      Case 1
         sample_rate = 24000
      Case 2
         sample_rate = 16000
      Case 4
         sample_rate = 44100
      Case 5
         sample_rate = 48000
      Case 6
         sample_rate = 32000
   End Select
   GetMP3Information.SampleRate = sample_rate
    
   'Trackdauer berechnen
   framesize = ((144 * actual_bitrate) / sample_rate) + mp3_pad
   total_frames = filesize / framesize
   track_length = total_frames / 38.5 '38.5 Frames pro Sekunde
   
   GetMP3Information.Frames = Int(total_frames)
   GetMP3Information.Duration = Int(track_length)
End Function

Private Function ShiftBits(dIN As String) As String
   Dim sd1 As Integer, sd2 As Integer, sd3 As Integer
   Dim do1 As Integer, do2 As Integer
   
   sd1 = Asc(Left(dIN, 1))
   sd2 = Asc(Mid(dIN, 2, 1))
   sd3 = Asc(Right(dIN, 1))
   
   do1 = ((sd1 And &HF) * 16) Or ((sd2 And &HF0) / 16)
   do2 = ((sd2 And &HF) * 16) Or ((sd3 And &HF0) / 16)
   
   ShiftBits = Chr(do1) + Chr(do2)
End Function
---

Someone an Idea ...

Greetz

Cape-City

Link to comment
Share on other sites

Sure, this is because Autoit can't handle binary data (strings are null-terminated). Till binary support will be added, you should read binary file byte-by-byte into HEX-string or array, where each element represent one byte. You can look for my image info scripts for example.

Link to comment
Share on other sites

Hi,

thnx for your fast answer. I had taken a look on your scripts, but thats

a bit too heavy for me - I'm hacking some Code only for 3 Weeks now,

therefor I hadn't coded anything.

Transfering from vb to AutoIt was O.k., but doing a workaround, 'coz AutoIt

can't handle Binary Data, no way for me.

I think I have to wait, till AutoIt can handle it ...

Greetz

Cape-City

Link to comment
Share on other sites

Hi,

I tried to edit my script a bit, in order to support Binarydata. Take a look on it :

Dim $GetMP3InformationID; ID
Dim $GetMP3InformationLayer; Layer
Dim $GetMP3InformationBitrate; Bitrate
Dim $GetMP3InformationSampleRate; Hz
Dim $GetMP3InformationFrames; Frame-Anzahl
Dim $GetMP3InformationDuration; Länge (in Sekunden)

Global $bitrate_lookup[8][16]

Dim $dIN
Dim $bitrate_data, $mp3bits_string
Dim $actual_bitrate
Dim $X, $Y
Dim $mp3_id, $mp3_layer, $mp3_prot
Dim $mp3_bitrate, $mp3_freq, $mp3_pad
Dim $framesize, $total_frames, $track_length

$MP3File = FileOpenDialog("Wähle die MP3 Datei aus :", "", "MP3 Datei (*.mp3)", 1 + 2)
   
; Bitraten-Informationen
$bitrate_data = $bitrate_data & "032,032,032,032,008,008,"
$bitrate_data = $bitrate_data & "064,048,040,048,016,016,"
$bitrate_data = $bitrate_data & "096,056,048,056,024,024,"
$bitrate_data = $bitrate_data & "128,064,056,064,032,032,"
$bitrate_data = $bitrate_data & "160,080,064,080,040,040,"
$bitrate_data = $bitrate_data & "192,096,080,096,048,048,"
$bitrate_data = $bitrate_data & "224,112,096,112,056,056,"
$bitrate_data = $bitrate_data & "256,128,112,128,064,064,"
$bitrate_data = $bitrate_data & "288,160,128,144,080,080,"
$bitrate_data = $bitrate_data & "320,192,160,160,096,096,"
$bitrate_data = $bitrate_data & "352,224,192,176,112,112,"
$bitrate_data = $bitrate_data & "384,256,224,192,128,128,"
$bitrate_data = $bitrate_data & "416,320,256,224,144,144,"
$bitrate_data = $bitrate_data & "448,384,320,256,160,160,"
    
For $Y = 1 To 14
    For $X = 7 To 5 Step -1
        $bitrate_lookup[$X][$Y] = StringLeft($bitrate_data, 3)
        $bitrate_data = StringRight($bitrate_data, StringLen($bitrate_data) - 4)
    Next
    For $X = 3 To 1 Step -1
        $bitrate_lookup[$X][$Y] = StringLeft($bitrate_data, 3)
        $bitrate_data = StringRight($bitrate_data, StringLen($bitrate_data) - 4)
    Next
Next

; 4k der Datei einlesen, um einen Frameheader zu finden
$i = 0
$File = FileOpen ($MP3File, 0)
Do
    $i = $i + 1
    $dIN = $dIN & Hex(Asc(FileRead ($File, 1)),2)
;   MsgBox(0, "dIN :", $dIN, 1)
Until $i = 4096
FileClose ($File)

MsgBox(0, "dIN :", $dIN, 5)

$filesize = FileGetSize($MP3File); Wird benötigt um die Tracklänge zu berechnen
   
; Frame-Header beginnt mit 12 Bitsätzen
$i = 0
Do
    $i = $i + 1
    $d1 = Hex(Asc(StringMid($dIN, $i, 1)),2)
    $d2 = Hex(Asc(StringMid($dIN, $i + 1, 1)),2)
;   MsgBox(0, "d1 :", $d1, 1)
;   MsgBox(0, "d2 :", $d2, 1)     
    If $d1 = Hex(255, 2) And ($d2 And Hex(240, 2)) = Hex(240, 2) Then
;       20 Header-Bits einlesen
;       $mp3bits_string = StringMid($dIN, $i + 1, 3)
        $mp3bits_string = ShiftBits(StringMid($dIN, $i + 1, 3))
        ExitLoop
    EndIf

    $dSHIFT = ShiftBits(StringMid($dIN, $i, 3))
    $dd1 = Hex(Asc(StringLeft($dSHIFT, 1)),2)
    $dd2 = Hex(Asc(StringRight($dSHIFT, 1)),2)
;   MsgBox(0, "dd1 :", $dd1, 1)
;   MsgBox(0, "dd2 :", $dd2, 1)
         
    If $dd1 = Hex(255, 2) And ($dd2 And Hex(240, 2)) = Hex(240, 2) Then
;       20 Header-Bits einlesen
        $mp3bits_string = StringMid($dIN, $i + 2, 3)
        ExitLoop
    EndIf
Until $i = 4096
   
; Die ersten 20 Bits sind Header-Infos für diesen Frame
; Das 1. Bit ist die ID; 0 = MPG-2, 1 = MPG-1
$mp3_id = (Hex(128, 2) And Asc(StringLeft($mp3bits_string, 1))) / 128

; Die nächsten 2 Bits sind Layer
$mp3_layer = (Hex(96, 2) And Asc(StringLeft($mp3bits_string, 1))) / 32

; Das nächste Bit ist Schutz
$mp3_prot = Hex(16, 2) And Asc(StringLeft($mp3bits_string, 1))

; Die nächsten 4 Bits beschreiben die Bitrate
$mp3_bitrate = Hex(15, 2) And Asc(StringLeft($mp3bits_string, 1))

; Die nächsten 2 Bits legen die Frequenz fest
$mp3_freq = Hex(192, 2) And Asc(StringMid($mp3bits_string, 2, 1))

; Das nächste Bit ist Padding
$mp3_pad = (Hex(32, 2) And Asc(StringMid($mp3bits_string, 2, 1))) / 2
$actual_bitrate = 1000 * Int($bitrate_lookup[$mp3_layer][$mp3_bitrate])
   
If $mp3_id = 0 Then
    $GetMP3InformationID = "MPEG-2"
Else
    $GetMP3InformationID = "MPEG-1"
EndIf

Select 
    Case $mp3_layer = 1
        $GetMP3InformationLayer = "Layer III"
    Case $mp3_layer = 2
        $GetMP3InformationLayer = "Layer II"
    Case $mp3_layer = 3
        $GetMP3InformationLayer = "Layer I"
EndSelect

$GetMP3InformationBitrate = $actual_bitrate
   
If ($mp3_id * 4) = 0 Or $mp3_freq = 0 Then
    $sample_rate = "22050"
ElseIf ($mp3_id * 4) = 1 Or $mp3_freq = 1 Then
    $sample_rate = "24000"
ElseIf ($mp3_id * 4) = 2 Or $mp3_freq = 2 Then
    $sample_rate = "16000"
ElseIf ($mp3_id * 4) = 4 Or $mp3_freq = 4 Then
    $sample_rate = "44100"
ElseIf ($mp3_id * 4) = 5 Or $mp3_freq = 5 Then
    $sample_rate = "48000"
ElseIf ($mp3_id * 4) = 6 Or $mp3_freq = 6 Then
    $sample_rate = "32000"
EndIf

$GetMP3InformationSampleRate = $sample_rate

;Trackdauer berechnen
$framesize = ((144 * $actual_bitrate) / $sample_rate) + $mp3_pad
$total_frames = $filesize / $framesize
$track_length = $total_frames / 38.5;38.5 Frames pro Sekunde
   
$GetMP3InformationFrames = Int($total_frames)
$GetMP3InformationDuration = Int($track_length)

$Message = ""
$Message = $Message & "ID = " & $GetMP3InformationID & @CRLF
$Message = $Message & "Layer = " & $GetMP3InformationLayer & @CRLF
$Message = $Message & "Bitrate = " & $GetMP3InformationBitrate & @CRLF
$Message = $Message & "SimpleRate = " & $GetMP3InformationSampleRate & @CRLF
$Message = $Message & "Frames = " & $GetMP3InformationFrames & @CRLF
$Message = $Message & "Duration = " & $GetMP3InformationDuration & @CRLF
MsgBox(0, "Daten der MP3 Datei :",$Message)

Func ShiftBits($dIN)
   Dim $sd1, $sd2, $sd3
   Dim $do1, $do2
   
   $sd1 = Hex(Asc(StringLeft($dIN, 1)),2)
   $sd2 = Hex(Asc(StringMid($dIN, 2, 1)),2)
   $sd3 = Hex(Asc(StringRight($dIN, 1)),2)
   
   $do1 = (($sd1 And Hex(15, 2)) * 16) Or (($sd2 And Hex(240, 2)) / 16)
   $do2 = (($sd2 And Hex(15, 2)) * 16) Or (($sd3 And Hex(240, 2)) / 16)

   Return Chr($do1) + Chr($do2)
EndFunc

But it further didn't do what it should do.

Greetz

Cape-City

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...