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

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

问题描述

我无法找到有关如何以编程方式获取文件视频长度的简单示例.很多人说,哦使用这个库/包装器或其他什么,但没有说如何.我已经下载了 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天全站免登陆