C#实现视频的批量剪辑功能
作者:xchenbb 时间:2023-07-15 06:18:30
篇首,完全没有技术含量的帖子,高手略过,只为十几年后重新捡起的我爱好玩玩。。。
起因,一个朋友说他下载了很多短视频,但只需要要其中的一小截,去头掐尾,在软件里搞来搞去太麻烦,让我帮忙,我这个编程二吊子爽快的接了下来。
还是一二三理清思路,方案就用ffmpeg,命令行剪辑生成新视频,c#做个集成一键处理。。
一,采用预置数据data.txt,记录【视频文件名,起点时间,终止时间】,此为单独一行,多个文件就多行,如下图
二,一个videocut类
class VideoCut
{
public string file;
public string begin;
public string end;
public VideoCut(string f,string b,string w)
{
file = f;
begin = b;
end = w;
}
}
三,解析数据文件data.txt,生成videocut的列表
count = 0;
listbox.Items.Clear();
logno("开始解析数据文件....");
if (!System.IO.File.Exists("data.txt"))
{
log("找不到数据文件data.txt");
return;
}
List<VideoCut> list = new List<VideoCut>();
string[] ary;
TimeSpan begin;
TimeSpan end;
int i = 0;
foreach (string line in System.IO.File.ReadLines("data.txt"))
{
ary = line.Trim().Split(',');
log("第" + ++i + "行:" + line.Trim());
if(ary.Length!=3)
{
log("数据:"+line.Trim()+",格式不对");
continue;
}
if (!System.IO.File.Exists(ary[0]))
{
log("文件:"+ary[0].Trim()+",不存在");
continue;
}
if (!TimeSpan.TryParse(ary[1].Trim(), out begin))
{
log("起点时间:" + ary[1].Trim() + ",格式不对");
continue;
}
if (!TimeSpan.TryParse(ary[2].Trim(), out end))
{
log("截止时间:" + ary[2].Trim() + ",格式不对");
continue;
}
if (end <= begin)
{
log("截止时间应该大于起点时间!!!!!");
continue;
}
list.Add(new VideoCut(ary[0], ary[1], (end-begin).ToString()));
}
logno("解析数据文件完毕,成功解析文件:"+list.Count+"个...");
if (list.Count < 1)
{
log("没有数据,退出");
}
四,一个ffmpeg的剪辑类
class FFMEPG
{
//视频切割
public static string Cut(string OriginFile/*视频源文件*/, string startTime/*开始时间*/, string endTime/*结束时间*/)
{
string DstFile = OriginFile.Replace(".", "a.");
string strCmd = " -ss "+ startTime
+" -i " + OriginFile
+ " -to " +endTime
+ " -vcodec copy -acodec copy " + DstFile + " -y ";
if (System.IO.File.Exists(DstFile))System.IO.File.Delete(DstFile);
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "ffmpeg.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();//等待程序执行完退出进程
if (System.IO.File.Exists(DstFile))
{
return DstFile;
}
return "";
}
}
五,循环调用videocut列表
VideoCut c;
string file;
for (i = 0; i < list.Count; i++)
{
logno("开始剪切第【" +i + "】个文件...");
c=list[i];
file = FFMEPG.Cut(c.file, c.begin, c.end);
if (file.Length > 0)
{
log("剪切成功,输出文件:"+file);
}
else log("剪切失败.....");
}
log("");
log("");
log("剪切完成......");
六,大致就这样了,运行如下图
ffmpeg命令要能够调用哈,放到同目录或都windows系统目录都行。。。
源代码已经上传,可以下载到。。。
来源:https://blog.csdn.net/xchenbb/article/details/129697765
标签:C#,视频,剪辑
0
投稿
猜你喜欢
PowerManagerService之手动灭屏流程示例分析
2023-01-13 17:11:51
浅谈Spring Data如何简化数据操作的方法
2023-04-19 04:50:08
C/C++根据年月日计算星期几(蔡勒公式篇)
2023-12-05 00:48:16
Java通过反射,如何动态修改注解的某个属性值
2022-08-22 04:10:51
简单掌握Android开发中彩信的发送接收及其附件的处理
2023-04-18 02:32:24
Java查看线程运行状态的方法详解
2023-05-23 12:21:10
使用java实现telnet-client工具分享
2023-10-18 10:56:59
C#中fixed关键字的作用总结
2023-07-17 09:43:44
C#中ListView用法实例
2021-10-15 06:10:24
Spring AOP的底层实现方式-代理模式
2022-10-05 23:27:50
Android的ListView多选删除操作实现代码
2023-06-25 22:24:38
c#利用Excel直接读取数据到DataGridView
2023-02-22 13:39:07
Flutter开发Widgets 之 PageView使用示例
2023-06-24 13:23:34
c#文档图片自动纠偏
2022-08-07 13:45:39
C++ 中const修饰虚函数实例详解
2022-01-06 16:40:47
Android HttpClient GET或者POST请求基本使用方法
2021-11-18 04:26:57
@JsonFormat处理LocalDateTime失效的问题
2023-07-22 18:41:13
详解Android开发中Activity的四种launchMode
2023-05-19 08:30:27
C# 枚举Color并展示各种颜色效果的示例
2023-10-24 21:27:27
Android百度地图实现搜索和定位及自定义图标绘制并点击时弹出泡泡
2023-08-30 21:30:38