如何在C#WPF应用程序中将FFMPEG转换进度转换为进度条?

时间:2018-03-19 12:21:32

标签: c# ffmpeg output progress-bar

所以我使用下面的代码转换指定的文件,运行ffmpeg,我需要在进度条中看到进度,所以我需要让它在我的文本框中显示cmd输出实时,并从那里得到它由进度条显示,任何帮助?

 private void convertbutton_Click(object sender, RoutedEventArgs e)
    {
        string resdir = AppDomain.CurrentDomain.BaseDirectory + "\\res";
        Extract("ADC", AppDomain.CurrentDomain.BaseDirectory + "\\res", "res", "ffmpeg.exe");

        string ffdir = AppDomain.CurrentDomain.BaseDirectory + "\\res\\ffmpeg.exe";
        string arg =  @"-y -activation_bytes ";
        string arg1 = @" -i ";
        string arg2 = @" -ab 80k -vn ";
        string abytes = bytebox.Text;
        string arguments = arg + abytes + arg1 + openFileDialog1.FileName + arg2 + saveFileDialog1.FileName;

        Process ffm = new Process();
        ffm.StartInfo.FileName = ffdir;
        ffm.StartInfo.Arguments = arguments;
        ffm.StartInfo.CreateNoWindow = true;
        ffm.StartInfo.RedirectStandardOutput = true;
        ffm.StartInfo.RedirectStandardError = true;
        ffm.StartInfo.UseShellExecute = false;
        ffm.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory();
        ffm.Start();

        ffm.WaitForExit();
        ffm.Close();

        Directory.Delete(resdir, true);
    }

FFMPEG输出常规看起来像这样:

size=    4824kB time=00:08:13.63 bitrate=  80.1kbits/s speed=  33x

1 个答案:

答案 0 :(得分:0)

您没有显示进度查看器的代码,但这是获取ffmpeg.exe和(仅相关部分)输出的方式:

Process ffm = new Process()
{
    RedirectStandardError = true,
    RedirectStandardOutput = true
};

ffm.OutputDataReceived += this.HandleOutputData;
ffm.ErrorDataReceived += this.HandleErrorData;

在这些方法中,您可以处理对TextBox和查看者的写作:

private void HandleOutputData(object sender, DataReceivedEventArgs e)
{
    // The new data is contained in e.Data
    MyProgressViewer.Update(e.Data);
    this.myTextBox.Text += e.Data;
}

private void HandleErrorData(object sender, DataReceivedEventArgs e)
{
    // The new data is contained in e.Data
    MyProgressViewer.Update(e.Data);
    this.myTextBox.Text += e.Data;

    // Additional error handling here.
}

至于解析:显然,

  

ffmpeg现在有一个进度选项,可以更容易地解析输出。

有关详细信息,请参阅this answer