Search code examples
audiomp3metadatajscriptwma

Get Audio File Duration Via a Microsoft DLL or Utility


Is there a Windows dll or utility that can be used to see the duration (HH:MM:SS) of an audio file (WMA, MP3, or WAV) that can be accessed from a script (specifically JScript)?

I know there is one, or something else can read file metadata, but I'm unfamiliar with it.


Solution

  • You can use the GetDetailsOf method of the Windows Shell Folder object to get the audio file length. This technique supports all audio file types whose metadata can be read and displayed by Windows Explorer natively.

    However, note that the index of the Length attribute is different on different Windows versions: it's 21 on Windows XP/2003 and 27 on Windows Vista+. See this page and this my answer for details. You will need to take this into account in your script.

    Example code:

    var LENGTH = 27; // Windows Vista+
    // var LENGTH = 21; // Windows XP
    
    var oShell = new ActiveXObject("Shell.Application");
    var oFolder = oShell.Namespace("C:\Music");
    var oFile   = oFolder.ParseName("Track.mp3");
    
    WScript.Echo(oFolder.GetDetailsOf(oFile, LENGTH));
    

    Example output:

    00:05:18