做了个从相机读取RTSP流,并保存成MP4视频的软件,但是保存的视频大小是0KB.Emgu的版本应该不高,3.0.0.****
/// <summary>
/// RTSP视频的工具类
/// </summary>
public class RTSPUtils
{
/// <summary>
/// 传过来的播放视频的组件
/// </summary>
private PictureBox pictureBox;
/// <summary>
/// 相机信息
/// </summary>
private CameraConfig camera;
/// <summary>
/// 用于播放视频的Emgu对象
/// </summary>
private Capture capture;
/// <summary>
/// Emgu 的视频流写
/// </summary>
private VideoWriter videoWriter = null;
/// <summary>
/// 视频尺寸
/// </summary>
private int videoWidth;
private int videoHeight;
/// <summary>
/// 是否在录制
/// </summary>
private bool isRecord =false;
public RTSPUtils(PictureBox pictureBox, CameraConfig camera)
{
this.pictureBox = pictureBox;
this.camera = camera;
}
/// <summary>
/// 打开视频
/// </summary>
/// <returns></returns>
public bool OpenVideo()
{
try
{
capture = new Capture(camera.rtspUrl);
capture.ImageGrabbed += capture_ImageGrabbed;
capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps, 0);
capture.Start();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// ImageGrabbed事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void capture_ImageGrabbed(object sender,EventArgs e)
{
try
{
Mat mat = new Mat();
capture.Retrieve(mat, 0);
pictureBox.Image = mat.Bitmap;
if (isRecord && videoWriter != null)
{
videoWriter.Write(mat);
}
}
catch
{
}
}
/// <summary>
/// 关闭视频
/// </summary>
/// <returns></returns>
public bool CloseVideo()
{
try
{
capture.Stop();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 开始录制
/// </summary>
public bool StartRecord(string file)
{
if (capture != null)
{
videoWidth = capture.Width;
videoHeight = capture.Height;
isRecord = true;
videoWriter = new VideoWriter(file, VideoWriter.Fourcc('M', 'P', '4', 'V'), 30, new System.Drawing.Size(videoWidth, videoHeight), true);
return true;
}
return false;
}
/// <summary>
/// 停止录制
/// </summary>
public void StopRecord()
{
isRecord = false;
if (videoWriter != null)
{
videoWriter.Dispose();
videoWriter = null;
}
}
}
Emgu.dll的问题
换个版本的dll