视频操作
读取视频
VideoCapture capture((char*)VideoPath);
VideoCapture capture((int)CameraID);
int countCameras()
{
cv::VideoCapture temp_camera;
int maxTested = 10;
for (int i = 0; i < maxTested; i++){
cv::VideoCapture temp_camera(i);
bool res = (!temp_camera.isOpened());
temp_camera.release();
if (res)
{
return i;
}
}
return maxTested;
}
capture.get(cv::CAP_PROP_FRAME_COUNT);
播放视频
Mat frame;
while (1) {
capture >> frame;
if (frame.empty()) {
cout << "frame error" << endl;
break;
}
imshow("video", frame);
waitKey(1);
}
保存视频
VideoWriter videoWriter;
int codec = CV_FOURCC('D', 'I', 'V', 'X');
double fps = 25.0;
string videoName = filepath + "live.avi";
videoWriter.open(videoName, codec, fps, Size(PicWidth, PicWidth), true);
if (!videoWriter.isOpened()) {
cerr << "Could not open the output video file for write\n";
}
while (1) {
capture >> frame;
if (frame.empty()) {
cout << "frame error" << endl;
break;
}
videoWriter.write(frame);
}
编码格式说明:http://www.fourcc.org/codecs.php#letter_p