c# 合并ts文件 批量合并ts文件


        /// <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(); }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM