資源簡介
使用ffmpeg.exe獲取文件屬性信息,ffmpeg是java開發的用于多媒體文件編輯的命令行工具,有多個版本,功能比較強大,C#中可以在進程外異步調用這個工具
using (System.Diagnostics.Process pro = new System.Diagnostics.Process())
{
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.ErrorDialog = false;
pro.StartInfo.RedirectStandardError = true;
pro.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory +
"ffmpeg.exe";
pro.StartInfo.Arguments = " -i " + fileName;
pro.Start();
System.IO.StreamReader errorreader = pro.StandardError;
pro.WaitForExit(1000);
string result = errorreader.ReadToEnd();
if (!string.IsNullOrEmpty(result))
{
result = result.Substring(result.IndexOf("Duration: ") +
("Duration: ").Length, ("00:00:00").Length);
duration = result;
}
}

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Text.Regularexpressions;
using?System.IO;
using?System.Reflection;
using?System.Diagnostics;
using?System.Drawing;
namespace?BjSTools.MultiMedia
{
????public?class?FFmpegMediaInfo
????{
????????private?static?string?FFMPEG_EXE_PATH?=?CheckRelativePath(@“ffmpeg\ffmpeg.exe“);
????????private?static?string?FFPROBE_EXE_PATH?=?CheckRelativePath(@“ffmpeg\ffprobe.exe“);
????????#region?static?helpers
????????///?
????????///?Safely?converts?a?string?in?format?h:m:s.f?to?a?TimeSpan?using?Regex?allowing?every?part?being?as?long?as?is
????????///?
????????private?static?TimeSpan?ConvertFFmpegTimeSpan(string?value)
????????{
????????????Match?m?=?_rexTimeSpan.Match(value);
????????????double?v?=?0.0;
????????????if?(m?==?null?||?!m.Success)?return?new?TimeSpan();
????????????if?(!String.IsNullOrEmpty(m.Groups[“h“].Value))
????????????????v?+=?Convert.ToInt32(m.Groups[“h“].Value);
????????????v?*=?60.0;
????????????if?(!String.IsNullOrEmpty(m.Groups[“m“].Value))
????????????????v?+=?Convert.ToInt32(m.Groups[“m“].Value);
????????????v?*=?60.0;
????????????if?(!String.IsNullOrEmpty(m.Groups[“s“].Value))
????????????????v?+=?Convert.ToInt32(m.Groups[“s“].Value);
????????????if?(!String.IsNullOrEmpty(m.Groups[“f“].Value))
????????????????v?+=?Convert.ToDouble(String.Format(“0{1}{0}“?m.Groups[“f“].Value?CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator));
????????????return?TimeSpan.FromSeconds(v);
????????}
????????private?static?readonly?Regex?_rexTimeSpan?=?new?Regex(@“^(((?\d+):)?(?\d+):)?(?\d+)([\.](?\d+))?$“?RegexOptions.Compiled);
????????///?
????????///?Checks?if?the?passed?path?is?rooted?and?if?not?resolves?it?relative?to?the?calling?assembly
????????///?
????????private?static?string?CheckRelativePath(string?path)
????????{
????????????if?(!Path.IsPathRooted(path))
????????????{
????????????????string?appDir?=?Path.GetDirectoryName(Assembly.GetCallingAssembly().GetName().Codebase);
????????????????path?=?Path.Combine(appDir?path);
????????????}
????????????if?(path.StartsWith(“file:“?StringComparison.OrdinalIgnoreCase))
????????????????path?=?path.Substring(6);
????????????return?path;
????????}
????????///?
????????///?Executes?a?process?and?passes?its?command-line?output?back?after?the?process?has?exitted
????????///?
????????private?static?string?Execute(string?exePath?string?parameters)
????????{
????????????string?result?=?String.Empty;
????????????using?(Process?p?=?new?Process())
????????????{
????????????????p.StartInfo.UseShellExecute?=?false;
????????????????p.StartInfo.CreateNoWindow?=?true;
????????????????p.StartInfo.RedirectStandardOutput?=?true;
????????????????p.StartInfo.FileName?=?exePath;
????????????????p.StartInfo.Arguments?=?parameters;
????????????????p.S
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????11654??2014-05-17?05:00??FFmpegMediaInfo.cs
評論
共有 條評論