비디오를 저장하려면 저장할 위치를 정의해야 합니다. 그런 다음 FourCC를 지정해야 합니다. FourCC는 '4개의 문자 코드'를 나타냅니다. 데이터 형식을 식별하는 4바이트 문자 시퀀스입니다. 또한 비디오를 저장하기 위해 FPS를 선언해야 하며 이 저장 프로세스에도 프레임 크기가 필요합니다. 다음 프로그램은 기본 카메라에서 실시간 비디오 스트림을 가져와 C 디렉터리에 저장합니다.
다음 프로그램은 C++를 사용하여 OpenCV로 컴퓨터에 비디오를 저장하는 방법을 보여줍니다.
예시
#include<opencv2/opencv.hpp>//OpenCV header to use VideoCapture class and VideoWriter//
#include<iostream>
using namespace std;
using namespace cv;
int main() {
Mat myImage;//Declaring a matrix to store the frames//
VideoCapture cap(0);//Taking an object of VideoCapture Class to capture video from default camera//
namedWindow("Video Player");//Declaring the video to show the video//
if(!cap.isOpened()){ //This section prompt an error message if no video stream is found//
cout << "Failed to access the camera" << endl;
system("pause");
return-1;
}
int frame_width = cap.get(CAP_PROP_FRAME_WIDTH);//Getting the frame height//
int frame_height = cap.get(CAP_PROP_FRAME_HEIGHT);//Getting the frame width//
VideoWriter video("video1.mp4",10,17,Size(frame_width, frame_height));//Declaring an object of VideoWriter class//
while (true){ //Taking an everlasting loop to show the video//
cap >> myImage;
if (myImage.empty()){ //Breaking the loop if no video frame is detected//
break;
}
video.write(myImage);//Write the video//
imshow("Video Player", myImage);//Showing the video//
char c= (char)waitKey(25);
if(c==27){
break;
}
}
cap.release();//Releasing the buffer memory//
video.release();
return 0;
} 이 프로그램은 정의된 형식의 정의된 이름으로 정의된 디렉토리에 비디오를 저장합니다.