Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting length of video

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.

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!

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

like image 881
MZimmerman6 Avatar asked Jun 02 '11 13:06

MZimmerman6


People also ask

How do I find the duration of a video element?

video elements have a duration property which represents the number of seconds in the video. To display the duration in a pretty fashion, you'll need to use parseInt and modulus ( % ): // Assume "video" is the video node var i = setInterval(function() { if(video. readyState > 0) { var minutes = parseInt(video.

How do I find the length of a video in HTML?

The duration property returns the length of the current audio/video, in seconds. If no audio/video is set, NaN (Not-a-Number) is returned.

How do I get the length of a video in Python?

CAP_PROP_FPS to get() method. Calculate the duration of the video in seconds by dividing frames and fps.


1 Answers

Here is an example:

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;
like image 144
nZeus Avatar answered Nov 15 '22 18:11

nZeus