如何在C#中从ffmpeg进程获取输出 [英] How to get output from ffmpeg process in c#

查看:103
本文介绍了如何在C#中从ffmpeg进程获取输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中编写的代码中,我在FFmpeg中运行了一些过滤器,如果我在终端中运行命令(PowerShell或cmd提示符),它将逐行为我提供信息.

In the code I written in WPF, I run some filter in FFmpeg, If I run the command in terminal (PowerShell or cmd prompt) It will give me information line by line what's going on.

我正在从C#代码中调用该过程,并且工作正常.我的代码存在的问题实际上是我无法从运行的进程中获取任何输出.

I am calling the process from C# code and it's work fine. The problem I have with my code is actually I am not able to get any output from the process I run.

我从FFmpeg进程的StackOverflow中尝试了一些答案.我在代码中看到了2个机会.我可以通过Timer方法修复它,或者再将事件挂接到OutputDataReceived.

I have tried some answers from StackOverflow for FFmpeg process. I see 2 opportunities in my code. I can either fix it by Timer approach or secondly hook an event to OutputDataReceived.

我尝试了OutputDataReceived事件,但我的代码无法正常工作.我尝试了Timer Approach,但是仍然没有达到我的代码.请检查下面的代码

I tried OutputDataReceived event, My code never got it worked. I tried Timer Approach but still, it's not hitting my code. Please check the code below

        _process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = ffmpeg,
                Arguments = arguments,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true,
            },
            EnableRaisingEvents = true
        };

        _process.OutputDataReceived += Proc_OutputDataReceived;

        _process.Exited += (a, b) =>
        {
            System.Threading.Tasks.Task.Run(() =>
            {
                System.Threading.Tasks.Task.Delay(5000);
                System.IO.File.Delete(newName);
            });

            //System.IO.File.Delete()
        };

        _process.Start();
        _timer = new Timer();
        _timer.Interval = 500;
        _timer.Start();
        _timer.Tick += Timer_Tick;
    }


    private void Timer_Tick(object sender, EventArgs e)
    {
        while (_process.StandardOutput.EndOfStream)
        {
           string line = _process.StandardOutput.ReadLine();
        }
        // Check the process.

    }

推荐答案

ffmpeg似乎在StandardError而不是StandardOutput上输出状态更新.

ffmpeg seems to output status updates on StandardError rather than StandardOutput.

我设法使用以下代码从中获取更新:

I managed to get updates from it using the following code:

process = new Process
{
    StartInfo = new ProcessStartInfo
    {
        FileName = ffmpeg,
        Arguments = args,
        UseShellExecute = false,
        RedirectStandardOutput = true,                    
        CreateNoWindow = false,
        RedirectStandardError = true
    },
    EnableRaisingEvents = true
};

process.Start();

string processOutput = null;
while ((processOutput = process.StandardError.ReadLine()) != null)
{
    // do something with processOutput
    Debug.WriteLine(processOutput);
}     

这篇关于如何在C#中从ffmpeg进程获取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆