目录
说明
项目
代码
下载
说明
各种原因需要采集数据,方便后续标注、训练……就写了这个小工具
项目
代码
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OpenCvSharp读取rtsp流录制mp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
CancellationTokenSource cts;
VideoCapture capture;
Mat frame;
private volatile bool StopFlag = false;
private volatile bool SaveFlag = false;
private VideoWriter VideoWriter;
string StartupPath = "";
string ImgPath = "";
string Mp4Path = "";
private void button1_Click(object sender, EventArgs e)
{
if (txtRTSP.Text == "")
{
MessageBox.Show("请输入RTSP地址");
txtRTSP.Focus();
return;
}
String rtspUrl = txtRTSP.Text;
button1.Enabled = false;
button2.Enabled = true;
txtInfo.Text = "";
cts = new CancellationTokenSource();
Task task = new Task(() =>
{
capture = new VideoCapture();
capture.Open(rtspUrl, VideoCaptureAPIs.ANY);
if (capture.IsOpened())
{
ShowLog("打开成功");
int time = Convert.ToInt32(Math.Round(1000 / capture.Fps));
frame = new Mat();
//var dsize = new System.Windows.Size(capture.FrameWidth, capture.FrameHeight);
while (true)
{
Thread.Sleep(time);
//判断是否被取消;
if (cts.Token.IsCancellationRequested)
{
pictureBox1.Image = null;
frame = null;
capture.Dispose();
capture = null;
txtInfo.Text = "";
return;
}
//Read image 会阻塞线程
capture.Read(frame);
if (frame.Empty())
continue;
if (SaveFlag && VideoWriter != null)
{
VideoWriter.Write(frame);
}
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
}
pictureBox1.Image = BitmapConverter.ToBitmap(frame);
}
}
}, cts.Token);
task.Start();
}
private void button2_Click(object sender, EventArgs e)
{
button2.Enabled = false;
button1.Enabled = true;
cts.Cancel();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (capture != null)
{
capture.Release();
capture.Dispose();
}
}
private void Form1_Load(object sender, EventArgs e)
{
//capture = new VideoCapture();
StartupPath = Application.StartupPath;
ImgPath = StartupPath + "\\img\\";
Mp4Path = StartupPath + "\\mp4\\";
}
private void button3_Click(object sender, EventArgs e)
{
if (capture == null || capture.IsDisposed)
{
//MessageBox.Show("请先打开摄像头");
ShowLog("请先打开摄像头");
return;
}
if (!Directory.Exists(ImgPath))
{
Directory.CreateDirectory(ImgPath);
}
if (frame != null && !frame.Empty())
{
//图片保存路径和文件名 the path and file name to save
String sJpegPicFileName = ImgPath + "JPEG_test_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff") + ".jpg";
//String sJpegPicFileName = "JPEG_test_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff") + ".jpg";
String str = "Successful to capture the JPEG file and the saved file is: \r\n" + sJpegPicFileName;
Mat dst = frame.Clone();
Cv2.ImWrite(sJpegPicFileName, dst);
dst.Dispose();
ShowLog(str);
}
}
//录像保存路径和文件名 the path and file name to save
string sVideoFileName = "Record_test.mp4";
private void button4_Click(object sender, EventArgs e)
{
if (capture == null || capture.IsDisposed)
{
ShowLog("请先打开摄像头");
return;
}
if (!capture.IsOpened()) return;
button4.Enabled = false;
button5.Enabled = true;
if (!Directory.Exists(Mp4Path))
{
Directory.CreateDirectory(Mp4Path);
}
sVideoFileName = Mp4Path + "Record_test" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff") + ".mp4";
VideoWriter = new VideoWriter(sVideoFileName, FourCC.X264, capture.Fps, new OpenCvSharp.Size(capture.FrameWidth, capture.FrameHeight));
SaveFlag = true;
}
private void button5_Click(object sender, EventArgs e)
{
button5.Enabled = false;
button4.Enabled = true;
SaveFlag = false;
if (VideoWriter != null && !VideoWriter.IsDisposed)
{
VideoWriter.Dispose();
VideoWriter = null;
string str = "Successful to stop recording and the saved file is " + sVideoFileName;
ShowLog(str);
}
}
void ShowLog(string info)
{
txtInfo.Invoke(new Action(() =>
{
txtInfo.Text = "";
txtInfo.Text = info;
}));
}
}
}
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;namespace OpenCvSharp读取rtsp流录制mp4
{public partial class Form1 : Form{public Form1(){InitializeComponent();}CancellationTokenSource cts;VideoCapture capture;Mat frame;private volatile bool StopFlag = false;private volatile bool SaveFlag = false;private VideoWriter VideoWriter;string StartupPath = "";string ImgPath = "";string Mp4Path = "";private void button1_Click(object sender, EventArgs e){if (txtRTSP.Text == ""){MessageBox.Show("请输入RTSP地址");txtRTSP.Focus();return;}String rtspUrl = txtRTSP.Text;button1.Enabled = false;button2.Enabled = true;txtInfo.Text = "";cts = new CancellationTokenSource();Task task = new Task(() =>{capture = new VideoCapture();capture.Open(rtspUrl, VideoCaptureAPIs.ANY);if (capture.IsOpened()){ShowLog("打开成功");int time = Convert.ToInt32(Math.Round(1000 / capture.Fps));frame = new Mat();//var dsize = new System.Windows.Size(capture.FrameWidth, capture.FrameHeight);while (true){Thread.Sleep(time);//判断是否被取消;if (cts.Token.IsCancellationRequested){pictureBox1.Image = null;frame = null;capture.Dispose();capture = null;txtInfo.Text = "";return;}//Read image 会阻塞线程capture.Read(frame);if (frame.Empty())continue;if (SaveFlag && VideoWriter != null){VideoWriter.Write(frame);}if (pictureBox1.Image != null){pictureBox1.Image.Dispose();}pictureBox1.Image = BitmapConverter.ToBitmap(frame);}}}, cts.Token);task.Start();}private void button2_Click(object sender, EventArgs e){button2.Enabled = false;button1.Enabled = true;cts.Cancel();}private void Form1_FormClosing(object sender, FormClosingEventArgs e){if (capture != null){capture.Release();capture.Dispose();}}private void Form1_Load(object sender, EventArgs e){//capture = new VideoCapture();StartupPath = Application.StartupPath;ImgPath = StartupPath + "\\img\\";Mp4Path = StartupPath + "\\mp4\\";}private void button3_Click(object sender, EventArgs e){if (capture == null || capture.IsDisposed){//MessageBox.Show("请先打开摄像头");ShowLog("请先打开摄像头");return;}if (!Directory.Exists(ImgPath)){Directory.CreateDirectory(ImgPath);}if (frame != null && !frame.Empty()){//图片保存路径和文件名 the path and file name to saveString sJpegPicFileName = ImgPath + "JPEG_test_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff") + ".jpg";//String sJpegPicFileName = "JPEG_test_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff") + ".jpg";String str = "Successful to capture the JPEG file and the saved file is: \r\n" + sJpegPicFileName;Mat dst = frame.Clone();Cv2.ImWrite(sJpegPicFileName, dst);dst.Dispose();ShowLog(str);}}//录像保存路径和文件名 the path and file name to savestring sVideoFileName = "Record_test.mp4";private void button4_Click(object sender, EventArgs e){if (capture == null || capture.IsDisposed){ShowLog("请先打开摄像头");return;}if (!capture.IsOpened()) return;button4.Enabled = false;button5.Enabled = true;if (!Directory.Exists(Mp4Path)){Directory.CreateDirectory(Mp4Path);}sVideoFileName = Mp4Path + "Record_test" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff") + ".mp4";VideoWriter = new VideoWriter(sVideoFileName, FourCC.X264, capture.Fps, new OpenCvSharp.Size(capture.FrameWidth, capture.FrameHeight));SaveFlag = true;}private void button5_Click(object sender, EventArgs e){button5.Enabled = false;button4.Enabled = true;SaveFlag = false;if (VideoWriter != null && !VideoWriter.IsDisposed){VideoWriter.Dispose();VideoWriter = null;string str = "Successful to stop recording and the saved file is " + sVideoFileName;ShowLog(str);}}void ShowLog(string info){txtInfo.Invoke(new Action(() =>{txtInfo.Text = "";txtInfo.Text = info;}));}}
}
下载
源码下载