一个可以读取ID3信息的类[可以很方便添加MP3了]

网络整理 - 07-26
<!--METADATA TYPE="typelib"
UUID="00000205-0000-0010-8000-00AA006D2EA4"
NAME="ADODB Type Library"
-->
<%
''Author: 小龙 zyvslxl,zyvslxl@163.com 请保留
''需指定的属性
''Path 文件的路径
''类头部分定义的属性为可以得到信息,当然不止这个,理论上可以返回任何标签值
''在Id3v2() 中的Select case FrameId(i)部分加入已知的标签,可以返回相应的内容
''因为个人知识有限,会的话请指教


''可用方法
''CreateStream 打开文件流
''pre 执行
''CloseStream 关闭文件流

Class mp3

Public Path
Public Flag
Public Title ''标题
Public Artist ''作者
Public Album ''专集
Public IssueYear ''出品年代
Public Comment ''备注
Public Genre ''类型
Public Tcon ''流派
Public MpTime ''时间
Public USLT ''歌词
Private objStream


''[待扩展]
private Function Exitext()
Dim ext
ext = lcase(right(path,len(path)-inStrRev(".")))
Exitext = ext
End Function


''[转换函数,有些类型还是判断不了]
private Function bin2str(binstr,num)
on error resume next
Dim varlen,clow,ccc,skipflag ,i,biy
skipflag=0
ccc = ""
For i=1 To num
clow = MidB(binstr,i,1)
If ascb(clow)=0 then
skipflag=0
'' ccc = null
Else
If skipflag=0 Then
If AscB(clow) > 127 Then
ccc =ccc & Chr(AscW(MidB(binstr,i+1,1) & clow))
skipflag=1
Else
ccc = ccc & Chr(AscB(clow))
End If
Else
skipflag=0
End If

End If
Next
bin2str = ccc
End Function


''[载入文件]
Public Function CreateStream()
on error resume next
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = adTypeBinary
objStream.Open
Err.clear
objStream.LoadFromFile Path
If err<>0 Then
objStream.LoadFromFile server.mappath(Path)
End If
Err.clear
End Function

''[关闭文件流]
Public Function CloseStream()
objStream.close
Set objStream = nothing
End Function


''[标志信息]
Public Function id()
Dim Comm
objStream.position =0
Comm = objStream.Read(3)
id = bin2str(Comm)
End Function


''[ID3V2信息读取]
Private Function Id3v2()
Dim FrameID()
Dim FrameSize()
Dim FrameSize1()
Dim Flags()
Dim Frame()
Dim MaxSize
Dim i
i = 0

objStream.position = 6
MaxSize = TopSize(objStream.Read(4))
''response.write "<font color=red>"&MaxSize&"</font><br>"


Do While ObjStream.Position < MaxSize
ReDim Preserve FrameID(i),FrameSize(i),FrameSize1(i),Flags(i),Frame(i)
FrameID(i) = bin2str(objStream.Read(4),4)
If Isnull (FrameId(i)) or FrameId(i)="" Then Exit Do
FrameSize(i) = Size(objStream.Read(4))
If Isnull (FrameSize(i)) or FrameSize(i)= 0 Then Exit Do
objStream.Read(2) ''[Flag]
''objStream.Read(1)
Frame(i) = bin2str(objStream.Read(FrameSize(i)),FrameSize(i))
i = i+1
Loop

''[依次赋值给属性]
Dim j
j = i-1
For i = 0 to j
''response.write i
Select case Ucase(FrameId(i))
Case "TIT2" : Title = cstr(Frame(i))
Case "COMM" : Comment = cstr(Frame(i))
Case "TPE1" : Artist = cstr(Frame(i))
Case "TALB" : Album = cstr(Frame(i))
Case "TYER" : IssueYear = cstr(Frame(i))
Case "TCON" : TCON = cstr(Frame(i))
Case "TIME" : MPTIME = cstr(Frame(i))
Case "USLT" : USLT = cstr(Frame(i))
End Select
Next
End Function



''[获取标签帧长度/某个具体标签内容长度]
Private Function Size(num)
Size = Null
Dim a,b,c,d
a = midb(num,1,1)
b = midb(num,2,1)
c = midb(num,3,1)
d = midb(num,4,1)

If not isNull(a) Then
size=ascb(a) * 4294967296
End If
If not isNull(b) Then
size= size + ascb(b) * 65536
End If
If not isNull(c) Then
size=size + ascb(c) * 256
End If
If not isNull(d) Then
size=size + ascb(d)
End If
End Function

''[获取头标签长度/标签总长度]
Private Function TopSize(num)
TopSize = Null
Dim a,b,c,d
a = midb(num,1,1)
b = midb(num,2,1)
c = midb(num,3,1)
d = midb(num,4,1)

If not isNull(a) Then
TopSize=ascb(a) * 2097152
End If
If not isNull(b) Then
TopSize= TopSize + ascb(b) * 1024
End If
If not isNull(c) Then
TopSize=TopSize + ascb(c) * 128
End If
If not isNull(d) Then
TopSize=TopSize + ascb(d)
End If
End Function



''[ID3V1信息读取]
Private Function Id3v1()
objStream.position = objStream.size - 128
Flag = bin2str(objStream.Read(3),3)
If Flag = "TAG" Then
Title = bin2str(objStream.Read(30),30)
Artist = bin2str(objStream.Read(30),30)
Album = bin2str(objStream.Read(30),30)
IssueYear = bin2str(objStream.Read(4),4)
Comment = bin2str(objStream.Read(30),30)
Genre = bin2str(objStream.Read(3),3)
End If
End Function



''[判断ID3版本 只区分ID3v1和ID3v2]
private Function FileFlag()
objStream.position = 0
Dim Flag
Flag = Ucase(bin2str(objStream.Read(3),3))
If Flag ="ID3" Then FileFlag=true
''response.write Flag & "<br>"
End Function


''[优先返回ID3V2版本信息]
Public Function Pre()
If FileFlag then
Id3v2
Else
Id3v1
End IF
End Function


End Class
%>