【转】解决C# 运行cmd.exe执行ffmpeg进行视频转码,程序无法关闭
作者:佚名 发表时间:2019-04-16 18:58:55
转自:https://blog.csdn.net/jing_cs/article/details/79757201
ffmpeg视频转码,转码完成后程序一直卡死且一直未收到exit消息,原因是ffmpeg是StandardError流,要读取StandardError而不是StandardOutput。
    public static void runCmdTranscoding()
    {
        DateTime start = DateTime.Now;
        try
        {
            string strCMD = "ffmpeg -i" + @" G:\RSC\video\002.mp4 -hls_time 10 -hls_list_size 0 -hls_key_info_file" + @" G:\RSC\videokey\video.keyinfo" + @" d:\nginx\html\002.m3u8";
            //创建一个进程
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
            p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            p.StartInfo.CreateNoWindow = true;//不显示程序窗口
            //p.StartInfo.Arguments = strCMD;
            p.Start();//启动程序

            //向cmd窗口发送输入信息
            p.StandardInput.WriteLine(strCMD + "&exit");

            p.StandardInput.AutoFlush = true;
            p.StandardInput.Close();
            //获取cmd窗口的输出信息
            **string output = p.StandardError.ReadToEnd();**
            //等待程序执行完退出进程
            p.WaitForExit();
            p.Close();

            DateTime end = DateTime.Now;

            MessageBox.Show("OK: start=" + start.ToString("yyyy-MM-dd HH:mm:ss:fff") + "   ####   end=" + end.ToString("yyyy-MM-dd HH:mm:ss:fff") + "\n消耗时长为:" + (end - start).TotalSeconds.ToString());
            //Console.WriteLine(output);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "\r\n跟踪;" + ex.StackTrace);
        }
    }