爬虫开发笔记 之显示 ffmpeg 的获取输出信息(异步获取重定向内容)

利用管道技术可以获取其他进程在命令行上的输出,即管道改向。有关内容请参考:
添加链接描述

对于一般的应用,上面的描述没有任何问题,也是正确的。但是对于FFmpeg 这个大咖而言上面的参考代码竟然无效!

直接进入主题:FFmpeg在错误时使用的是 StandardOutput 而正确时,使用竟然是StandardError!不知道自己是否理解有问题。

获取ffmpeg 正确输出的代码如下:

        private void btnTest_Click(object sender, EventArgs e)
        {

            string mergeCmd = "ffmpeg.exe";
            string mergeParm = "-y -i \"concat:K:\\temp\\39349807290087364_0.flv|K:\\temp\\39349807290087364_1.flv\" -i K:\\temp\\39349807290087364_2.mp4 -c:v copy -c:a aac -threads 10 -preset ultrafast -strict experimental MergedVideo.mp4";

            ProcessStartInfo psi;
            psi = new ProcessStartInfo(mergeCmd, mergeParm);
            psi.UseShellExecute = false;
            //psi.RedirectStandardOutput = true;  //允许重定向标准输出

            psi.RedirectStandardError = true;
            psi.CreateNoWindow = true;
            psi.WindowStyle = ProcessWindowStyle.Hidden;

            var p = Process.Start(psi);

            //StreamReader myStreamReader = p.StandardError;
             Read the standard output of the spawned process.
            //string myString = myStreamReader.ReadLine();
            //Console.WriteLine(myString);

            //the following does not work well if the there is no error about the process
            //p.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);
            p.ErrorDataReceived += new DataReceivedEventHandler(OnDataReceived);
            p.BeginErrorReadLine();
            while (!p.HasExited)
            {
                Application.DoEvents();
                txtInfo.Text = m_showInfo;// member variable ..
            }
            //if (p.ExitCode != 0)
            //{
            //    txtInfo.Text += p.StandardError.ReadToEnd();
            //}
            p.Close();
        }

其中:OnDataReceived 代码如下:

private void OnDataReceived(object Sender, DataReceivedEventArgs e)
        {
            if (e.Data != null)
            {
                //txtInfo.Text += e.Data;// will ocur an not the same instance error
                m_showInfo = e.Data;//use a variable to store the infor , 
            }
        }

上面代码工作正常。

马拉孙 2021-03-17 于 泛五地区

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值