/// <summary> /// 批量合並ts視頻文件 直接調用10以上批量添加會沒有順序 by GJW
///在程序中調用cmd命令打開一個文件,而文件路徑帶有空格,如果直接把路徑傳給cmd,那么cmd就會把路徑空格前面的部分當做是一個參數,空格后當做另一個參數,命令行執行把后邊截掉了,導致程序出錯,
///會彈出了C:\Program 不是內部或外部命令,也不是可運行的程序或批處理文件的錯誤提示。解決方法是把傳入的參數前后添加雙引號 /// </summary> /// <param name="nameList">需要合並的視頻地址集合</param> /// <param name="savePath">保存地址 物理地址</param> /// <param name="fileName">合成的文件名 需要添加拓展名.ts</param> public static void MergeVideoStatr(List<string> nameList, string savePath, string fileName) { //當次進行生成的文件名 存儲合成文件名 List<string> Mergefile = new List<string>(); if (nameList.Count > 10) { int i = 0; foreach (var item in nameList) { //第二次之后合並 需要將合並好的視頻放到最前面 if (Mergefile.Count == 0 && i == 1) Mergefile.Add(Path.Combine(savePath, fileName)); Mergefile.Add(item); //每10個文件合並一次 if (Mergefile.Count == 10) { if (i == 0) MergeVideo(Mergefile, savePath, fileName); else { MergeVideo(Mergefile, savePath, fileName); } //轉換狀態 下次添加新生成的初次合成的完成版文件 i = 1; //清除重新添加 Mergefile.Clear(); } } //清理剩下的文件 if (Mergefile.Count != 0 && i == 1) { MergeVideo(Mergefile, savePath, fileName); } } else { if (nameList.Count == 0) throw new Exception("合並視頻數不能為0"); MergeVideo(nameList, savePath, fileName); } } /// <summary> /// 將某一文件夾下的ts文件 合並為一個完整版 BY GJW /// </summary> /// <param name="nameList">需要合並的視頻地址集合</param> /// <param name="savePath">保存地址</param> /// <param name="fileNmae">合成的文件名</param> public static void MergeVideo(List<string> nameList,string savePath,string fileNmae) { if (nameList.Count == 0 || string.IsNullOrEmpty(savePath)) throw new Exception("文件路徑不能為空"); System.Diagnostics.Process p = new System.Diagnostics.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.Start();//啟動程序 //向cmd窗口發送輸入信息 //拼接命令 //string cmdstr = string.Format(@"copy /b {0}\*.ts {1}\{2}_完整版.ts",videoPath,savePath,fileNmae); string cmdstr = string.Format(@"copy /b ""{0}"" ""{1}\{2}""", string.Join(@"""+""", nameList), savePath, fileNmae); p.StandardInput.WriteLine(cmdstr + "&exit"); p.StandardInput.AutoFlush = true; //獲取cmd窗口的輸出信息 string output = p.StandardOutput.ReadToEnd(); p.WaitForExit();//等待程序執行完退出進程 p.Close(); }