下载Nuget包:EMGU.CV
引用Emgu.CV;
public void InitVideo()
{
VideoCapture _capture = new VideoCapture("rtsp://admin:123456@192.168.0.198:554");
Thread.Sleep(100);
VideoWriter videoWriter = null;
Mat fram = new Mat();
Task.Factory.StartNew(() => {
while (true)
{
try
{
_capture.Read(fram);
var item = _capture.QueryFrame();
Image Imageshow = item.Bitmap;//获取当前帧图片 发到图片控件中
pictureBox1.Image = Imageshow;
if (videoWriter == null)
{
videoWriter = new VideoWriter("out.mp4", 0, 24, fram.Size, false);
videoWriter.Set(WriterProperty.Framebytes, 10);
}
videoWriter.Write(fram);//写入帧
}
catch (Exception ex)
{
break;
}
Thread.Sleep(10);
}
});
}
参考资料:
https://www.cnblogs.com/LCLBook/p/16649870.html
另一种使用方法:挂载事件操作(速度稍慢)
文章来源:https://www.toymoban.com/news/detail-516716.html
/// <summary>
/// 初始化程序
/// </summary>
private void InitializeVariables()
{
currentDevice = new VideoCapture("rtsp://admin:***@192.168.0.198:554");
recording = false;
videoWidth = currentDevice.Width;
videoHeight = currentDevice.Height;
}
/// <summary>
/// 图片转换事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CurrentDevice_ImageGrabbed(object sender, EventArgs e)
{
try
{
Mat m = new Mat();
currentDevice.Retrieve(m, 0);
VideoPictureBox.Image = m.Bitmap;
if (recording && videoWriter != null)
{
videoWriter.Write(m);
}
Thread.Sleep(10);
}
catch (Exception ex)
{
}
}
/// <summary>
/// 确认录像记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BTStrat_Click(object sender, EventArgs e)
{
recording = true;
SaveFileDialog dialog = new SaveFileDialog();
dialog.DefaultExt = ".mp4";
dialog.AddExtension = true;
dialog.FileName =Guid.NewGuid().ToString("N").Substring(0,5);
DialogResult dialogResult = dialog.ShowDialog();
if (dialogResult != System.Windows.Forms.DialogResult.OK)
{
return;
}
Task.Factory.StartNew(() =>
{
videoWriter = new VideoWriter(dialog.FileName, VideoWriter.Fourcc('M', 'P', '4', 'V'), 30, new System.Drawing.Size(videoWidth, videoHeight), true);
});
}
/// <summary>
/// 关闭录像
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TBClose_Click(object sender, EventArgs e)
{
recording = false;
if (videoWriter != null)
{
currentDevice.ImageGrabbed -= CurrentDevice_ImageGrabbed;//解除事件
currentDevice.Stop();
videoWriter.Dispose();
}
}
/// <summary>
/// 按钮预览
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
currentDevice.ImageGrabbed += CurrentDevice_ImageGrabbed;
currentDevice.Start();
}
参考数据:http://t.zoukankan.com/chengNet-p-11724429.html文章来源地址https://www.toymoban.com/news/detail-516716.html
到了这里,关于C#读取RTSP流并且录制显示视频(PictrueBox)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!