如何从mp4,wmv,flv,mov视频获取视频时长

Mon*_*RPG 21 .net c# video mp4 duration

好的.其实我主要需要的是mp4格式.但是,如果有可能获得其他类型,那将是不错的.我只需要读取文件的持续时间.我怎么能用C#4.0做到这一点?

所以我需要的是这个视频就像: 13 minutes 12 seconds

我也可以使用3个第三方的前任.就像他们将有关文件的信息保存到文本文件中一样.我可以解析该文本文件.

谢谢.

nic*_*sen 12

您也可以使用Windows Media Player,尽管它不支持您请求的alle文件类型

using WMPLib;

public Double Duration(String file)
    {
        WindowsMediaPlayer wmp = new WindowsMediaPlayerClass();
        IWMPMedia mediainfo = wmp.newMedia(file);
        return mediainfo.duration;
    }
}
Run Code Online (Sandbox Code Playgroud)


Rom*_* R. 12

您可以MediaDet通过DirectShow.NET包装库使用DirectShow API 对象.请参阅获取代码示例的视频长度,get_StreamLength以秒为单位获取持续时间.这假设Windows安装了MPEG-4解复用器(需要第三方组件与7之前的Windows,我相信这同样适用于cezor的另一个答案,尽管可以自由地重新分配组件).


nek*_*kno 10

这个回答有关的P/Invoke的SHELL32提醒我的Windows API的代码包来访问常用的Windows Vista/7/2008/2008R2的API.

使用包含的示例中的PropertyEdit演示,可以非常轻松地找出Shell32 API以获取各种媒体文件属性,例如持续时间.

我假设同样的先决条件适用于安装正确的解复用器,但它非常简单,因为它只需要添加对Microsoft.WindowsAPICodePack.dllMicrosoft.WindowsAPICodePack.Shell.dll和以下代码的引用:

using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;

using (ShellObject shell = ShellObject.FromParsingName(filePath))
{
    // alternatively: shell.Properties.GetProperty("System.Media.Duration");
    IShellProperty prop = shell.Properties.System.Media.Duration; 
    // Duration will be formatted as 00:44:08
    string duration = prop.FormatForDisplay(PropertyDescriptionFormatOptions.None);
}
Run Code Online (Sandbox Code Playgroud)

其他的东西

MPEG-4/AAC音频媒体文件的一些常见属性:

System.Audio.Format = {00001610-0000-0010-8000-00AA00389B71}
System.Media.Duration = 00:44:08
System.Audio.EncodingBitrate = ?56kbps
System.Audio.SampleRate = ?32 kHz
System.Audio.SampleSize = ?16 bit
System.Audio.ChannelCount = 2 (stereo)
System.Audio.StreamNumber = 1
System.DRM.IsProtected = No
System.KindText = Music
System.Kind = Music
Run Code Online (Sandbox Code Playgroud)

如果您正在寻找可用的元数据,则可以轻松遍历所有属性:

using (ShellPropertyCollection properties = new ShellPropertyCollection(filePath))
{
    foreach (IShellProperty prop in properties)
    {
        string value = (prop.ValueAsObject == null) ? "" : prop.FormatForDisplay(PropertyDescriptionFormatOptions.None);
        Console.WriteLine("{0} = {1}", prop.CanonicalName, value);
    }
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*rco 5

恕我直言,您可以使用MediaInfo,它为您提供有关媒体文件的大量信息.
它有一个CLI,因此您可以从代码中使用它并获取所需的信息.
你可以看一下这个链接.


Dor*_*hen 5

我认为您正在寻找 FFMPEG - https://ffmpeg.org/

还有一些免费的替代品,您可以在这个问题中阅读有关它们的信息 -在 .net 中使用 FFmpeg?

   FFMpeg.NET
   FFMpeg-Sharp
   FFLib.NET
Run Code Online (Sandbox Code Playgroud)

您可以查看此链接,了解使用 FFMPEG 并查找持续时间的示例 - 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]));
                }
            }
       }
Run Code Online (Sandbox Code Playgroud)