我找到了一个函数,它调用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" 代之以留言。
发布于 2013-09-30 23:24:37
通常,DirectShow/DirectX中的内容必须按照文档所要求的方式处理,否则这类事情就会发生。在这里,您正在创建videoToGetInfoOn对象,但从未发布它们。
在您的过程退出之前,您需要显式地释放您和它用videoToGetInfoOn = Nothing分配的所有资源。试试看。
我要补充的是,可以使用MediaInfo.DLL从媒体文件中获取everything,而不需要Dx的开销。有一个命令行版本,您可以从stdout中读取。
发布于 2013-10-01 12:34:00
我让它起作用了。
代码需要一个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`https://stackoverflow.com/questions/19104610
复制相似问题