首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >修复directX函数,该函数在VB.NET中获取视频长度

修复directX函数,该函数在VB.NET中获取视频长度
EN

Stack Overflow用户
提问于 2013-09-30 21:59:37
回答 2查看 315关注 0票数 0

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

这是代码:

代码语言:javascript
复制
`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个视频中运行得很好。在那之后,它

代码语言:javascript
复制
"Error in application" 

代之以留言。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-30 23:24:37

通常,DirectShow/DirectX中的内容必须按照文档所要求的方式处理,否则这类事情就会发生。在这里,您正在创建videoToGetInfoOn对象,但从未发布它们。

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

我要补充的是,可以使用MediaInfo.DLL从媒体文件中获取everything,而不需要Dx的开销。有一个命令行版本,您可以从stdout中读取。

票数 0
EN

Stack Overflow用户

发布于 2013-10-01 12:34:00

我让它起作用了。

代码需要一个dispose方法。

以下是最终代码:

代码语言:javascript
复制
`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`
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19104610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档