How to get video duration from mp4, wmv, flv, mov

2019-01-07 18:21发布

Alright. Actually i need mostly the mp4 format. But if it is possible to get for other types as well that would be nice. I just need to read the duration of the file. How can i do that with C# 4.0 ?

So the thing i need is like this video is like : 13 minutes 12 seconds

I can use 3 third party exes too. Like they save the information about the file to a text file. I can parse that text file.

Thank you.

9条回答
太酷不给撩
2楼-- · 2019-01-07 18:36

I think you are looking for FFMPEG - http://www.ffmpeg-csharp.com/

there are also some free alternatives that you can read about them in this question - Using FFmpeg in .net?

   FFMpeg.NET
   FFMpeg-Sharp
   FFLib.NET

you can see this link for examples of using FFMPEG and finding the duration - http://jasonjano.wordpress.com/2010/02/09/a-simple-c-wrapper-for-ffmpeg/

        public VideoFile GetVideoInfo(string inputPath)
        {
            VideoFile vf = null;
            try
            {
                vf = new VideoFile(inputPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            GetVideoInfo(vf);
            return vf;
        }
        public void GetVideoInfo(VideoFile input)
        {
            //set up the parameters for video info
            string Params = string.Format("-i {0}", input.Path);
            string output = RunProcess(Params);
            input.RawInfo = output;

            //get duration
            Regex re = new Regex("[D|d]uration:.((\\d|:|\\.)*)");
            Match m = re.Match(input.RawInfo);

            if (m.Success)
            {
                string duration = m.Groups[1].Value;
                string[] timepieces = duration.Split(new char[] { ':', '.' });
                if (timepieces.Length == 4)
                {
                    input.Duration = new TimeSpan(0, Convert.ToInt16(timepieces[0]), Convert.ToInt16(timepieces[1]), Convert.ToInt16(timepieces[2]), Convert.ToInt16(timepieces[3]));
                }
            }
       }
查看更多
爷的心禁止访问
3楼-- · 2019-01-07 18:39

Using Windows Media Player Component also, we can get the duration of the video.
Following code snippet may help you guys :

using WMPLib;
// ...
var player = new WindowsMediaPlayer();
var clip = player.newMedia(filePath);
Console.WriteLine(TimeSpan.FromSeconds(clip.duration));

and don't forget to add the reference of wmp.dll which will be present in System32 folder.

查看更多
beautiful°
4楼-- · 2019-01-07 18:42

IMHO you could use MediaInfo which gives you a lot of information about media files.
There is a CLI for it so you can use it from your code and get info you need.
You can take a look at this link.

查看更多
聊天终结者
5楼-- · 2019-01-07 18:43
StreamReader errorreader;
string InterviewID = txtToolsInterviewID.Text;

Process ffmpeg = new Process();

ffmpeg.StartInfo.UseShellExecute = false;
ffmpeg.StartInfo.ErrorDialog = false;
ffmpeg.StartInfo.RedirectStandardError = true;

ffmpeg.StartInfo.FileName = Server.MapPath("ffmpeg.exe");
ffmpeg.StartInfo.Arguments = "-i " + Server.MapPath("videos") + "\\226.flv";


ffmpeg.Start();

errorreader = ffmpeg.StandardError;

ffmpeg.WaitForExit();

string result = errorreader.ReadToEnd();

string duration = result.Substring(result.IndexOf("Duration: ") + ("Duration: ").Length, ("00:00:00.00").Length);
查看更多
贪生不怕死
6楼-- · 2019-01-07 18:44

FFMPEG project has a tool, called ffprobe which can provide you the information you need about your multimedia files and ouput the information in a nicely formated JSON.

Take a look at this answer for an example.

查看更多
看我几分像从前
7楼-- · 2019-01-07 18:47

I found the NReco.VideoInfo library to be the best option and far simpler than some of those above. It's a simple as giving the library a file path and it spits out the metadata:

var ffProbe = new FFProbe();
var videoInfo = ffProbe.GetMediaInfo(blob.Uri.AbsoluteUri);
return videoInfo.Duration.TotalMilliseconds;
查看更多
登录 后发表回答