asp读写wma的类

抽时间把写功能完成了,本地测试通过,但是仅试了几个文件,没有充分测试,还是有可能会损坏文件,所以在使用前最好能做好备份测试一下

wmaExif.class.asp
  1. <%
  2. 'Author: dragonbbc
  3. 'Email: dragonbbc@163.com
  4. 'Blog: http://blog.csdn.net/dragonbbc
  5. '参考资料:
  6. 'http://linle.ycool.com/post.1255557.html
  7. 'http://www.itlearner.com/Article/2007/3944.shtml
  8. '需指定的属性
  9. 'File
  10. '可用方法
  11. 'CheckSize  检查文件大小
  12. 'Parse  读取信息
  13. 'GetInfo(TagName)
  14. 'SetInfo(TagName)
  15. 'SavaChange
  16. 'SaveTo(FileName)
  17. '常见TagName
  18. '标准Tag: Title, Artist, Copyright, Description
  19. '扩展Tag: WM/AlbumTitle, WM/EncodedBy, WM/Genre, WM/Lyrics, WM/Track, WM/Year, WM/URL, WM/UserWebURL, WMFSDKVersion, WM/TrackNumber
  20. Class wmaExif
  21.     Public File '文件
  22.     Public debugMode    'Debug模式
  23.     
  24.     Private objStream
  25.     Private headtag, bodytag, std_tag, ext_tag
  26.     Private headsize,headext1,tagnum,headext2,headbuf,bodysize,bodyext,bodybuf
  27.     Private framebuf(),sectionbuf(),std_buf,ext_buf
  28.     Private items1, items2
  29.     Private isStdChanged, isExtChanged, extAddnum
  30.     Private std_no, ext_no
  31.     Private addExtbuf
  32.     Private Sub Class_Initialize()
  33.     headtag = "3026b2758e66cf11a6d900aa0062ce6c"
  34.     bodytag = "3626b2758e66cf11a6d900aa0062ce6c"
  35.     std_tag = "3326b2758e66cf11a6d900aa0062ce6c"
  36.     ext_tag = "40a4d0d207e3d21197f000a0c95ea850"
  37.     items1 = Array("Title""Artist""Copyright""Description""Reserved")
  38.     'items2 = Array("AlbumTitle", "EncodedBy", "Genre", "Lyrics", "Track", _
  39.     '                            "Year", "URL", "UserWebURL", "TrackNumber")
  40.     isStdChanged = False
  41.     isExtChanged = False
  42.     extAddnum = 0
  43.     End Sub
  44.   Private Sub Class_Terminate
  45.     on error resume next
  46.     CloseStream
  47.   End Sub
  48.     
  49. 'CheckSize
  50.     Public Function CheckSize()
  51.         CheckSize = False
  52.         CreateStream
  53.         If ReadHeader = False Then Err("文件头读取失败")
  54.         If ReadBody = False Then Err("文件头读取失败")
  55.         DebugMsg("Head:"&CStr(headsize))
  56.         DebugMsg("Body:"&Cstr(bodysize))
  57.         DebugMsg("Total:"&objStream.size)
  58.         If objStream.Size <> headsize + bodysize Then Err("文件大小有误")
  59.         CheckSize = True
  60.         CloseStream
  61.     End Function
  62.     
  63. 'GetInfo
  64.     Public Function GetInfo(TagName)
  65.         TagName = Replace(TagName, "/""")
  66.         If Eval("Not IsEmpty("&TagName&")"Then
  67.             Execute("GetInfo = "&TagName)
  68.         End If
  69.     End Function
  70. 'SetInfo
  71.     Public Function SetInfo(TagName, TagValue)
  72.         SetInfo = False
  73.         Select Case TagName
  74.             Case "Title"
  75.                 If Eval(TagName&" <> TagValue"Then 
  76.                     Execute(TagName&" = TagValue")
  77.                     isStdChanged = True
  78.                 End If
  79.             Case "Artist"
  80.                 If Eval(TagName&" <> TagValue"Then
  81.                     Execute(TagName&" = TagValue")
  82.                     isStdChanged = True
  83.                 End If
  84.             Case "CopyRight"
  85.                 If Eval(TagName&" <> TagValue"Then
  86.                     Execute(TagName&" = TagValue")
  87.                     isStdChanged = True
  88.                 End If
  89.             Case "Description"
  90.                 If Eval(TagName&" <> TagValue"Then
  91.                     Execute(TagName&" = TagValue")
  92.                     isStdChanged = True
  93.                 End If
  94.             Case "Reserved"
  95.                 If Eval(TagName&" <> TagValue"Then
  96.                     Execute(TagName&" = TagValue")
  97.                     isStdChanged = True
  98.                 End If
  99.             Case Else
  100.                 tTagName = TagName
  101.                 TagName = Replace(TagName, "/""")
  102.                 If Eval("IsEmpty("&TagName&")"Then
  103.                     Execute(TagName&" = TagValue")
  104.                     extAddnum = extAddnum + 1
  105.                     nbuf = WriteString(tTagName)
  106.                     nlen = LenB(nbuf)
  107.                     flag = 0
  108.                     vbuf = WriteString(TagValue)
  109.                     vlen = LenB(vbuf)
  110.                     addExtbuf = addExtbuf&WriteShort_UL(nlen)&nbuf&WriteShort_UL(flag)&WriteShort_UL(vlen)&vbuf
  111.                 ElseIf Eval(TagName&" <> TagValue"Then
  112.                     Execute(TagName&" = TagValue")
  113.                     isExtChanged = True
  114.                 End If
  115.         End Select
  116.     End Function
  117. 'SaveChange
  118.     Public Function SaveChange()
  119.         SaveTo(File)
  120.     End Function
  121.     
  122. 'SaveToFile
  123.     Public Function SaveTo(FileName)
  124.         'Rebuild Standard frame
  125.         If isStdChanged Then
  126.             For i = 0 To 4
  127.                 Execute("tmpstr = "&items1(i))
  128.                 tmpbuf = WriteString(tmpstr)
  129.                 tmpStdbuf1 = tmpStdbuf1&WriteShort_UL(LenB(tmpbuf))
  130.                 tmpStdbuf2 = tmpStdbuf2&tmpbuf
  131.             Next
  132.             If IsEmpty(std_no) Then
  133.                 DebugMsg("standard frame added")
  134.                 std_buf = WriteHex(std_tag)&WriteLong_UL(24+10+LenB(tmpStdbuf2))&Chr(0)&Chr(0)&tmpStdbuf1&tmpStdbuf2
  135.             Else
  136.                 DebugMsg("standard frame changed")
  137.                 framebuf(std_no) = LeftB(framebuf(std_no), 16)&WriteLong_UL(24+10+LenB(tmpStdbuf2))&MidB(framebuf(std_no), 21, 4)&tmpStdbuf1&tmpStdbuf2
  138.             End If
  139.         End If
  140.         'Rebuild Extended frame
  141.         If isExtChanged Then
  142.             DebugMsg("extended frame changed")
  143.             For i = 0 To Ubound(sectionbuf)
  144.                 If IsEmpty(sectionbuf(i)) Then Exit For
  145.                 nlen = ReadShort_UL(MidB(sectionbuf(i), 1, 2))
  146.                 nbuf = ReadString(MidB(sectionbuf(i), 3, nlen))
  147.                 flag = ReadShort_UL(MidB(sectionbuf(i), 3+nlen, 2))
  148.                 Execute("vbuf = "&Replace(nbuf,"/",""))
  149.                 If flag = 0 Then
  150.                     vbuf = WriteString(vbuf)
  151.                 Else
  152.                     vbuf = WriteShort_UL(vbuf)&Chr(0)
  153.                 End If
  154.                 vlen = LenB(vbuf)
  155.                 tmpExtbuf = tmpExtbuf&MidB(sectionbuf(i), 1, 2+nlen+2)&WriteShort_UL(vlen)&vbuf
  156.             Next
  157.             framebuf(ext_no) = LeftB(framebuf(ext_no), 16)&WriteLong_UL(24+2+LenB(tmpExtbuf))&MidB(framebuf(ext_no), 21, 6)&tmpExtbuf
  158.         End If
  159.         If extAddnum > 0 Then
  160.             If IsEmpty(ext_no) Then
  161.                 ext_buf = WriteHex(ext_tag)&WriteLong_UL(24+2+LenB(addExtbuf))&Chr(0)&Chr(0)&WriteShort_UL(extAddnum)&addExtbuf
  162.                 DebugMsg("extended frame generated")
  163.             Else
  164.                 framebuf(ext_no) = LeftB(framebuf(ext_no), 16)&WriteLong_UL(LenB(framebuf(ext_no))+LenB(addExtbuf))&MidB(framebuf(ext_no), 21, 4)&WriteShort_UL(Ubound(sectionbuf)+extAddnum)&MidB(framebuf(ext_no), 27)&addExtbuf
  165.             End If
  166.             DebugMsg("extended frame added "&extAddnum&" content")
  167.         End If
  168.         For i = 1 To Ubound(framebuf)
  169.             DebugMsg("merging framebuf"&i&",size:"&LenB(framebuf(i)))
  170.             tmpheadbuf = tmpheadbuf&framebuf(i)
  171.         Next
  172.         If IsEmpty(std_no) Then
  173.             DebugMsg("standard frame added,size:"&LenB(std_buf))
  174.             tmpheadbuf = tmpheadbuf&std_buf
  175.         End If
  176.         If IsEmpty(ext_no) Then
  177.             DebugMsg("extended frame added,size:"&LenB(std_buf))
  178.             tmpheadbuf = tmpheadbuf&ext_buf
  179.         End If
  180.         totalframes = Ubound(framebuf)
  181.         If Not IsEmpty(std_buf) Then totalframes = totalframes + 1
  182.         If Not IsEmpty(ext_buf) Then totalframes = totalframes + 1
  183.         tmpwmabuf = WriteHex(headtag)&WriteLong_UL(LenB(tmpheadbuf)+30) _
  184.                             & Cstr(headext1)&WriteLong_UL(totalframes)&Cstr(headext2)&tmpheadbuf _
  185.                             & WriteHex(bodytag)&WriteLong_UL(bodysize)&Cstr(bodyext)&CStr(bodybuf)
  186.         DebugMsg("headext1(to write):"&ReadHex(Cstr(headext1)))
  187.         DebugMsg("headext2(to write):"&ReadHex(Cstr(headext2)))
  188.         DebugMsg("headsize(to write):"&LenB(tmpheadbuf)+30)
  189.         DebugMsg("bodysize(to write):"&bodysize)
  190.         DebugMsg("filesize(to write):"&LenB(tmpwmabuf))
  191.         CreateStream
  192.         With objStream
  193.             .position = 0
  194.             .Type = 2
  195.             .WriteText tmpwmabuf
  196.             .SetEOS
  197.             .position = 0
  198.             .Type = 1
  199.             .Read(2)
  200.             tmpbuf = .Read
  201.             .position = 0
  202.             .Write tmpbuf
  203.             .SetEOS
  204.             DebugMsg("end position(to write):"&.position)
  205.             DebugMsg("file size(to write):"&.Size)
  206.             .SaveToFile FileName,2
  207.         End With
  208.         CloseStream
  209.     End Function
  210. 'Parse
  211.     Public Function Parse()
  212.         CreateStream
  213.         If ReadHeader = False Then Exit Function
  214.         DebugMsg("find "&tagnum&" frames,total size:"&LenB(headbuf))
  215.         ReDim framebuf(tagnum)
  216.         head_off = 1
  217.         For j = 1 To tagnum
  218.             If Not IsEmpty(std_no) And Not IsEmpty(ext_no) Then
  219.                 framebuf(j) = MidB(headbuf,head_off)
  220.                 DebugMsg("escape other frames,start from position:"&CStr(head_off)&",total size:"&LenB(framebuf(j)))
  221.                 Exit For
  222.             End If
  223.             frame_head = MidB(headbuf, head_off, 24)
  224.             frametag = ReadHex(LeftB(frame_head, 16))
  225.             framesize = ReadLong_UL(MidB(frame_head,17,4))
  226.             framebuf(j) = MidB(headbuf,head_off,framesize)
  227.             DebugMsg("frame"&j&":"&ReadHex(frame_head)&" size:"&framesize)
  228.             head_off = head_off + 24
  229.             If frametag = std_tag Then
  230.                 DebugMsg("standard frame no:"&j&"  size:"&framesize)
  231.                 std_no = j
  232.                 Dim lenx(5)
  233.                 tmp_off = 0
  234.                 For i = 0 To 4
  235.                     lenx(i) = ReadShort_UL(MidB(headbuf, head_off+2*i, 2))
  236.                     tempstr = ReadString(MidB(headbuf, head_off+10+tmp_off, lenx(i)))
  237.                     tmp_off = tmp_off + lenx(i)
  238.                     Execute(items1(i)&" = tempstr")
  239.                 Next
  240.                 head_off = head_off + 10 + tmp_off
  241.                 DebugMsg("current offeset:"&Cstr(head_off))
  242.             ElseIf frametag = ext_tag Then
  243.                 DebugMsg("extended frame no:"&j&"  size:"&framesize)
  244.                 ext_no = j
  245.                 inum = ReadShort_UL(MidB(headbuf, head_off, 2))
  246.                 Redim sectionbuf(inum)
  247.                 tmp_off = 2
  248.                 k = 0
  249.                 While k < inum And tmp_off+24 < framesize
  250.                     nlen = ReadShort_UL(MidB(headbuf, head_off+tmp_off, 2))
  251.                     nbuf = ReadString(MidB(headbuf, head_off+tmp_off+2, nlen))
  252.                     flag = ReadShort_UL(MidB(headbuf, head_off+tmp_off+2+nlen, 2))
  253.                     vlen = ReadShort_UL(MidB(headbuf, head_off+tmp_off+2+nlen+2, 2))
  254.                     vbuf = MidB(headbuf, head_off+tmp_off+2+nlen+4, vlen)
  255.                     If flag = 0 Then
  256.                         vbuf = ReadString(vbuf)
  257.                     Else
  258.                         vbuf = ReadShort_UL(vbuf)
  259.                     End If
  260.             'on error resume next
  261.                     Execute(Replace(nbuf,"/","")&" = vbuf")
  262.                     'err.clear
  263.                     sectionbuf(k) = MidB(headbuf, head_off+tmp_off, 6+nlen+vlen)
  264.                     tmp_off = tmp_off + 6 + nlen + vlen
  265.                     k = k + 1
  266.                 Wend
  267.                 head_off = head_off + framesize-24
  268.                 DebugMsg("current offeset:"&Cstr(head_off))
  269.             Else
  270.                 head_off = head_off + framesize-24
  271.                 DebugMsg("current offeset:"&Cstr(head_off))
  272.             End If
  273.         Next
  274.         
  275.         CloseStream
  276.     End Function
  277. 'get the header
  278.     Private Function ReadHeader()
  279.         ReadHeader = False
  280.         buf = objStream.Read(30)
  281.         If ReadHex(LeftB(buf, 16)) <> headtag Then Exit Function
  282.         headsize = ReadLong_UL(MidB(buf,17,4))
  283.         headext1 = MidB(buf,21,4)
  284.         tagnum = ReadLong_UL(MidB(buf,25,4))
  285.         headext2 = MidB(buf,29,2)
  286.         DebugMsg("headext1:"&ReadHex(headext1))
  287.         DebugMsg("headext2:"&ReadHex(headext2))
  288.         headbuf = objStream.Read(headsize-30)
  289.         ReadHeader = True
  290.     End Function
  291. 'get the body
  292.     Private Function ReadBody()
  293.         ReadBody = False
  294.         buf = objStream.Read(24)
  295.         If ReadHex(LeftB(buf, 16)) <> bodytag Then Exit Function
  296.         bodysize = ReadLong_UL(MidB(buf,17,4))
  297.         bodyext = MidB(buf,21,4)
  298.         DebugMsg("bodyext:"&ReadHex(bodyext))
  299.         bodybuf = objStream.Read(bodysize-24)
  300.         ReadBody = True
  301.     End Function
  302. 'CreateStream
  303.     Public Function CreateStream()
  304.         on error resume next
  305.         Set objStream = Server.CreateObject("ADODB.Stream")
  306.         objStream.Type = 1
  307.         objStream.Open
  308.         Err.clear
  309.         objStream.LoadFromFile File
  310.         If err<>0 Then
  311.             File = server.mappath(File)
  312.             objStream.LoadFromFile File
  313.         End If
  314.         Err.clear
  315.     End Function
  316. 'CloseStream
  317.     Public Function CloseStream()
  318.         objStream.close
  319.         Set objStream = nothing
  320.     End Function
  321. 'Read String    From Byte
  322.     Private Function ReadString(binstr)
  323.         If LenB(binstr) > 2 And ReadHex(RightB(binstr, 2)) = "0000" Then
  324.             ReadString = LeftB(binstr, LenB(binstr)-2)
  325.         Else
  326.             ReadString = binstr
  327.         End If
  328.     End Function
  329. 'Read Hex   From Byte
  330.     Private Function ReadHex(binstr)
  331.         ReadHex = ""
  332.         on error resume next
  333.         For i = 1 To LenB(binstr)
  334.             ReadHex = ReadHex & Hex((AscB(MidB(binstr,i,1)) And &HF0)/16) & Hex(AscB(MidB(binstr,i,1)) And &H0F)
  335.         Next
  336.         ReadHex = LCase(ReadHex)
  337.     End Function
  338. 'Read Unsigned Long(Little Endian) From Byte
  339.     Private Function ReadLong_UL(binstr)
  340.         ReadLong_UL = 0
  341.         on error resume next
  342.         ReadLong_UL = AscB(MidB(binstr,1,1)) + AscB(MidB(binstr,2,1))*256 + AscB(MidB(binstr,3,1))*65536 + AscB(MidB(binstr,4,1))*16777216
  343.     End Function
  344. 'Read Unsigned Short(Little Endian) From Byte
  345.     Private Function ReadShort_UL(binstr)
  346.         ReadShort_UL = 0
  347.         on error resume next
  348.         ReadShort_UL = AscB(MidB(binstr,1,1)) + AscB(MidB(binstr,2,1))*256
  349.     End Function
  350. 'Write String   To Byte
  351.     Private Function WriteString(rawstr)
  352.         If LenB(rawstr) = 0 Then
  353.             WriteString = rawstr
  354.         Else
  355.             WriteString = rawstr&Chr(0)
  356.         End If
  357.     End Function
  358. 'Write Hex  To Byte
  359.     Private Function WriteHex(hexstr)
  360.         For i = 1 To Len(hexstr)/2
  361.             WriteHex = WriteHex & ChrB("&H"&Mid(hexstr, i*2-1, 2))
  362.         Next
  363.     End Function
  364. 'Write Unsigned Long(Little Endian) To Byte
  365.     Private Function WriteLong_UL(lnum)
  366.         WriteLong_UL = ChrB(lnum Mod 256) & ChrB(lnum/256 Mod 256) & ChrB(lnum/65536 Mod 256) & ChrB(lnum/16777216 Mod 256)
  367.     End Function
  368. 'Write Unsigned Short(Little Endian) To Byte
  369.     Private Function WriteShort_UL(snum)
  370.         WriteShort_UL = ChrB(snum Mod 256) & ChrB(snum/256 Mod 256)
  371.     End Function
  372. 'Print error message
  373.     Private Function Err(errmsg)
  374.         Response.Write "<b>err:</b>"
  375.         Response.Write errmsg
  376.         Response.End
  377.     End Function
  378. 'Print debug message
  379.     Private Function DebugMsg(msg)
  380.         If DebugMode = True Then
  381.             Response.Write "<b>Debug:</b>"
  382.             Response.Write msg
  383.             Response.Write "<br />"
  384.         End If
  385.     End Function
  386. End Class
  387. %>

测试代码:wma.asp
  1. <!-- #include file="wmaExif.class.asp" -->
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  6. <title>利用ASP获取WMA的TAG信息</title>
  7. </head>
  8. <body>
  9. <%
  10. dim info
  11. set info = New wmaExif
  12. set info.File=request("soundPath")
  13. If info.CheckSize = True Then
  14.     info.Parse
  15.   Response.write "歌名:"&info.GetInfo("Title")&"<br/>"
  16.   Response.write "歌手:"&info.GetInfo("Artist")&"<br/>"
  17.   Response.write "版权:"&info.GetInfo("CopyRight")&"<br/>"
  18.   Response.write "注释:"&info.GetInfo("Description")&"<br/>"
  19.   Response.write "专辑:"&info.GetInfo("WM/AlbumTitle")&"<br/>"
  20.   Response.write "曲目:"&info.GetInfo("WM/TrackNumber")&"<br/>"
  21.   Response.write "发行年份:"&info.GetInfo("WM/Year")&"<br/>"
  22.   Response.write "类型:"&info.GetInfo("WM/Genre")&"<br/>"
  23.   Response.write "测试:"&info.GetInfo("AverageLevel")&"<br/>"
  24. '  info.SetInfo "Title", info.GetInfo("Title")
  25.   info.SetInfo "Title""歌名测试"
  26.   info.SetInfo "Artist""歌手测试"
  27.   info.SetInfo "CopyRight""版权测试"
  28.   info.SetInfo "Description""备注测试"
  29.   info.SetInfo "WM/Year""2009"
  30.   info.SetInfo "WM/AlbumTitle""test"
  31.   info.SetInfo "WM/AlbuadmTitle""test1"
  32. '  info.SaveChange
  33.   info.SaveTo(server.mappath("test.wma"))
  34. Else
  35.     Response.Write "error"
  36. End If
  37. set info = Nothing
  38. %>
  39. </body>
  40. </html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值