CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Threaded View

  1. #4
    Join Date
    Jul 2010
    Location
    Athens, Greece
    Posts
    3

    Re: Get Video File Duration

    Like countless others, I also could not seem to find how to get video durations. Many forums have been searched, all with many people searching for the same thing, and none with any successful methods of doing so. However, I used to be able to do this in VBA, and I have found a way to make this work in VB.NET by adapting the code I used to use. Basically, in the details view of windows, it already lists the durations of both video and audio files. So basically, we just extract it from windows. Some file formats even windows cannot get this information (like for example .mp4) but for all that windows can find, here's how to do it:

    Code:
     '1) Declare this at the top of your code module: 
    Imports System.IO 
     
     '2) Declare the function itself:
    
    Function GetDuration(ByVal MovieFullPath As String) As String
       If File.Exists(MovieFullPath) Then
          Dim objShell As Object = CreateObject("Shell.Application")
          Dim objFolder As Object = _
             objShell.Namespace(Path.GetDirectoryName(MovieFullPath))
                For Each strFileName In objFolder.Items
                   If strFileName.Name = Path.GetFileName(MovieFullPath) Then
                      Return objFolder.GetDetailsOf(strFileName, 21).ToString
                      Exit For
                      Exit Function
                   End If
                Next
                Return ""
       Else
          Return ""
       End If
    End Function
    
     '3) Call the function like this:
    Dim MyDuration As String =  GetDuration("C:\SomePath\SomeVideoOrAudioFile.avi")
    Hope this helps!!
    Last edited by Belial9; July 30th, 2010 at 08:35 PM. Reason: BB code tags needed adding

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured