好的,实际上我主要需要mp4格式。但如果能够获取其他类型的格式也可以。我只需要读取文件的时长信息。如何使用C# 4.0实现?
所以我需要的东西就像这个视频: 13 分钟 12 秒
我还可以使用三个第三方exe工具,它们可以将文件信息保存到文本文件中。我可以解析那个文本文件。
谢谢。
这个答案关于P/Invoke访问Shell32让我想起了Windows API Code Pack,可以访问常见的Windows Vista/7/2008/2008R2 APIs。
使用示例中包含的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);
}
一些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
如果你正在寻找可用的元数据,那么遍历所有属性非常容易:
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);
}
}
你也可以使用Windows Media Player,不过它并不支持你请求的所有文件类型。
using WMPLib;
public Double Duration(String file)
{
WindowsMediaPlayer wmp = new WindowsMediaPlayerClass();
IWMPMedia mediainfo = wmp.newMedia(file);
return mediainfo.duration;
}
}
MediaDet
对象,通过DirectShow.NET包装库。查看获取视频长度的代码示例,get_StreamLength
将以秒为单位返回持续时间。这假定Windows已安装MPEG-4分离器(在 Windows 7 之前需要第三方组件,我认为对于cezor的另一个答案也适用,虽然有免费的可重新分发的组件)。我认为您正在寻找FFMPEG - https://ffmpeg.org/
还有一些免费的替代方案,您可以在这个问题中了解它们 - 在.NET中使用FFmpeg?
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]));
}
}
}
using WMPLib;
// ...
var player = new WindowsMediaPlayer();
var clip = player.newMedia(filePath);
Console.WriteLine(TimeSpan.FromSeconds(clip.duration));
wmp.dll
的引用,它将存在于System32
文件夹中。我发现NReco.VideoInfo库是最好的选择,比上述库简单得多。只需给该库一个文件路径,它就会输出元数据:
var ffProbe = new FFProbe();
var videoInfo = ffProbe.GetMediaInfo(blob.Uri.AbsoluteUri);
return videoInfo.Duration.TotalMilliseconds;
nuget
软件包即可使用它。同时需要ffprobe二进制文件。PM> install-package Alturos.VideoInfo
例子
var videoFilePath = "myVideo.mp4";
var videoAnalyer = new VideoAnalyzer("ffprobe.exe");
var analyzeResult = videoAnalyer.GetVideoInfo(videoFilePath);
var duration = analyzeResult.VideoInfo.Format.Duration;
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);