我已经解决了standardError的重定向并在方法中使用streamreader读取。
public double Progress{get;set;}
创建新进程
Process run = new Process();
run.StartInfo.FileName = Path.Combine(path, "ffmpeg.exe");
run.StartInfo.Arguments ="your string with output options";
run.StartInfo.UseShellExecute = false;
run.StartInfo.CreateNoWindow = true;
run.StartInfo.RedirectStandardError = true;
run.Start();
StreamReader sr = run.StandardError;
while (!sr.EndOfStream)
{
getTotalSecondProcessed(sr.ReadLine());
}
并重定向输出
private void getTotalSecondProcessed(string v)
{
try
{
string[] split = v.Split(' ');
foreach (var row in split)
{
if (row.StartsWith("time="))
{
var time = row.Split('=');
Progress = TimeSpan.Parse(time[1]).TotalSeconds;
}
}
catch{}
}
在 WinForm 中创建一个由 timer_Tick 调用的方法
private void timer1_Tick(object sender, EventArgs e)
{
try
{
int progress = int.Parse(utilityVideo.Progress.ToString("0"));
if (progress > 0)
{
RefreshProgressBar(Progress);
}
}
}
catch { }
}
private void RefreshProgressBar(int currentTimeProcessed)
{
if (InvokeRequired)
{
BeginInvoke((MethodInvoker)delegate { RefreshProgressBar(currentTimeProcessed); });
return;
}
progressBar1.Value = currentTimeProcessed;
}