代码之家  ›  专栏  ›  技术社区  ›  Anirudha Gupta

如何在C语言中从ffmpeg进程中获取输出#

  •  0
  • Anirudha Gupta  · 技术社区  · 6 年前

    在我用wpf编写的代码中,我在ffmpeg中运行一些过滤器,如果我在终端(powershell或cmd提示符)中运行该命令,它将一行一行地告诉我发生了什么。

    我从C代码调用这个过程,它工作得很好。我的代码存在的问题是,实际上我无法从运行的进程中获得任何输出。

    我尝试了StackOverflow对ffmpeg过程的一些回答。我在代码中看到2个机会。我可以通过计时器方法修复它,或者第二次挂接事件以输出接收到的数据。

    我尝试了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.
    
        }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   steve16351    6 年前

    ffmpeg似乎输出标准错误的状态更新,而不是标准输出。

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

    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);
    }