1. The code is as follows:
#include<opencv2/opencv.hpp> using namespace cv; //-----------------------------------[main() function]------- ------------------------------------- // Description: The entry function of the console application, our program starts here //------------------------------------------------------------------------------------------------- void main() { //[1] Read video from video file VideoCapture capture("Blessing film.avi");//Read the video file if (!capture.isOpened())//If the video file is not opened, return. return; Size size0 = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT)); VideoWriter writer("out.avi", CV_FOURCC('X', 'V', 'I', 'D'), capture.get(CV_CAP_PROP_FPS), size0, true); //[2] Loop to display each frame while (1) { Mat frame; //Define a Mat variable to store the image of each frame capture >> frame; //Read the current frame writer << frame;//Equivalent to writer.write(frame); to write the frame. if (frame.empty()) { break; } imshow("Video", frame); //Display the current frame waitKey(30); //delay 30ms } capture.release();//Release resources destroyAllWindows();//Close all windows }
2. The effect is as follows:
3. A video file named "out.avi" is written in the program running directory.
4. Notes:
1) Before writing the video, you need to install the corresponding codec (eg: XVID)
2) Whether the generated video supports color should be consistent with the constructor setting
3) The size of the generated video must be the same as the size of the read video