AutoHotkey: Get Media Information and Display

Applies To:
  • Microsoft Windows 7 Professional v6.1.7601 32-bit
  • AutoHotkey v1.1.33.02

What?
This is a copied article just to store a permanent reference for if I ever need to build an app similar to the file explorer in the Microsoft Windows OS.

Why?
I had an app for use in MS Windows 7 to display the properties of sound files though this is also applicable to media files in general.

Windows File Explorer: Audio File Windows File Explorer: Video File

How?
So first I'm going to list a function I use called "Filexpro()", then list some examples of usage. And in the second-to-last section, I'll quickly convert a 100 nanoseconds unit to display Hours : Minutes : Seconds.

We could have tried out some cool new DLL files for MS Windows 10 but we needed something lightweight that doesn't require any extra files and that could work on MS Windows 7 (32-bit) for this particular app. For this reason, we are going to use a function by Skan called Filexpro() which is copied in the below:
Filexpro( sFile := "", Kind := "", P* ) {           ; v.90 By SKAN on D1CC @ goo.gl/jyXFo9
    Local
    Static xDetails
    If ( sFile = "" )
    {                                                           ;   Deinit static variable
        xDetails := ""
        Return
    }
    fex := {}, _FileExt := ""
    Loop, Files, % RTrim(sfile,"\*/."), DF
    {
        If not FileExist( sFile:=A_LoopFileLongPath )
        {
            Return
        }
        SplitPath, sFile, _FileExt, _Dir, _Ext, _File, _Drv
        If ( p[p.length()] = "xInfo" )                          ;  Last parameter is xInfo
        {
            p.Pop()                                           ;         Delete parameter
            fex.SetCapacity(11)                               ; Make room for Extra info
            fex["_Attrib"]    := A_LoopFileAttrib
            fex["_Dir"]       := _Dir
            fex["_Drv"]       := _Drv
            fex["_Ext"]       := _Ext
            fex["_File"]      := _File
            fex["_File.Ext"]  := _FileExt
            fex["_FilePath"]  := sFile
            fex["_FileSize"]  := A_LoopFileSize
            fex["_FileTimeA"] := A_LoopFileTimeAccessed
            fex["_FileTimeC"] := A_LoopFileTimeCreated
            fex["_FileTimeM"] := A_LoopFileTimeModified
        }
        Break
    }
    If Not ( _FileExt )                                   ;    Filepath not resolved
    {
        Return
    }
    objShl := ComObjCreate("Shell.Application")
    objDir := objShl.NameSpace(_Dir)
    objItm := objDir.ParseName(_FileExt)
    If ( VarSetCapacity(xDetails) = 0 )                           ;     Init static variable
    {
        i:=-1,  xDetails:={},  xDetails.SetCapacity(309)
        While ( i++ < 309 )
        {
            xDetails[ objDir.GetDetailsOf(0,i) ] := i
        }
        xDetails.Delete("")
    }
    If ( Kind and Kind <> objDir.GetDetailsOf(objItm,11) )        ;  File isn't desired kind
    {
        Return
    }
    i:=0,  nParams:=p.Count(),  fex.SetCapacity(nParams + 11)
    While ( i++ < nParams )
    {
        Prop := p[i]
        If ( (Dot:=InStr(Prop,".")) and (Prop:=(Dot=1 ? "System":"") . Prop) )
        {
            fex[Prop] := objItm.ExtendedProperty(Prop)
            Continue
        }
        If ( PropNum := xDetails[Prop] ) > -1
        {
            fex[Prop] := ObjDir.GetDetailsOf(objItm,PropNum)
            Continue
        }
    }
    fex.SetCapacity(-1)
    Return fex
}

Usage
v_MyMediaFile := "C:\Users\Public\Music\Sample Music\Maid with the Flaxen Hair.mp3"
a_MediaObject := Filexpro( v_MyMediaFile,, "System.Media.Duration"
                                        , "System.Audio.ChannelCount"
                                        , "System.Audio.Compression"
                                        , "System.Audio.EncodingBitrate"
                                        , "System.Audio.Format"
                                        , "System.Audio.IsVariableBitRate"
                                        , "System.Audio.PeakValue"
                                        , "System.Audio.SampleRate"
                                        , "System.Audio.SampleSize"
                                        , "System.Audio.StreamName"
                                        , "System.Audio.StreamNumber" )

v_MyMediaFile := "C:\Users\Public\Videos\Sample Videos\Wildlife.wmv"
a_MediaObject := Filexpro( v_MyMediaFile,, "System.Media.Duration"
                                        , "System.Media.FrameCount"
                                        , "System.Video.EncodingBitrate"
                                        , "System.Video.Format"
                                        , "System.Video.FrameHeight"
                                        , "System.Video.FrameRate"
                                        , "System.Video.FrameWidth"
                                        , "System.Video.HorizontalAspectRatio"
                                        , "System.Video.Orientation"
                                        , "System.Video.VerticalAspectRatio" )

MsgBox % a_MediaObject["System.Media.Duration"]
// note this returns in 100ns units (= 100 nanoseconds) as per Microsoft Docs.

100ns Units
So the following snippet of code will convert your 100ns units to a displayable Hours : Minutes : Seconds format which is more user-friendly:
; Function: Convert100nsToHMS: Convert MS 100 nanoseconds to Hours Minutes Seconds for display
Convert100nsToHMS( v_100NsUnits )
{
    v_NanoSeconds100    := v_100NsUnits * 1
    v_MicroSeconds      := Floor( v_NanoSeconds100 / 10)
    v_MilliSeconds      := Floor( v_MicroSeconds / 1000)
    v_Seconds           := Floor( v_MilliSeconds / 1000)

    v_CalcHours         := Floor( v_Seconds / 3600 )
    v_CalcMinutes1      := v_Seconds / 60
    v_CalcMinutes2      := Mod( v_CalcMinutes1, 60 )
    v_CalcMinutes       := Floor( v_CalcMinutes2 )
    v_CalcSeconds       := Mod( v_Seconds, 60)

    v_DispHours         := SubStr("0" . v_CalcHours, -1)
    v_DispMinutes       := SubStr("0" . v_CalcMinutes, -1)
    v_DispSeconds       := SubStr("0" . v_CalcSeconds, -1)

    v_DispHMS           := v_DispHours ":" v_DispMinutes ":" v_DispSeconds

    Return v_DispHMS
}

; ------------- Usage -------------
v_My100NsUnits  := 9858250000
v_DisplayHMS    := Convert100nsToHMS( v_My100NsUnits )

; yields 00:16:25

Source(s):

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.