好的。其实我最需要的是MP4格式。但是,如果有可能获得其他类型,那也不错。我只需要阅读文件的持续时间。我如何用C#4.0来做到这一点?如何从mp4,wmv,flv,mov视频获取视频时长
所以我需要的东西是这样的视频是这样的:13 minutes 12 seconds
我可以使用3个第三方前男友了。就像他们将有关文件的信息保存到文本文件一样。我可以解析该文本文件。
谢谢。
好的。其实我最需要的是MP4格式。但是,如果有可能获得其他类型,那也不错。我只需要阅读文件的持续时间。我如何用C#4.0来做到这一点?如何从mp4,wmv,flv,mov视频获取视频时长
所以我需要的东西是这样的视频是这样的:13 minutes 12 seconds
我可以使用3个第三方前男友了。就像他们将有关文件的信息保存到文本文件一样。我可以解析该文本文件。
谢谢。
您可以使用DirectShow的API MediaDet
对象,通过DirectShow.NET包装库。代码示例请参阅Getting length of video,get_StreamLength
以秒为单位给出您的持续时间。这假定Windows安装了MPEG-4解复用器(需要第三方组件,Windows之前7,我相信这同样适用于另一个answer by cezor,虽然有免费重新分配组件)。
你好。我无法添加mediainfodll作为参考。你知道为什么吗 ?这里的错误消息:http://img707.imageshack.us/img707/8130/hataow.png – MonsterMMORPG 2012-04-17 12:27:38
@MonsterMMORPG:你必须用params调用应用程序并获得输出,不能在你的解决方案中用它作为参考 – Marco 2012-04-17 12:41:55
我认为你正在寻找FFMPEG - http://www.ffmpeg-csharp.com/
也有,你可以在这个问题上读到过一些免费的替代品 - Using FFmpeg in .net?
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]));
}
}
}
这是一个支付有申请。他们只提供试用。这对我不起作用:( – MonsterMMORPG 2012-04-17 12:17:55
@MonsterMMORPG看看我的编辑.. – 2012-04-17 12:27:28
谢谢,但正如我所说的,他们只提供试用版。 – MonsterMMORPG 2012-04-17 12:28:43
你也可以使用Windows媒体播放器,虽然它不支持全部的文件类型,你要求
using WMPLib;
public Double Duration(String file)
{
WindowsMediaPlayer wmp = new WindowsMediaPlayerClass();
IWMPMedia mediainfo = wmp.newMedia(file);
return mediainfo.duration;
}
}
谢谢,但该应用程序将在Windows Server 2008上工作。我想他们没有Windows媒体播放器。 – MonsterMMORPG 2012-04-17 12:27:58
@MonsterMMORPG看起来你是对的http://stackoverflow.com/a/13929540/188926 – Dunc 2015-09-24 09:05:40
FFMPEG项目有一个名为ffprobe的工具,它可以为您提供所需的关于多媒体文件的信息,并将信息输出到一个很好的格式化的JSON中。
查看this answer的示例。
This answer关于P/Invoke for Shell32使我想起Windows API Code Pack来访问常见的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 (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);
}
}
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
非常整洁,但持续时间分辨率是秒 - 我也需要分数。请注意,任何尝试此操作的人,都需要将NuGet引用添加到Windows7APICodePack-Core/Shell。 – 2017-01-18 16:20:15
非常感谢您的回答! – 2017-01-31 13:39:14
爱它:)我在这一点上关于CodePack。请注意,该属性的原始值似乎为10 -7秒......我不知道是否为所有文件类型提供了实际的详细信息,但您可以查找它。原始值位于属性“.ValueAsObject”中,并且您需要从一个通用对象中删除这个值。我将编辑答案以尝试添加此内容。 – 2018-01-29 02:51:09
我发现NReco.VideoInfo library是最好的选择,远比一些上述的简单。这是一个简单的给库中的文件路径和它吐出来的是元数据:
var ffProbe = new FFProbe();
var videoInfo = ffProbe.GetMediaInfo(blob.Uri.AbsoluteUri);
return videoInfo.Duration.TotalMilliseconds;
使用Windows Media Player组件也,我们可以得到视频的持续时间。
下面的代码片段可以帮助你们:
using WMPLib;
// ...
var player = new WindowsMediaPlayer();
var clip = player.newMedia(filePath);
Console.WriteLine(TimeSpan.FromSeconds(clip.duration));
,不要忘记添加的
wmp.dll
的参考,这将是 出现在System32
文件夹。
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);
这个DirectShot API在哪里?你也可以给我这个第三部分的网址吗?或者你的意思是像k lite mega编解码器包? – MonsterMMORPG 2012-04-17 12:37:10
好吧试了一个文件,它显示了持续时间NAN:D – MonsterMMORPG 2012-04-17 12:42:00
感谢这一行给出了正确的持续时间秒数:mediaDet.get_StreamLength(out mediaLength); – MonsterMMORPG 2012-04-17 12:47:23