user3337590
user3337590

Reputation: 160

get duration of .mp4 file

I am making an application in .Net(VB) in which display a list of video files (.mp4). I want show to duration of each file as well. But I couldn't find the way how to got duration of mp4 file. Please provide and hint or guideline. Thanks

Upvotes: 1

Views: 5471

Answers (2)

akhil kumar
akhil kumar

Reputation: 1618

You can extract the file attributes and get duration of particular file.this function will help:

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
               End If
            Next

            Return ""
   Else
      Return ""
   End If
End Function

and call function like

Dim MyDuration As String = GetDuration("C:\SomePath\SomeVideoOrAudioFile.avi")

hope this helps!!

Updates:

in case of using a different operating system than XP, you should make small changes for the above code..

if you are using Windows 7, change

Dim MyDuration As String = GetDuration("C:\SomePath\SomeVideoOrAudioFile.avi")

to

Return objFolder.GetDetailsOf(strFileName, 36).ToString

For Windows Vista Use Return objFolder.GetDetailsOf(strFileName, 27).ToString

Upvotes: 1

graeme rees
graeme rees

Reputation: 11

This is akhil kumar Code I'm Just Updating Some of it

You can extract the file attributes and get duration of particular file.this function will help:

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

and call function like

Dim MyDuration As String = GetDuration("C:\SomePath\SomeVideoOrAudioFile.avi")

hope this helps!!

Updates

in case of using a different operating system than XP,you should make small changed for the above code..

if you are using Windows 7, change

Dim MyDuration As String = GetDuration("C:\SomePath\SomeVideoOrAudioFile.avi")

to

Return objFolder.GetDetailsOf(strFileName, 36).ToString.

For Windows Vista Use Return objFolder.GetDetailsOf(strFileName, 27).ToString

For Windows 8 Use Return objFolder.GetDetailsOf(strFileName, 28).ToString

For Windows 10 Use Return objFolder.GetDetailsOf(strFileName, 27).ToString

Upvotes: 1

Related Questions