Opencv video read and write operations (solve the saved video is faster than the real playback speed)

There is a class VideoWriter in opencv that saves videos.
The meaning of several parameters of the VideoWriter constructor:
* 1. The path of the output file
* 2. fourcc is the codec of the four characters used to represent the compressed frame For example:
CV_FOURCC('P', 'I','M','1') = MPEG-1 codec
CV_FOURCC('M','J','P','G') = motion-jpeg codec
CV_FOURCC('M','P', '4', '2') = MPEG-4.2 codec
CV_FOURCC('D','I','V', '3') = MPEG-4.3 codec
CV_FOURCC('D','I','V', 'X') = MPEG-4 codec
CV_FOURCC('U', '2', '6', '3') = H263 codec
CV_FOURCC('I', '2', '6', '3') = H263I codec
CV_FOURCC('F','L','V', '1') = FLV1 codec
If the encoder code is -1, an encoder selection box will pop up when running.
3. Save the video frame rate
4. frame_size saves the width and height of the video.
5. If isColor is non-zero, the encoder will hope to get a color frame and encode; otherwise, it is a grayscale frame (this flag is only supported under Windows)
Remarks: The encoding method that takes up the smallest space for the generated file is MPEG-4.2 codec. The constructor parameter of the VideoWriter class is CV_FOURCC('M','P', '4', '2').
The largest is the MPEG-1 codec. The constructor parameter corresponding to the VideoWriter class is CV_FOURCC('P','I','M','1'), which
occupies 5.7 times the disk space of the former. So if you need 24-hour recording and monitoring, you can give priority to using MPEG-4.2 codec.
It should be noted that when saving a video, you need to wait for a certain time between every two frames. The waiting time is 1000ms/video frame rate, otherwise the saved video will be played faster than the original one. The
specific code is as follows:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char **argv) {
    
    
    VideoCapture capture(1);
    if (!capture.isOpened()) {
    
    
        printf("could not load video data...\n");
        return -1;
    }
    double fps = capture.get(CV_CAP_PROP_FPS);    //读取视频帧率
    Size size = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT));
    printf("FPS : %f", fps);
    VideoWriter writer("/home/fuhong/code/cpp/opencv_learning/src/object_tracing/video/test.avi",
                       CV_FOURCC('M', 'P', '4', '2'), 15.0, size, true);


    // create window
    Mat frame, gray, binary;
    namedWindow("video-demo", CV_WINDOW_AUTOSIZE);

    // show each frame and save
    vector<Mat> bgr;
    while (capture.read(frame)) {
    
    
        //inRange(frame, Scalar(0, 127, 0), Scalar(127, 255, 127), gray);
        //cvtColor(frame, gray, COLOR_BGR2GRAY);
        //threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
        //bitwise_not(frame, frame);
        flip(frame, frame, 1);
        imshow("video-demo", frame);
        writer.write(frame);
        char c = waitKey(1000/fps);  //这里等待的时间为1000ms/视频帧率,要不然保存的视频会比原来播放的快.
        if (c == 27) {
    
    
            break;
        }
    }

    waitKey(0);
    return 0;
}

Guess you like

Origin blog.csdn.net/hongge_smile/article/details/108765240