c# 调用 ffmpeg切割MP4视频TS

AddTime 2020/6/10 Hits 2270
namespace qiem3u8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            //ofd.Filter = "MP4文件(*.xls;*.xlsx)|*.xls;*.xlsx|所有文件|*.*";
            ofd.Filter = "MP4文件(*.mp4;*.MP4)|*.mp4;*.MP4";
            ofd.ValidateNames = true;
            ofd.CheckPathExists = true;
            ofd.CheckFileExists = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string strFileName = ofd.FileName;
                txtfilepath.Text = strFileName;
                qiepianfilelujing.Text = "";
            }
        }

        /// <summary>
        /// 执行cmd命令
        /// </summary>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public string ExeCommand(string commandText)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = false;
            p.StartInfo.CreateNoWindow = true;
            string strOutput = null;
            try
            {
                p.Start();
                p.StandardInput.WriteLine(commandText);
                p.StandardInput.WriteLine("exit");
                strOutput = p.StandardOutput.ReadToEnd();
                p.WaitForExit();
                p.Close();

            }
            catch (Exception e)
            {
                strOutput = e.Message;
            }
            return strOutput;
        }

        public void RunCmd(string ffmpegPath,string param, out string output)
        {
            //string CmdPath = @"C:\Windows\System32\cmd.exe";
            //cmd = cmd.Trim().TrimEnd('&') + "&exit";//说明:不管命令是否成功均执行exit命令,否则当调用ReadToEnd()方法时,会处于假死状态
            using (Process p = new Process())
            {
                p.StartInfo.FileName = ffmpegPath;
                p.StartInfo.Arguments = param;
                p.StartInfo.UseShellExecute = false;        //是否使用操作系统shell启动
                //p.StartInfo.RedirectStandardInput = true;   //接受来自调用程序的输入信息
                //p.StartInfo.RedirectStandardOutput = true;  //由调用程序获取输出信息
                //p.StartInfo.RedirectStandardError = true;   //重定向标准错误输出
                p.StartInfo.CreateNoWindow = true;          //不显示程序窗口
                p.Start();//启动程序

                //向cmd窗口写入命令
                //p.StandardInput.WriteLine(cmd);
                //p.StandardInput.AutoFlush = true;
                output = "";
                //获取cmd窗口的输出信息
                //output = p.StandardOutput.ReadToEnd();
                p.WaitForExit();//等待程序执行完退出进程
                p.Close();
            }
        }
    

        private void btnqiepian_Click(object sender, EventArgs e)
        {


            string _qiepianfilelujing = qiepianfilelujing.Text;
            if (string.IsNullOrEmpty(_qiepianfilelujing) == true)
            {
                //开始切片
                btnqiepian.Enabled = false;
                btnqiepian.Text = "处理中";
                string strFileName = txtfilepath.Text;

                string rootpath = System.Windows.Forms.Application.StartupPath;

                string app_path = rootpath + "\\ffmpeg";

                //其他代码
                //txtfilepath.Text = strFileName + "|" + app_path;

                string FFmpegPath = rootpath + "\\ffmpeg.exe";


                //创建目录
                var uuidN = Guid.NewGuid().ToString("N");//创建文件目录
                string outfilemulu = rootpath + "\\outm3u8\\" + uuidN + "\\";
                if (!Directory.Exists(outfilemulu))
                {
                    Directory.CreateDirectory(outfilemulu);//创建目录
                }
                //输出的文件路径
                string outfilepath = outfilemulu + "playlist.m3u8";

                //string str = "";
                //string outputstr = HttpContext.Current.Request.MapPath(_pcm);
                //string outputwav = HttpContext.Current.Request.MapPath(_wav);

                //string pcm = outputstr;
                //string wav = outputwav;

                ////一种转换cheng
                ////string zhuanmatools = "e:\\zm\\ffmpeg.exe  -f s16le -v 8 -y -ar 16000 -ac 1 -i ";
                ////ffmpeg -f s16le -ar 12000 -ac 2 -i e:\zm\web\vd\444.pcm -f wav -ar 16000 -ac 1 e:\zm\web\vd\444.wav

                //string mingling = PubConstant.Pan + ":\\www\\chuliSilk\\ffmpeg\\ffmpeg.exe";

                //str = string.Format("{0} -f s16le -ar 12000 -ac 2 -i {1} -f wav -ar 16000 -ac 1 {2}", mingling, pcm, wav);

                //e://m3u8/ffmpeg -i e://m3u8/1.mp4 -codec copy -vbsf h264_mp4toannexb -map 0 -f segment -segment_list e://m3u8/100001/out.m3u8 -segment_time 10 e://m3u8/100001/out%03d.ts

                //https://blog.csdn.net/cctvcqupt/article/details/80695879?utm_source=blogxgwz4

                string run_str = string.Format("-i {0} -codec copy -vbsf h264_mp4toannexb -map 0 -f segment -segment_list {1} -segment_time 10 {2}/out%03d.ts",
                    strFileName, outfilepath, outfilemulu);

                string outmsg = "";
                RunCmd(FFmpegPath,run_str, out outmsg);

                lblmsg.Text = "切片完成";

                btnqiepian.Enabled = true;
                btnqiepian.Text = "开始切片";

                qiepianfilelujing.Text = outfilepath;
                txtmulu.Text = outfilemulu;
            }
            else
            {
                MessageBox.Show("次文件已切片"); 
            }
        }
    }
}
小珂爱学习(XkWeb)
© 2020 Xkweb.cn. 小珂爱学习 让知识变得有趣 冀ICP备13014724号-2
Designed by:Xkweb3.6