如何从c#中的ffmpeg进程获取输出

Ani*_*pta 0 c# ffmpeg timer process

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

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

我已经为 FFmpeg 过程尝试了 StackOverflow 的一些答案。我在我的代码中看到了 2 个机会。我可以通过 Timer 方法修复它,也可以将事件挂接到 OutputDataReceived。

我尝试了 OutputDataReceived 事件,我的代码从来没有让它工作。我尝试了计时器方法,但仍然没有命中我的代码。请检查下面的代码

        _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.

    }
Run Code Online (Sandbox Code Playgroud)

ste*_*351 6

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

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

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);
}     
Run Code Online (Sandbox Code Playgroud)