详解C#如何实现分割视频
作者:Csharp小记 时间:2022-12-07 10:48:23
文章描述
在前面两篇写完了对于GIF动态图片的分割和合成,这一篇来写下将视频文件分割成一帧帧图片的方法。
开发环境
.NET Framework版本:4.5
开发工具
Visual Studio 2013
实现代码
public static void Run(string cmd)
{
try
{
string ffmpeg = AppDomain.CurrentDomain.BaseDirectory + "ffmpeg.exe";
ProcessStartInfo startInfo = new ProcessStartInfo(ffmpeg);
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = cmd;
Process process = Process.Start(startInfo);
process.WaitForExit(5000);
process.Kill();
}
catch { }
}
/// <summary>
/// 分割视频
/// </summary>
/// <param name="videoPath">视频路径</param>
/// <param name="outPath">输出图片路径</param>
public static void Split(string videoPath, string outPath)
{
Run(string.Format(" -i {0} -r 10 -y -f image2 -ss 00:00:01 {1}\\%d.jpg", videoPath, outPath));
}
/// <summary>
/// 按时间获取某帧图片
/// </summary>
/// <param name="videoPath">视频路径</param>
/// <param name="outPath">输出图片路径</param>
/// <param name="frameTime">时间(格式:00:00:01)</param>
public static void GetFrame(string videoPath, string outPath, string frameTime)
{
Run(string.Format("-ss 00:00:01 -i {1} {2}", frameTime, videoPath, outPath));
}
private void btn_select_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "视频|*.mp4;*.avi";
ofd.Title = "请选择视频文件";
ofd.InitialDirectory = Application.StartupPath;
if (ofd.ShowDialog() == DialogResult.OK)
{
axWindowsMediaPlayer1.URL = ofd.FileName;
string outPath = Application.StartupPath + "\\cover.jpg";
FFmpegUtil.GetFrame(ofd.FileName, outPath, "00:00:01");
pictureBox1.Image = Image.FromFile(outPath);
}
}
private void btn_split_Click(object sender, EventArgs e)
{
if(!File.Exists(axWindowsMediaPlayer1.URL)){
MessageBox.Show("未选择视频");
return;
}
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "选择存储位置";
fbd.ShowNewFolderButton = true;
if (fbd.ShowDialog() == DialogResult.OK)
{
string[] files = Directory.GetFiles(fbd.SelectedPath);
foreach (string file in files)
{
File.Delete(file);
}
FFmpegUtil.Split(axWindowsMediaPlayer1.URL, fbd.SelectedPath);
if (MessageBox.Show("视频分割完成,是否打开文件夹?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Process.Start(fbd.SelectedPath);
}
}
}
实现效果
代码解析:视频分割技术主要是利用了FFMpeg来实现的,C#在这里其实只是一个调用者。这个在方法Run
中可以看的出来,首先是需要将FFMpeg放到debug目录下的,然后使用Process类来调用;这里有个问题需要注意下,就是调用后经常会被卡住,没办法退出来,所以使用了WaitForExit(2000)
来处理。并且在等待结束后将该进程给Kill掉了,这个方式可能不太规范,如有更好的方法,感谢指教。
调用的话就直接输入命令就可以了,代码中分别使用了以下两条命令:
获取第一秒的图片作为封面图显示到了图片控件中
将视频分割成一张张图片并保存到了文件夹中
来源:https://mp.weixin.qq.com/s/goB1FHQ-FRRhRYRjOkOZTw
标签:C#,分割,视频
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot实现WebSocket即时通讯的示例代码
2022-06-14 19:59:36
![](https://img.aspxhome.com/file/2023/8/78868_0s.jpg)
身份证号码验证算法深入研究和Java实现
2023-05-18 08:17:42
C# XML字符串包含特殊字符的处理转换方法小结
2022-08-17 07:21:24
![](https://img.aspxhome.com/file/2023/3/96363_0s.jpg)
Jenkins使用Gradle编译Android项目详解
2021-12-30 22:26:30
![](https://img.aspxhome.com/file/2023/9/83059_0s.png)
SeekBar拖动条的应用实例
2022-01-28 05:33:34
![](https://img.aspxhome.com/file/2023/2/106882_0s.jpg)
使用C#获取远程图片 Form用户名与密码Authorization认证的实现
2022-01-22 21:44:06
Java锁之可重入锁介绍
2021-06-01 03:05:19
Android ViewPager导航小圆点实现无限循环效果
2022-07-09 13:10:33
优化SimpleAdapter适配器加载效率的方法
2022-03-10 20:33:32
C#防SQL注入代码的三种方法
2021-06-07 05:15:14
Android编程实现分页加载ListView功能示例
2023-11-13 06:27:27
Spring bean配置单例或多例模式方式
2023-01-18 04:03:57
![](https://img.aspxhome.com/file/2023/3/63643_0s.jpg)
android使用SwipeRefreshLayout实现ListView下拉刷新上拉加载
2022-07-15 08:39:50
![](https://img.aspxhome.com/file/2023/7/137567_0s.gif)
将文件夹下所有文件输出到日志文件中 c#递归算法学习示例
2023-04-08 20:36:22
java实现文件归档和还原
2023-02-28 23:09:51
![](https://img.aspxhome.com/file/2023/7/102127_0s.jpg)
Android 3D旋转动画效果实现分解
2022-11-14 17:53:00
![](https://img.aspxhome.com/file/2023/8/129028_0s.gif)
Android实现点击图片上传SQLite数据库
2021-06-23 04:35:50
spring boot加载第三方jar包的配置文件的方法
2023-03-02 22:45:13
Android编程实现获取新浪天气预报数据的方法
2022-08-15 22:14:44
Java的方法和this关键字如何理解与应用
2023-02-14 02:43:29
![](https://img.aspxhome.com/file/2023/7/121947_0s.png)