C语言ffmpeg合并多个视频,ffmpeg合并多个视频

这段代码展示了如何遍历指定文件夹获取.mp4视频文件,按照文件名顺序合并视频,并使用ffmpeg工具进行转换和拼接操作。最终将多个视频合并为一个新视频文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

///

///遍历文件夹获取所有视频路径///

///

private void TraverseFolder(string path,stringfilepath)

{

DirectoryInfo dInfo= newDirectoryInfo(path);

Dictionary dic = new Dictionary();

Dictionary dic2 = new Dictionary();

List list = new List();//遍历该文件夹

foreach (FileInfo fileitem indInfo.GetFiles())

{if (fileitem.Extension == ".mp4")

{

dic.Add(fileitem.Name, fileitem.FullName);

}

}

list= dic2.OrderBy(p => p.Key).Select(p => p.Value).ToList();//遍历获取所有需要合并视频的路径(为了保证视频合并后播放的顺序是正确的,所有需要合并的视频命名是有序的)

VideoCombine(list, filepath); //执行视频合并操作

}///

///

///

/// 需要合并视频路径(含文件名和文件类型)集合

/// 合并后文件路径(含文件名和文件类型)

public void VideoCombine(List list, stringDstFile)

{//DstFile=@"E:\新建文件夹\新视频.mp4";

string strTmp = "";string strCmd = "";

StringBuilder sb= newStringBuilder();

sb.Append("-i \"concat:");foreach (var item inlist)

{

strTmp= item + ".ts";

strCmd= "-i" + item + "-c copy -bsf:v h264_mp4toannexb -f mpegts" + strTmp + "-y";

CombineImplement(strCmd);

sb.Append($"{strTmp}|");

}

sb.Remove(sb.ToString().LastIndexOf('|'), 1);

sb.Append($"\" -c copy -bsf:a aac_adtstoasc -movflags +faststart {DstFile} -y");var path =sb.ToString();

CombineImplement(path);

}public void CombineImplement(stringstrCmd)

{string exe = @"C\ffmpeg.exe";//转换文件类型,由于不是所有类型的视频文件都支持直接合并,需要先转换格式

System.Diagnostics.Process p = newSystem.Diagnostics.Process();

p.StartInfo.FileName= exe;//要执行的程序名称

p.StartInfo.Arguments = " " +strCmd;

p.StartInfo.UseShellExecute= false;

p.StartInfo.RedirectStandardInput= false;//可能接受来自调用程序的输入信息

p.StartInfo.RedirectStandardOutput = false;//由调用程序获取输出信息

p.StartInfo.RedirectStandardError = false;//重定向标准错误输出

p.StartInfo.CreateNoWindow = false;//不显示程序窗口

p.Start();//启动程序

p.WaitForExit();

p.Close();

p.Dispose();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值