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?
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:
'1) Declare this at the top of your code module:
'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 = _
For Each strFileName In objFolder.Items
If strFileName.Name = Path.GetFileName(MovieFullPath) Then
Return objFolder.GetDetailsOf(strFileName, 21).ToString
'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
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!