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)
nek*_*kno 10
这个回答有关的P/Invoke的SHELL32提醒我的Windows API的代码包来访问常用的Windows Vista/7/2008/2008R2的API.
使用包含的示例中的PropertyEdit演示,可以非常轻松地找出Shell32 API以获取各种媒体文件属性,例如持续时间.
我假设同样的先决条件适用于安装正确的解复用器,但它非常简单,因为它只需要添加对Microsoft.WindowsAPICodePack.dll和Microsoft.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)
我认为您正在寻找 FFMPEG - https://ffmpeg.org/
还有一些免费的替代品,您可以在这个问题中阅读有关它们的信息 -在 .net 中使用 FFmpeg?
Run Code Online (Sandbox Code Playgroud)FFMpeg.NET FFMpeg-Sharp FFLib.NET
您可以查看此链接,了解使用 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)