【发布时间】:2012-04-28 18:33:48
【问题描述】:
好的。实际上,我主要需要 mp4 格式。但是,如果也有可能获得其他类型,那就太好了。我只需要读取文件的持续时间。我如何使用 C# 4.0 做到这一点?
所以我需要的就是这个视频:13 minutes 12 seconds
我也可以使用 3 个第三方 exe。就像他们将有关文件的信息保存到文本文件一样。我可以解析那个文本文件。
谢谢。
【问题讨论】:
标签: c# .net video mp4 duration
好的。实际上,我主要需要 mp4 格式。但是,如果也有可能获得其他类型,那就太好了。我只需要读取文件的持续时间。我如何使用 C# 4.0 做到这一点?
所以我需要的就是这个视频:13 minutes 12 seconds
我也可以使用 3 个第三方 exe。就像他们将有关文件的信息保存到文本文件一样。我可以解析那个文本文件。
谢谢。
【问题讨论】:
标签: c# .net video mp4 duration
我遇到了同样的问题,我们为 ffprobe Alturos.VideoInfo 构建了一个包装器。
您只需安装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;
【讨论】:
我认为您正在寻找 FFMPEG - https://ffmpeg.org/
还有一些免费的替代品,您可以在这个问题中了解它们 - 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]));
}
}
}
【讨论】:
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 (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);
}
}
【讨论】:
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);
【讨论】:
也使用 Windows Media Player 组件,我们可以获得视频的时长。
以下代码 sn-p 可以帮助你们:
using WMPLib;
// ...
var player = new WindowsMediaPlayer();
var clip = player.newMedia(filePath);
Console.WriteLine(TimeSpan.FromSeconds(clip.duration));
并且不要忘记添加
wmp.dll的引用,这将是 存在于System32文件夹中。
【讨论】:
我发现NReco.VideoInfo library 是最好的选择,而且比上面的一些更简单。给库一个文件路径并吐出元数据很简单:
var ffProbe = new FFProbe();
var videoInfo = ffProbe.GetMediaInfo(blob.Uri.AbsoluteUri);
return videoInfo.Duration.TotalMilliseconds;
【讨论】:
您可以通过 DirectShow.NET 包装库使用 DirectShow API MediaDet 对象。有关代码示例,请参见 Getting length of video,get_StreamLength 以秒为单位获取持续时间。这假设 Windows 已安装 MPEG-4 解复用器(需要 Windows 7 之前的第三方组件,我相信这同样适用于另一个 answer by cezor,尽管可以免费重新分发组件)。
【讨论】:
MP4 文件的问题是您需要安装解复用器。由于它是专利技术,这给生活带来了麻烦——默认情况下没有人。 gdcl.co.uk/mpeg4 有一个很好的可安装/可再分发选项@您只需要弄清楚如何重新分发和安装它。
regsvr32 它们 - 这应该足够了。
FFMPEG 项目有一个名为 ffprobe 的工具,它可以为您提供多媒体文件所需的信息,并以格式良好的 JSON 格式输出信息。
以this answer 为例。
【讨论】:
您也可以使用 windows 媒体播放器,尽管它不支持您请求的所有文件类型
using WMPLib;
public Double Duration(String file)
{
WindowsMediaPlayer wmp = new WindowsMediaPlayerClass();
IWMPMedia mediainfo = wmp.newMedia(file);
return mediainfo.duration;
}
}
【讨论】:
【讨论】: