获取视频的长度 [英] Getting length of video

查看:531
本文介绍了获取视频的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到如何获得一个文件的视频长度编程一个简单的例子。很多人说,哦,使用这个库/包装或什么的,但不要说怎么样。我已经下载的ffmpeg,但不知道如何实际使用它,似乎没有要如何使用它来获取视频的持续时间的任何实例。我看你如何使用它来转换视频,但我只是只是想知道视频的持续时间。所有的其他信息也没关系。

I am having trouble finding a simple example of how to get the video length of a file programmatically. Many people say, oh use this library/wrapper or whatever, but do not say how. I have downloaded ffmpeg, but have no clue how to actually use it and there does not seem to be any example of how to use it to get the video duration. I see how you can use it to convert videos, but I simply just want to know the duration of a video. All of the other information does not matter.

是否有这样做简单,无论是在C#,Python和Java的,不管,这将只返回一个字符串,指示视频文件的长度的任何方式。

Is there any way of doing this simply, whether it be in C#, python, java, whatever, that will just return a string that indicates the length of a video file.

如果可能,请提供范例。在此先感谢!

Please provide examples if possible. Thanks in advance!

假设标准的文件格式,如WMV,AVI,MP4,MPEG。东西有元数据。

Assume standard file formats, such as wmv, avi, mp4, mpeg. Stuff that has metadata.

推荐答案

下面是一个例子:

using DirectShowLib;
using DirectShowLib.DES;
using System.Runtime.InteropServices;

...

var mediaDet = (IMediaDet)new MediaDet();
DsError.ThrowExceptionForHR(mediaDet.put_Filename(FileName));

// find the video stream in the file
int index;
var type = Guid.Empty;
for (index = 0; index < 1000 && type != MediaType.Video; index++)
{
    mediaDet.put_CurrentStream(index);
    mediaDet.get_StreamType(out type);
}

// retrieve some measurements from the video
double frameRate;
mediaDet.get_FrameRate(out frameRate);

var mediaType = new AMMediaType();
mediaDet.get_StreamMediaType(mediaType);
var videoInfo = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.formatPtr, typeof(VideoInfoHeader));
DsUtils.FreeAMMediaType(mediaType);
var width = videoInfo.BmiHeader.Width;
var height = videoInfo.BmiHeader.Height;

double mediaLength;
mediaDet.get_StreamLength(out mediaLength);
var frameCount = (int)(frameRate * mediaLength);
var duration = frameCount / frameRate;

这篇关于获取视频的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆