0

我找到了一个调用Microsoft.DirectX.AudioVideoPlayback来获取视频文件长度的函数。

这是该代码:

`Private Function GetVideoInformation(ByVal videoFilePath As String) As VideoInfo
    Try
        If My.Computer.FileSystem.FileExists(videoFilePath) Then
            Dim videoToGetInfoOn As Microsoft.DirectX.AudioVideoPlayback.Video
            videoToGetInfoOn = New Microsoft.DirectX.AudioVideoPlayback.Video(videoFilePath)
            Dim atpf As Double = videoToGetInfoOn.AverageTimePerFrame
            Dim vidSize As New Size
            vidSize = videoToGetInfoOn.Size

        Dim thisVideoInfo As New VideoInfo
            thisVideoInfo.videoWidth = vidSize.Width
            thisVideoInfo.videoHeight = vidSize.Height
        thisVideoInfo.videoDuration = videoToGetInfoOn.Duration
        If videoToGetInfoOn.Duration > 0 Then
            defaultLength = videoToGetInfoOn.Duration
        End If

        If atpf > 0 Then
            thisVideoInfo.videoFps = 1 / atpf
        Else
            thisVideoInfo.videoFps = 0
        End If

        Return thisVideoInfo
    Else
        Throw New Exception("Video File Not Found" & vbCrLf & vbCrLf & videoFilePath)

        Return Nothing
    End If
   Catch ex as Exception
     msgbox(ex.message)
   End Try
End Function`

我有一个计时器,它每 2 秒调用一次此函数来检查许多视频,并且该应用程序在前 10 个左右的视频中运行良好。之后,它抛出

"Error in application" 

改为消息。

4

2 回答 2

0

一般来说,来自 DirectShow/DirectX 的东西必须按照文档要求的方式处理,否则会发生这种事情。在这里,您正在创建videoToGetInfoOn对象但从不释放它们。

您需要videoToGetInfoOn = Nothing在过程退出之前显式释放您和它分配的所有资源。试试看。

我将补充一点,MediaInfo.DLL它可用于从媒体文件中获取所有内容,而无需 Dx 的开销。有一个命令行版本,您可以通过阅读标准输出来阅读。

于 2013-09-30T23:24:37.100 回答
0

我让它工作了。

代码需要一个 dispose 方法。

这是最终代码:

`Private Function GetVideoInformation(ByVal videoFilePath As String) As VideoInfo
    Try
      If My.Computer.FileSystem.FileExists(videoFilePath) Then
        Dim videoToGetInfoOn As Microsoft.DirectX.AudioVideoPlayback.Video
        videoToGetInfoOn = New Microsoft.DirectX.AudioVideoPlayback.Video(videoFilePath)
        Dim atpf As Double = videoToGetInfoOn.AverageTimePerFrame
        Dim vidSize As New Size
        vidSize = videoToGetInfoOn.Size

    Dim thisVideoInfo As New VideoInfo
        thisVideoInfo.videoWidth = vidSize.Width
        thisVideoInfo.videoHeight = vidSize.Height
    thisVideoInfo.videoDuration = videoToGetInfoOn.Duration
    If videoToGetInfoOn.Duration > 0 Then
        defaultLength = videoToGetInfoOn.Duration
    End If

    If atpf > 0 Then
        thisVideoInfo.videoFps = 1 / atpf
    Else
        thisVideoInfo.videoFps = 0
    End If

    videoToGetInfoOn.Dispose() 'this line here needed to be added
    Return thisVideoInfo
Else
    Throw New Exception("Video File Not Found" & vbCrLf & vbCrLf & videoFilePath)

    Return Nothing
End If
  Catch ex as Exception
     msgbox(ex.message)
  End Try
End Function`
于 2013-10-01T12:34:00.980 回答