///
/// 执行一条command命令
///
/// 需要执行的Command
/// 输出
/// 错误
public static void ExecuteCommand(string command,out string output,out string error)
{
try
{
//创建一个进程
Process pc = new Process();
pc.StartInfo.FileName = "cmd.exe";
pc.StartInfo.UseShellExecute = false;
pc.StartInfo.RedirectStandardInput = true;
pc.StartInfo.RedirectStandardOutput = true;
pc.StartInfo.RedirectStandardError = true;
pc.StartInfo.CreateNoWindow = true;
//启动进程
pc.Start();
//执行命令
pc.StandardInput.WriteLine(command);
//等待退出
pc.StandardInput.WriteLine("exit");
//获取所有输出
output = pc.StandardOutput.ReadToEnd();
error = pc.StandardError.ReadToEnd();
//关闭进程
pc.Close();
}
catch(Exception)
{
output = null;
error = null;
}
}
///
/// 获取视频的帧宽度和帧高度
///
/// mov文件的路径
/// null表示获取宽度或高度失败
public static void GetMovWidthAndHeight(string videoFilePath, out int? width, out int? height)
{
try
{
//判断文件是否存在
if (!File.Exists(videoFilePath))
{
width = null;
height = null;
}
//执行命令获取该文件的一些信息
string ffmpegPath = new FileInfo(Process.GetCurrentProcess().MainModule.FileName).DirectoryName + @"\ffmpeg.exe";
string output;
string error;
Helpers.ExecuteCommand("\"" + ffmpegPath + "\"" + " -i " + "\"" + videoFilePath + "\"",out output,out error);
if(string.IsNullOrEmpty(error))
{
width = null;
height = null;
}
//通过正则表达式获取信息里面的宽度信息
Regex regex = new Regex("(\\d{2,4})x(\\d{2,4})", RegexOptions.Compiled);
Match m = regex.Match(error);
if (m.Success)
{
width = int.Parse(m.Groups[1].Value);
height = int.Parse(m.Groups[2].Value);
}
else
{
width = null;
height = null;
}
}
catch (Exception)
{
width = null;
height = null;
}
}