class cv::VideoWriter

Overview

Video writer class. More…

#include <videoio.hpp>

class VideoWriter
{
public:
    // construction

    VideoWriter();

    VideoWriter(
        const String& filename,
        int fourcc,
        double fps,
        Size frameSize,
        bool isColor = true
        );

    VideoWriter(
        int apiPreference,
        const String& filename,
        int fourcc,
        double fps,
        Size frameSize,
        bool isColor = true
        );

    // methods

    virtual
    double
    get(int propId) const;

    virtual
    bool
    isOpened() const;

    virtual
    bool
    open(
        const String& filename,
        int fourcc,
        double fps,
        Size frameSize,
        bool isColor = true
        );

    bool
    open(
        int apiPreference,
        const String& filename,
        int fourcc,
        double fps,
        Size frameSize,
        bool isColor = true
        );

    virtual
    VideoWriter&
    operator<<(const Mat& image);

    virtual
    void
    release();

    virtual
    bool
    set(
        int propId,
        double value
        );

    virtual
    void
    write(const Mat& image);

    static
    int
    fourcc(
        char c1,
        char c2,
        char c3,
        char c4
        );

protected:
    // fields

    Ptr<IVideoWriter> iwriter;
    Ptr<CvVideoWriter> writer;

    // methods

    static
    Ptr<IVideoWriter>
    create(
        const String& filename,
        int fourcc,
        double fps,
        Size frameSize,
        bool isColor = true
        );
};

Detailed Documentation

Video writer class.

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

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;
}

Construction

VideoWriter()

Default constructors.

The constructors/functions initialize video writers.

  • On Linux FFMPEG is used to write videos;
  • On Windows FFMPEG or VFW is used;
  • On MacOSX QTKit is used.
VideoWriter(
    const String& filename,
    int fourcc,
    double fps,
    Size frameSize,
    bool isColor = true
    )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. 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.

Parameters:

filename Name of the output video file.
fourcc 4-character code of codec used to compress the frames. For example, VideoWriter::fourcc (‘P’,’I’,’M’,‘1’) is a MPEG-1 codec, VideoWriter::fourcc (‘M’,’J’,’P’,’G’) is a motion-jpeg codec etc. List of codes can be obtained at Video Codecs by FOURCC page. FFMPEG backend with MP4 container natively uses other values as fourcc code: see ObjectType, so you may receive a warning message from OpenCV about fourcc code conversion.
fps Framerate of the created video stream.
frameSize Size of the video frames.
isColor If it is not zero, the encoder will expect and encode color frames, otherwise it will work with grayscale frames (the flag is currently supported on Windows only).
VideoWriter(
    int apiPreference,
    const String& filename,
    int fourcc,
    double fps,
    Size frameSize,
    bool isColor = true
    )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. The apiPreference parameter allows to specify API backends to use. Can be used to enforce a specific reader implementation if multiple are available: e.g. cv::CAP_FFMPEG or cv::CAP_GSTREAMER.

Methods

virtual
double
get(int propId) const

Returns the specified VideoWriter property.

Parameters:

propId Property identifier from cv::VideoWriterProperties (eg. cv::VIDEOWRITER_PROP_QUALITY) or one of Additional flags for video I/O API backends

Returns:

Value for the specified property. Value 0 is returned when querying a property that is not supported by the backend used by the VideoWriter instance.

virtual
bool
isOpened() const

Returns true if video writer has been successfully initialized.

virtual
bool
open(
    const String& filename,
    int fourcc,
    double fps,
    Size frameSize,
    bool isColor = true
    )

Initializes or reinitializes video writer.

The method opens video writer. Parameters are the same as in the constructor VideoWriter::VideoWriter. The method first calls VideoWriter::release to close the already opened file.

Returns:

true if video writer has been successfully initialized

bool
open(
    int apiPreference,
    const String& filename,
    int fourcc,
    double fps,
    Size frameSize,
    bool isColor = true
    )

Initializes or reinitializes video writer.

The method opens video writer. Parameters are the same as in the constructor VideoWriter::VideoWriter. The method first calls VideoWriter::release to close the already opened file.

Returns:

true if video writer has been successfully initialized

virtual
VideoWriter&
operator<<(const Mat& image)

Stream operator to write the next video frame.

See also:

write

virtual
void
release()

Closes the video writer.

The method is automatically called by subsequent VideoWriter::open and by the VideoWriter destructor.

virtual
bool
set(
    int propId,
    double value
    )

Sets a property in the VideoWriter.

Parameters:

propId Property identifier from cv::VideoWriterProperties (eg. cv::VIDEOWRITER_PROP_QUALITY) or one of Additional flags for video I/O API backends
value Value of the property.

Returns:

true if the property is supported by the backend used by the VideoWriter instance.

virtual
void
write(const Mat& image)

Writes the next video frame.

The function/method writes the specified image to video file. It must have the same size as has been specified when opening the video writer.

Parameters:

image The written frame
static
int
fourcc(
    char c1,
    char c2,
    char c3,
    char c4
    )

Concatenates 4 chars to a fourcc code.

This static method constructs the fourcc code of the codec to be used in the constructor VideoWriter::VideoWriter or VideoWriter::open.

Returns:

a fourcc code