|
MP3 InfoA class to get info from your mp3 files
File Name : class_mp3.vbs Requirement : none Author : Jean-Luc Antoine Submitted : 10/02/2002 Updated : 09/11/2002 Category : Class To use a class, follow this syntax : Dim MyObjClass set MyObjClass=new MyClass MyObjClass.Property="Any value" MsgBox MyObjClass.AnotherProperty MyObjClass.MyMethod MyParam1,MyParam2 Set MyObjClass=Nothing Very simple, no need to rename any variable nor register any ActiveX. Option Explicit
Class Mp3
Private fso
Private id3v1 'Buffer of 128 Bytes
Private Header 'Buffer of 4 bytes starting the file
Private LastFile
Public Filename 'Path to the mp3 File
Private Sub Class_Initialize ' Initialize event.
LastFile=""
Set fso=WScript.CreateObject("Scripting.FileSystemObject")
End Sub
Private Sub Class_Terminate ' Terminate event.
Set fso=Nothing
End Sub
Private Function Test_Path
Test_Path=False
If fso.FileExists(FileName) Then
Dim extension
extension=UCase(fso.GetExtensionName(FileName))
If extension="MP2" Or extension="MP3" Then Test_Path=True
End If
End Function
Property Get FileSize
If Not Test_Path Then Exit Property
'If the file doesn't exist, an error is raised
FileSize=fso.GetFile(FileName).Size
End Property
Sub Read_id3v1
'Read the structure only if needed
If LastFile<>FileName Then
LastFile=FileName
Dim f,Chaine
' Read the Header
'More info : http://www.id3.org/mp3frame.html
Set f=fso.OpenTextFile(FileName,1,False,0)
'Header=Asc(f.Read(1))*256+Asc(f.Read(1))+Asc(f.Read(1))*256*256+Asc(f.Read(1))*256*256*256
Header=f.Read(4)
If FileSize>128 Then
f.skip(FileSize-128-4)
id3v1=f.Read(128)
End If
f.Close
Set f=Nothing
End If
End Sub
Function sz2str(Chaine)
Dim x
x=Instr(chaine,chr(0))
If x>0 Then Chaine=Left(Chaine,x-1)
sz2str=Chaine
End Function
' --- Header ---
'The header is followed by the audio data
Property Get Sync '4095 : 11 or 12 bits set
If Not Test_Path Then Exit Property
Read_id3v1
Sync=Asc(Left(Header,1))+(Asc(Mid(Header,2,1))\16)*256
End Property
Property Get Layer
If Not Test_Path Then Exit Property
Read_id3v1
Layer=Array("Not defined","Layer III","Layer II","Layer I")(Asc(Mid(Header,2,1))\2 Mod 4)
End Property
Property Get Prot
If Not Test_Path Then Exit Property
'If zero then header follwed by 16bits checksum
Read_id3v1
Prot=Asc(Mid(Header,2,1)) Mod 2
End Property
Property Get ID
If Not Test_Path Then Exit Property
Read_id3v1
ID=Array("MPEG-2","MPEG-1")(Asc(Mid(Header,2,1))\8 Mod 2)
End Property
Property Get BitRate 'in kbits/s
If Not Test_Path Then Exit Property
Read_id3v1
Select Case (Asc(Mid(Header,2,1))\2 Mod 4) 'Layer
Case 3 'Layer 1
BitRate=(Asc(Mid(Header,3,1))\16)*32 'Asc(Mid(Header,3,1))\16
Case 2 'Layer 2
BitRate=Array(0,32,48,56,64,80,96,112,128,160,192,224,256,320,384)(Asc(Mid(Header,3,1))\16)
Case 1 'Layer 3
If (Asc(Mid(Header,2,1))\8 Mod 2)=0 Then 'MPEG 2
BitRate=Array(0,8,16,24,32,64,80,56,64,128,160,112,128,256,320)(Asc(Mid(Header,3,1))\16)
Else 'MPEG 1
BitRate=Array(0,32,40,48,56,64,80,96,112,128,160,192,224,256,320)(Asc(Mid(Header,3,1))\16)
End If
Case Else
BitRate=0
End Select
End Property
Property Get Frequency 'in Hz
If Not Test_Path Then Exit Property
If (Asc(Mid(Header,2,1))\8 Mod 2)=0 Then 'MPEG2
Frequency=Array(22050,24000,16000)((Asc(Mid(Header,3,1)) Mod 16)\4)
Else 'MPEG1
Frequency=Array(44100,48000,32000)((Asc(Mid(Header,3,1)) Mod 16)\4)
End If
End Property
Property Get Pad 'Frame padded with an extra byte
If Not Test_Path Then Exit Property
Pad=(Asc(Mid(Header,3,1)) Mod 4)\2
End Property
Property Get Priv 'Private
If Not Test_Path Then Exit Property
Priv=Asc(Mid(Header,3,1)) Mod 2
End Property
Property Get Mode
If Not Test_Path Then Exit Property
Mode=Array("Stereo","Joint Stereo","Dual Channel","Mono")(Asc(Right(Header,1))\64)
End Property
Property Get ModeExtension
If Not Test_Path Then Exit Property
ModeExtension=(Asc(Right(Header,1))\16) Mod 4
End Property
Property Get Copyright
If Not Test_Path Then Exit Property
'If value=1 then it's illegal to copy the contents
CopyRight=(Asc(Right(Header,1))Mod 16)\8
End Property
Property Get Home
If Not Test_Path Then Exit Property
'If set to 1 then file located on its original media
Home=(Asc(Right(Header,1))Mod 8)\4
End Property
Property Get Emphasis
If Not Test_Path Then Exit Property
Emphasis=Array("none","50/15ms","","CCITT j.17")(Asc(Right(Header,1))Mod 4)
End Property
Property Get FrameSize
If Not Test_Path Then Exit Property
FrameSize=Fix(144*BitRate*1000/Frequency)+Pad
End Property
'--- id3v1 ---
' Not in all mp3 files. Check the ValidTag value
Property Get ValidTag 'Return True if it contains an id3v1 Tag
If Not Test_Path Then Exit Property
Read_id3v1
ValidTag=(Left(id3v1,3)="TAG")
End Property
Property Get Title
If Not Test_Path Then Exit Property
Read_id3v1
Title=sz2str(Mid(id3v1,4,30))
End Property
Property Get Artist
If Not Test_Path Then Exit Property
Read_id3v1
Artist=sz2str(Mid(id3v1,34,30))
End Property
Property Get Album
If Not Test_Path Then Exit Property
Read_id3v1
Album=sz2str(Mid(id3v1,64,30))
End Property
Property Get Year
If Not Test_Path Then Exit Property
Read_id3v1
Year=Mid(id3v1,94,4)
End Property
Property Get Comment
If Not Test_Path Then Exit Property
Read_id3v1
Comment=sz2str(Mid(id3v1,98,30))
End Property
Property Get Track
If Not Test_Path Then Exit Property
Read_id3v1
Track=asc(Mid(id3v1,127,1))
End Property
Property Get Genre
Dim x
If Not Test_Path Then Exit Property
Read_id3v1
x=Asc(Right(id3v1,1))
Genre=Array("Blues","Classic Rock","Country","Dance","Disco","Funk","Grunge","Hip-Hop","Jazz","Metal", _
"New Age","Oldies","Other","Pop","R&B","Rap","Reggae","Rock","Techno","Industrial","Alternative", _
"Ska","Death Metal","Pranks","Soundtrack","Euro-Techno","Ambient","Trip-Hop","Vocal","Jazz+Funk", _
"Fusion","Trance","Classical","Instrumental","Acid","House","Game","Sound Clip","Gospel","Noise", _
"AlternRock","Bass","Soul","Punk","Space","Meditative","Instrumental Pop","Instrumental Rock","Ethnic", _
"Gothic","Darkwave","Techno-Industrial","Electronic","Pop-Folk","Eurodance","Dream","Southern Rock", _
"Comedy","Cult","Gangsta","Top 40","Christian Rap","Pop/Funk","Jungle","Native American","Cabaret", _
"New Wave","Psychadelic","Rave","Showtunes","Trailer","Lo-Fi","Tribal","Acid Punk","Acid Jazz", _
"Polka","Retro","Musical","Rock & Roll","Hard Rock","Folk","Folk/Rock","National Folk","Swing", _
"Fast-Fusion","Bebob","Latin","Revival","Celtic","Blue Grass","Avantegarde","Gothic Rock", _
"Progressive Rock","Psychedelic Rock","Symphonic Rock","Slow Rock","Big Band","Chorus","Easy Listening", _
"Acoustic","Humour","Speech","Chanson","Opera","Chamber Music","Sonata","Symphony","Booty Bass", _
"Primus","Porn Groove","Satire","Slow Jam","Club","Tango","Samba","Folklore","Ballad","power Ballad", _
"Rhythmic Soul","Freestyle","Duet","Punk Rock","Drum Solo","A Capella","Euro-House","Dance Hall", _
"Goa","Drum & Bass","Club-House","Hardcore","Terror","indie","Brit Pop","Negerpunk","Polsk Punk", _
"Beat","Christian Gangsta Rap","Heavy Metal","Black Metal","Crossover","Comteporary Christian", _
"Christian Rock","Merengue","Salsa","Trash Metal","Anime","JPop","Synth Pop")(x)
End Property
Property Get GenreNum
If Not Test_Path Then Exit Property
Read_id3v1
GenreNum=Asc(Right(id3v1,1))
End Property
Property Let machin(truc)
End Property
End Class
Dim x
set x=new mp3
x.FileName="x:\yourfile.mp3"
wscript.echo "Sync=" & x.Sync & vbCrLf & "Layer=" & x.Layer & vbCrLf & "ID=" & x.id & vbCrLf _
& "Prot=" & x.prot & vbCrLf & "BitRate=" & x.BitRate & vbCrLf & "frequency:" & x.Frequency & vbCrLf _
& "Pad=" & x.pad & vbCrLf & "Priv=" & x.Priv & vbCrLf & "Mode=" & x.Mode & vbCrLf & "ModeExtension=" & x.ModeExtension & vbCrLF _
& "copyRight=" & x.Copyright & vbCrLf & "Home=" & x.Home & vbCrLf & "Emphasis=" & x.Emphasis & vbCrLf & "FrameSize=" & x.FrameSize
If x.ValidTag Then
WScript.Echo "Title=" & x.Title & vbCrLF & "artist:" & x.artist & vbCrLf & "album:" & x.album & vbCrLf _
& "Year:" & x.year & vbCrLf & "Comment : " & x.comment & vbCrLf & "Track # : " & x.track & vbCrLf & "Genre:" & x.genre & vbCrLf & "Genre Number:" & x.GenreNum
Else
WScript.Echo "No info (id3v1) in this file"
End If
Set x=Nothing
|
|||||
|
|