1. 使用Emgu.CV将tif保存视频,非常简单
打开:
VideoWriter writer = new VideoWriter(name, VideoWriter.Fourcc('M', 'J', 'P', 'G'), displayRate, new Size(width, height), false);
写入
writer.Write(mat);
关闭
writer.Dispose();
完整代码如下:
using Emgu.CV;
using System;
using System.Drawing;public class CVideoFiles{private VideoWriter writer;private bool isOpen = false;// 打开视频文件public void OpenVideo(string name, int displayRate, int width, int height){// 如果视频已经打开,抛出异常if (isOpen){throw new InvalidOperationException("视频文件已经打开,请关闭当前文件后再打开新的文件。");}// 打开视频文件并设置编码器,帧率,分辨率writer = new VideoWriter(name, VideoWriter.Fourcc('M', 'J', 'P', 'G'), displayRate, new Size(width, height), false);// 如果视频文件没有成功打开,抛出异常if (!writer.IsOpened){throw new InvalidOperationException("打开视频文件失败,请检查文件路径或编码器设置。");}isOpen = true;}// 写入一帧视频public void WriteVideo(Mat mat){if (!isOpen){throw new InvalidOperationException("视频尚未打开,无法写入视频帧。");}// 检查视频帧数据是否有效if (mat == null || mat.IsEmpty){throw new ArgumentException("无效的帧数据,无法写入视频。");}// 写入视频帧writer.Write(mat);}// 关闭视频文件public void CloseVideo(){if (isOpen){// 释放 VideoWriter 对象writer.Dispose();isOpen = false;}else{throw new InvalidOperationException("视频文件未打开,无法关闭。");}}}
2. 保存的avi视频,用AxWindowsMediaPlayer打开
给AxWindowsMediaPlayer控制的URL 赋值,就会自动播放,如:
axWindowsMediaPlayer1.URL = "c:\\1.avi";
如果AxWindowsMediaPlayer打不开,提示报错,说明MediaPlayer缺少解码器, 从 Download K-Lite Codec Pack (codecguide.com) 下载K-Lite_Codec_Pack_1865_Standard .exe,安装就可以了。