在VBScript中读取音乐文件长度
我只是想知道是否有一种方法可以通过VBScript将mp3文件的长度以秒为单位输入到变量中在VBScript中读取音乐文件长度,vbscript,Vbscript,我只是想知道是否有一种方法可以通过VBScript将mp3文件的长度以秒为单位输入到变量中 Set objShell = CreateObject("Shell.Application") Set Ag=Wscript.Arguments set WshShell = WScript.CreateObject("WScript.Shell") WshShell.RegWrite "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.RegWrite "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\" & Wscript.ScriptName & "\", Chr(34) & Wscript.ScriptFullName & Chr(34)
WshShell.RegWrite "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\" & Left(Wscript.ScriptName, Len(Wscript.ScriptName)-3) & "exe" & "\", Chr(34) & Wscript.ScriptFullName & Chr(34)
Set Fldr=objShell.NameSpace(Ag(0))
Set FldrItems=Fldr.Items
Set fso = CreateObject("Scripting.FileSystemObject")
Set DeskFldr=objShell.Namespace(16)
FName=fso.buildpath(DeskFldr.self.path, "Folder Property List.txt")
Set ts = fso.OpenTextFile(FName, 8, vbtrue)
For x = 0 to 50
t1 = t1 & Fldr.GetDetailsOf(vbnull, x) & " (Shell)" & vbtab
Next
ts.write FLDR.self.path &vbcrlf
ts.Write T1 & vbcrlf
T1=""
For Each FldrItem in FldrItems
For x = 0 to 50
t1 = t1 & Fldr.GetDetailsOf(FldrItem, x) & vbtab
Next
t1=t1 & vbcrlf
ts.Write T1
T1=""
Next
'msgbox FName & "has a tab delimited list of all properties"
如果将文件夹放在上面,它将为文件夹中的文件生成所有shell属性的列表。我没有任何mp3文件。这将取决于你安装了什么软件,以及会发生什么。Wma文件将持续时间保留为空。而且属性在不同的Windows版本之间发生了巨大的变化
第一个循环获取可用的属性(通过为folderitem传递null),第二个循环获取每个folderitem的属性。使用Windows Media Player控件库是另一种方法。使用此选项之前,请确保路径正确
Function MediaDuration(path)
With CreateObject("Wmplayer.OCX")
.settings.mute = True
.url = path
Do While Not .playState = 3 'wmppsPlaying
WScript.Sleep 50
Loop
MediaDuration = Round(.currentMedia.duration) 'in seconds
'MediaDuration = .currentMedia.durationString 'in hh:mm:ss format
.Close
End With
End Function
WScript.Echo MediaDuration("C:\media\song.mp3")
(改编自一个关于JScript的类似问题。)
可以使用Windows Shell对象的方法获取音频文件长度。此技术支持所有音频文件类型,其元数据可以由Windows资源管理器本机读取和显示
但是,请注意,长度属性的索引在不同的Windows版本上是不同的:在Windows XP/2003上为21,在Windows Vista+上为27。有关详细信息,请参阅和。您需要在脚本中考虑这一点
示例代码:
Const LENGTH = 27 ' Windows Vista+
' Const LENGTH = 21 ' Windows XP
Dim oShell : Set oShell = CreateObject("Shell.Application")
Dim oFolder : Set oFolder = oShell.Namespace("C:\Music")
Dim oFile : Set oFile = oFolder.ParseName("Track.mp3")
Dim strLength : strLength = oFolder.GetDetailsOf(oFile, LENGTH)
WScript.Echo strLength
示例输出:
00:05:18
您知道是否有任何方法可以检查该文件是否受支持?这是否支持所有文件类型?@Lugia101101:它适用于Windows支持的所有文件类型。MP3肯定是受支持的。要检查是否支持其他音频格式,请打开音频文件属性,查看音频元数据(如长度)是否显示详细信息。对于不支持的文件类型,
GetDetailsOf
将以空字符串形式报告长度。我有类似的要求。谢谢你的回答。但是,如果通过oShell.Namespace(sFolderName)中的变量传递文件夹名称,则这不起作用。你知道怎么处理吗?@Karpak,你的Windows版本是什么,以及LENGTH
和sFolderName
的值是多少?文件类型是MP3还是其他?你有没有得到任何错误或只是不正确的结果?@Helen,这是windows 10。无论如何,当我将字符串作为变量时,它开始工作(而不是sFolderName,我更改为vFolderName,并将vFolderName作为variant.Thx,以获取您对此OP.up的答案)。