• Determining Audio Files Length

    Author
    Topic
    #459116

    I have a sub in Access, posted below, which uses the Microsoft Scripting Runtime library, to determine files properties but, using Intellisense to see the list of all the properties available in VBA for Scripting.File, I don’t see a Length(hour, minutes, seconds) property, there’s only a Size property. But I know that if, in Windows Explorer, you hover the mouse over an audio file name, it also shows its time length, is there a way to get that in VBA?

    Code:
    Public Sub GetFilesNamesFromFolder(strFolderPath As String)
    
    On Error GoTo ErrorHandler
    
       Dim fso As Scripting.FileSystemObject
       Dim fld As Scripting.Folder
       Dim fil As Scripting.File
       Dim strSQL As String
       Dim dbs As DAO.Database
       Dim rst As DAO.Recordset
       Dim strPrompt As String
       Dim strTitle As String
       Dim strTable As String
       Dim intMissingCount As Integer
       
    
       strTable = "tblFilesFromFolder"
       Set fso = CreateObject("Scripting.FileSystemObject")
       If Not fso.FolderExists(strFolderPath) Then
    	  'Specified folder does not exist
    	  strPrompt = "Please enter a valid folder path"
    	  strTitle = "Folder path not found"
    	  MsgBox strPrompt, vbCritical + vbOKOnly, strTitle
    	  GoTo ErrorHandlerExit
    	  Forms![fdlgSelectFolder].SetFocus
       Else
    	  'Folder exists
    	  Set dbs = CurrentDb
    	  Set rst = dbs.OpenRecordset(strTable)
    	  Set fld = fso.GetFolder(strFolderPath)
    	  For Each fil In fld.Files
    		 If fil.Name = "DSSMANAGEMENT.Dat" Then GoTo mm
    		 rst.AddNew
    		 rst![FileName] = fil.Name
    		 If Forms!fdlgSelectFolder!m Then
    		 rst![DateChecked] = fil.DateLastModified
    		 Else
    		 rst![DateChecked] = fil.DateCreated
    		 End If
    		 rst.Update
    mm:
    	  Next fil
       End If
       
    
       
    ErrorHandlerExit:
       Exit Sub
    
    ErrorHandler:
    If Not Err = 3022 Then
    
       MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
     
       Else: MsgBox "You already have all or some of the files you want to import."
       End If
       Resume ErrorHandlerExit
    
    End Sub
    Viewing 0 reply threads
    Author
    Replies
    • #1156665

      See Retrieving Extended File Properties.
      If you’d like to view a working example, see John Walkenbach’s Creating A List of MP3 Files. It’s for Excel but the code can be adapted for Access.

      • #1156944

        Thank you Hans, I’ve run the Sub DisplayMP3Info() included in the Excel file and it works superbly, listing all the MP3 files contained in a folder, is there any way to modify it so that it could list other types as well, such as WMA or WAV?

        • #1156950

          John Walkenbach’s DisplayMP3Info procedure has a line

          If Right(.FoundFiles(i), 3) = “mp3” Then

          You can change this to

          If Right(.FoundFiles(i), 3) = “mp3” Or Right(.FoundFiles(i), 3) = “wma” Or Right(.FoundFiles(i), 3) = “wav” Then

          If you have a folder that contains only sound files, you could even remove the If … Then line and the corresponding End If.

          • #1156952

            Thank you Hans, unfortunately that modification works for detecting wma’s duration but not for detecting wav files duration; the cells corresponding to wav files duration in the Excel procedure output remain empty. Any workaround available?

            • #1156953

              I have thousansds of .wav files on my PC, but the duration isn’t listed in the file properties for any of them, only the bit rate. So I guess that this information simply isn’t available for .wav files.

            • #1156971

              Because the “bit rate” doesn’t vary for .WAV files, I believe you can determine the length of a .WAV file from its size by using what I assume is a pretty simple formula. (I believe there’s a set amount of space at the very start of a .WAV file that’s reserved for certain information, and then the rest of the file would simply have so many bytes/second.) I don’t know what the exact formula is, but I suspect you can find it pretty easily with some internet searching.

              Assuming you can, your workaround would simply be to have your code do its own calculation for the .WAV files, rather than getting the info from the file’s properties.

            • #1156976

              Thank you all for your timely and extremely helfpul input
              For the sake of completion, here‘s where you can find the formula sought after.

            • #1157048

              Oops. The link you posted reminded me that .WAV files theoretically can have different sample rates, so it’s not quite as simple as I said. However, assuming you’re talking about a typical 2-channel .WAV file ripped from (or ready to be burned to) a CD, you can basically just divide the file size (in bytes) by 176,400.

              As further explanation (in case anyone ever reading this post is trying to figure out how or why this works), I note the following:

              1. I think a slightly more accurate formula would actually deduct the header size from the file size before dividing, but since the header is only 43 bytes long (assuming the link you posted is accurate), ignoring that is unlikely to affect the result (in seconds).

              2. My simple divide-by-176,400 formula assumes a sample rate of 44,100 samples per second (standard CD audio). The 44,100 gets multiplied by 2 twice — first because there are 2 bytes per sample, and second because there are 2 channels (left and right).

            • #1157049

              I have a wav file which is 274896 bytes and has a bit rate of 13kbps, what would its (even approximate)duration be?

            • #1157055

              I have a wav file which is 274896 bytes and has a bit rate of 13kbps, what would its (even approximate)duration be?

              You can use the code from the site that you referred to yourself to find out…

            • #1157057

              The sub at that site gives incorrect results, the wav file I attach here is 2 minutes 49 seconds long but the sub at the site gives
              0.528630769230769 seconds instead, while Windows Media Player gets the length right. I wonder how they do it…

            • #1157059

              I suspect that this is some kind of compressed audio format disguised as a .wav.
              You can calculate the length by dividing the file size in bytes by the number of bytes per second – the code from your link retrieves this from the file header too.

              274888 bytes / 1625 bytes per second = 169.16 seconds = 2:49.

            • #1157590

              Perfect. Thank you Hans

          • #1162186

            John Walkenbach’s DisplayMP3Info procedure has a line

            If Right(.FoundFiles(i), 3) = “mp3” Then

            You can change this to

            If Right(.FoundFiles(i), 3) = “mp3” Or Right(.FoundFiles(i), 3) = “wma” Or Right(.FoundFiles(i), 3) = “wav” Then

            If you have a folder that contains only sound files, you could even remove the If … Then line and the corresponding End If.

            The FileLen function in the attached mdb uses the GetMP3TagInfo function from John Walkenbach’s Creating A List of MP3 Files and it works perfectly, returning the file length of WMA and MP3 files. I’ve tried it yesterday on a pc running Access 2003 on Vista and I get FileLen = “” rather than a string specifying the file length. What gives??!! Can anyone try that out and confirm or disprove my finding?

            • #1162190

              I’ve tested your database in Access 2003 SP3 and it appears to work OK:

            • #1162204

              I’ve tested your database in Access 2003 SP3 and it appears to work OK:

              Thanks Hans, can you test a wma file, please? Because that’s what I tried. You’re testing it on Vista, right?

            • #1162209

              It also works with .wma files:
              [indent]
              ? FileLen(“C:Documents and SettingsAll UsersDocumentsMy Musicmusic.wma”)
              00:03:33
              [/indent]
              But I’m testing in Windows XP – I don’t have Vista.

            • #1162212

              It also works with .wma files:
              [indent]
              ? FileLen(“C:Documents and SettingsAll UsersDocumentsMy Musicmusic.wma”)
              00:03:33
              [/indent]
              But I’m testing in Windows XP – I don’t have Vista.

              That’s the problem, it works in XP but not in Vista.

            • #1162214

              So the solution is easy…

            • #1162217

              So the solution is easy…

              I’m afraid setting fire to all Vista pc’s isn’t an option

            • #1162298

              I’ve tried it yesterday on a pc running Access 2003 on Vista and I get FileLen = “” rather than a string specifying the file length.

              Is Access/VBA running with administrator privileges? If not, does that make any difference?

            • #1162314

              Is Access/VBA running with administrator privileges? If not, does that make any difference?

              Hi Jeff, thanks for stopping by. 🙂
              How do you assess that? If you mean the user is logged on in Vista as an administrator then yes she is.

            • #1162315

              Vista is more restrictive than earlier versions of Windows – users normally aren’t administrator users themselves.
              You can, however, open an application with administrator privileges: right-click an application and select “Run as administrator” from the popup menu, or right-click the shortcut used to start the application, select Properties, click Advanced… and tick the check box “Run as administrator”, then click OK.

            • #1162316

              Vista is more restrictive than earlier versions of Windows – users normally aren’t administrator users themselves.
              You can, however, open an application with administrator privileges: right-click an application and select “Run as administrator” from the popup menu, or right-click the shortcut used to start the application, click Advanced… and tick the check box “Run as administrator”, then click OK.

              Thanks Hans, I don’t have access to a Vista-run pc right now but, in any case, is it possible to make the “Run as administrator” choice permanent so you don’t have to do it every time?

            • #1162318

              If you specify it in the advanced properties of the shortcut, as described in my previous reply, the application will always start with “Run as administrator”.

            • #1163738

              If you specify it in the advanced properties of the shortcut, as described in my previous reply, the application will always start with “Run as administrator”.

              I’ve been able to access a Vista pc only right now and I go right-click the shortcut used to start the application, select Properties, click the Advanced button… but the check box “Run as administrator” is grayed out as you can see in the attached jpg.

            • #1163740

              I don’t have Vista, so I can’t help you with this.

              By the way, could you remove the white space from your attachment? Thanks.

            • #1163742

              I don’t have Vista, so I can’t help you with this.

              By the way, could you remove the white space from your attachment? Thanks.

              Done

            • #1164619

              I don’t have Vista, so I can’t help you with this.

              By the way, could you remove the white space from your attachment? Thanks.

              The line FileLen = GetMP3TagInfo(FileName, 21) needs to be changed to
              FileLen = GetMP3TagInfo(FileName, 36)
              because many of the extended data values on Windows Vista have been renumbered.
              Now, how do I tell VBA that the code is running in Vista rather than XP?

            • #1164635

              Copy this code to the top of a module:

              Code:
              Private Type OSVERSIONINFO
                dwOSVersionInfoSize As Long
                dwMajorVersion As Long
                dwMinorVersion As Long
                dwBuildNumber As Long
                dwPlatformId As Long
                szCSDVersion As String * 128
              End Type
              
              Private Declare Function apiGetVersionEx Lib "kernel32" _
                Alias "GetVersionExA" _
                (lpVersionInformation As Any) _
                As Long

              Use like this:

              Code:
              Sub TestOS()
                Dim os As OSVERSIONINFO
                os.dwOSVersionInfoSize = Len(os)
                apiGetVersionEx os
                Debug.Print os.dwMajorVersion + os.dwMinorVersion / 10
              End Sub

              Here are the results for some recent versions of Window:

              Windows 2000: 5.0
              Windows XP: 5.1
              Windows Server 2003: 5.2
              Windows Vista: 6.0
              Windows 7: 6.1

    Viewing 0 reply threads
    Reply To: Determining Audio Files Length

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: