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

后端 未结 10 710
深忆病人
深忆病人 2020-11-30 07:51

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 ca

10条回答
  •  孤街浪徒
    2020-11-30 08:23

    I think you are looking for FFMPEG - https://ffmpeg.org/

    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]));
                    }
                }
           }
    

提交回复
热议问题