Get Video File Duration
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Get Video File Duration

  1. #1
    Join Date
    Apr 2008
    Posts
    43

    Get Video File Duration

    I have created my own application for organising my video library. when the application starts a datagridview displays all of the videos in my database. i can't get it to display the duration of each video. i assumed i'd be able to loop through each row in the datagridview and use the FileInfo class to look up the properties of each file and get the duration from there because if you look at the properties of any .avi and then click on Details you can see the duration of the video but i can't figure it out. can anyone help me?

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: Get Video File Duration

    Save them in the database
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Get Video File Duration

    You need to open each file and read the data out of the headers ...

    if you look at the AVI format you can use the Frame Rate and Total frames to get the length in seconds of the clip ....

    Total seconds = Total frames / Frame rate ...

    Depending on how you do it this should not take too long to read all the listed files..

    Gremmy..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  4. #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

  5. #5
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: Get Video File Duration

    Please use CODE TAGS when you post.
    Code:
    ' And remove the old post by deleting it
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

    Thumbs up Re: Get Video File Duration

    Quote Originally Posted by dglienna View Post
    Please use CODE TAGS when you post.
    Code:
    ' And remove the old post by deleting it
    Thanks dglienna for pointing me out how to space the code text. It was my first post on this forum, and I had a feeling there was a way to get the code to keep the correct spacings, just I didn't see the page with all the code syntaxes. But now I know it's standard BB code on this forum, they should all be looking how I wanted them from now on. It's now been amended, and the old post deleted. Does this look better?

    Once again, thanks for pointing me in the right direction!

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

    Re: Get Video File Duration

    Just an addition. My code was written for Windows XP, but if you are runnning a different operating system, you must change the value from the following line:

    XP:
    Code:
    Return objFolder.GetDetailsOf(strFileName, 21).ToString
    Vista:
    Code:
    Return objFolder.GetDetailsOf(strFileName, 36).ToString
    Windows 7:
    Code:
    Return objFolder.GetDetailsOf(strFileName, 27).ToString
    (Obviously, if you intend your application to be used cross-platform, then the more efficient way would be to check which OS is being used first, and then dynamically add the value)

  8. #8
    Join Date
    Jun 2014
    Posts
    1

    Re: Get Video File Duration

    XP:
    Code:
    Return objFolder.GetDetailsOf(strFileName, 21).ToString
    Vista:
    Code:
    Return objFolder.GetDetailsOf(strFileName, 36).ToString
    Windows 7:
    Code:
    Return objFolder.GetDetailsOf(strFileName, 27).ToString

    very useful informatioin....
    does anybody knows the right number (21, 36, 27....) for windows 8...?
    thanks in advance.....

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center