首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在C# WPF应用程序中将FFMPEG转换为进度条?

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

Stack Overflow用户
提问于 2018-03-19 12:21:32
回答 1查看 4.5K关注 0票数 1

所以我使用下面的代码通过运行ffmpeg来转换一个指定的文件,我需要进度在一个进度条中是可见的,所以I#d需要让它实时地将cmd输出显示到我的文本框中,然后从那里得到进度条显示,有帮助吗?

代码语言:javascript
复制
 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输出规则如下所示:

代码语言:javascript
复制
size=    4824kB time=00:08:13.63 bitrate=  80.1kbits/s speed=  33x
EN

回答 1

Stack Overflow用户

发布于 2018-03-19 12:43:59

您没有显示进度查看器的代码,但这是如何获得ffmpeg.exe的输出的(只有相关部分):

代码语言:javascript
复制
Process ffm = new Process()
{
    RedirectStandardError = true,
    RedirectStandardOutput = true
};

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

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

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49362864

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档