Skip to content

Cv2.VideoWriter

Video writer class. The class provides C++ API for writing video files or image sequences.

Example

Here is how the class can be used:

#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
int main(int, char**)
{
    Mat src;
    // use default camera as video source
    VideoCapture cap(0);
    // check if we succeeded
    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open camera\n";
        return -1;
    }
    // get one frame from camera to know frame size and type
    cap >> src;
    // check if we succeeded
    if (src.empty()) {
        cerr << "ERROR! blank frame grabbed\n";
        return -1;
    }
    bool isColor = (src.type() == CV_8UC3);
    //--- INITIALIZE VIDEOWRITER
    VideoWriter writer;
    int codec = CV_FOURCC('M', 'J', 'P', 'G');  // select desired codec (must be available at runtime)
    double fps = 25.0;                          // framerate of the created video stream
    string filename = "./live.avi";             // name of the output video file
    writer.open(filename, codec, fps, src.size(), isColor);
    // check if we succeeded
    if (!writer.isOpened()) {
        cerr << "Could not open the output video file for write\n";
        return -1;
    }
    //--- GRAB AND WRITE LOOP
    cout << "Writing videofile: " << filename << endl
         << "Press any key to terminate" << endl;
    for (;;)
    {
        // check if we succeeded
        if (!cap.read(src)) {
            cerr << "ERROR! blank frame grabbed\n";
            break;
        }
        // encode the frame into the videofile stream
        writer.write(src);
        // show live and wait for a key with timeout long enough to show images
        imshow("Live", src);
        if (waitKey(5) >= 0)
            break;
    }
    // the videofile will be closed and released automatically in VideoWriter destructor
    return 0;
}

Open method

Tips

  • With some backends fourcc=-1 pops up the codec selection dialog from the system.
  • To save image sequence use a proper filename (eg. img_%02d.jpg) and fourcc=0 OR fps=0. Use uncompressed image format (eg. img_%02d.BMP) to save raw frames.
  • Most codecs are lossy. If you want lossless video file you need to use a lossless codecs (eg. FFMPEG FFV1, Huffman HFYU, Lagarith LAGS, etc...)
  • If FFMPEG is enabled, using codec=0; fps=0; you can create an uncompressed (raw) video file.

Sample FOURCC

CV_FOURCC('P', 'I', 'M', '1') // MPEG-1 codec
CV_FOURCC('M', 'J', 'P', 'G') // motion-jpeg codec (does not work well)
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('X', '2', '6', '4') // H264 codec
CV_FOURCC('F', 'L', 'V', '1') // FLV1 codec

python 에서는 cv2.VideoWrite_fourcc를 사용하면 된다.

cv2.VideoWrite_fourcc(*"DIV4")  # Divx MPEG-4
cv2.VideoWrite_fourcc(*"DIV5")  # Div5
cv2.VideoWrite_fourcc(*"DIVX")  # DivX
cv2.VideoWrite_fourcc(*"DI50")  # DivX MPEG-4
cv2.VideoWrite_fourcc(*"FMP4")  # FFMpeg
cv2.VideoWrite_fourcc(*"TYUV")  # IYUV
cv2.VideoWrite_fourcc(*"MJPG")  # Motion JPED codec
cv2.VideoWrite_fourcc(*"MP42")  # MPEG4 v2
cv2.VideoWrite_fourcc(*"MPEG")  # MPEG codecs
cv2.VideoWrite_fourcc(*"XVID")  # XVID codecs
cv2.VideoWrite_fourcc(*"X264")  # H.264/AVC codecs

See also

Favorite site