【问题标题】:How to get FFMPEG conversion progress to progressbar in a C# WPF application?如何在 C# WPF 应用程序中获取 FFMPEG 转换进度到进度条?
【发布时间】:2018-03-19 12:21:32
【问题描述】:

所以我使用下面的代码通过运行 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

【问题讨论】:

  • @Borian 可能是,但我认为这可能是关于如何正确重定向进程输出并获取实际数据。
  • 异步执行并在启动时显示进度条。异步结束后隐藏进度条。
  • stackoverflow.com/questions/11441517/… 有关于从 ffmpeg 解析进度信息的信息

标签: c# ffmpeg output progress-bar


【解决方案1】:

您没有显示进度查看器的代码,但这是您获得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 了解更多详情。

【讨论】:

  • 因为 ffmpeg 只是在 stderr 中输出,所以我猜 stdout 部分没用,但我仍然不明白这将如何将转换进度转换为进度条值,因为 ffmpeg 在转换时具有一定的输出格式
  • @adrifcastr 你是对的,如果它只使用stderr。关于转换:这取决于您;-) 您没有向我们展示格式,例如它会打印“42%”还是“42”等。您需要将值解析为int,然后将其设置为进度条。
  • 等一下,我把它添加到OP中
猜你喜欢
  • 2011-09-18
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 2011-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多