.net实现调用cmd命令

public void runCmdTranscoding()
        {
            DateTime start = DateTime.Now;
            try
            {
                string strCMD = @"ffmpeg -i C:\Users\Dell\Desktop\b\b.mp4 -profile:v High -level 5.0 -start_number 0 -hls_time 10 -hls_list_size 0 -f hls C:\Users\Dell\Desktop\b\b.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;

                string str = ("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);
                Response.Write(str);

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message + "\r\n跟踪;" + ex.StackTrace);
            }
        }

Comments