需要使用VideoCapture类
读取mp4文件
cv::VideoCapture vcapture("chouyan2.mp4");
if (vcapture.isOpened()) {
while (vcapture.grab()) {// 直接从视频文件或者设备捕获中抓取帧
cv::Mat myFrame;
//vcapture >> myFrame; // 将视频中的数据流向Mat
if (vcapture.read(myFrame)) {
cv::imshow("读取mp4视频文件", myFrame);
cv::waitKey(40);
}
}
}
vcapture.release();
读取rtsp流并且按下空格键保存图片
修改文件名即可
cv::VideoCapture vcapture("rtsp://admin:Admin123@192.168.101.234");
int picNum{ 0 };
if (vcapture.isOpened()) {
while (vcapture.grab()) {// 直接从视频文件或者设备捕获中抓取帧
cv::Mat myFrame;
//vcapture >> myFrame; // 将视频中的数据流向Mat
if (vcapture.read(myFrame)) {
cv::imshow("读取rtsp视频文件", myFrame);
if (cv::waitKey(20) == 32) {
std::string picName = std::to_string(picNum) + ".jpg";
cv::imwrite(picName.c_str(), myFrame);
picNum++;
}
}
}
}
vcapture.release();
编码生成视频文件
cv::VideoCapture vcapture("rtsp://admin:Admin123@192.168.101.234");
cv::VideoWriter vcwriter("test.mp4", cv::VideoWriter::fourcc('X', '2', '6', '4'), 25
, cv::Size(vcapture.get(cv::CAP_PROP_FRAME_WIDTH), vcapture.get(cv::CAP_PROP_FRAME_HEIGHT)), true);
int picNum{ 0 };
if (vcapture.isOpened()) {
while (vcapture.grab()) {// 直接从视频文件或者设备捕获中抓取帧
cv::Mat myFrame;
//vcapture >> myFrame; // 将视频中的数据流向Mat
if (vcapture.read(myFrame)) {
drawrectangle(myFrame);
cv::imshow("读取rtsp视频文件", myFrame);
int key = cv::waitKey(20);
if (key == 32) {
std::string picName = std::to_string(picNum) + ".jpg";
cv::imwrite(picName.c_str(), myFrame);
picNum++;
break;
} else if (key == 'q') {
break;
}
vcwriter.write(myFrame);
}
}
}
vcapture.release();
vcwriter.release();