class mrpt::img::CVideoFileWriter

Overview

Writes a sequence of CImage frames to a video file.

Supports dependency-free AVI output via two codecs (see VideoCodec). The file is opened with open() and finalised/closed with close() or at destruction.

Example:

mrpt::img::CVideoFileWriter vid;
vid.open("out.avi", 30.0, {640, 480});          // MJPEG by default
// -- or --
vid.open("out.avi", 30.0, {640, 480},
         mrpt::img::VideoCodec::UncompressedRGB);

mrpt::img::CImage img(640, 480, mrpt::img::CH_RGB);
vid << img;          // throws on error
vid.writeImage(img); // returns false on error instead
vid.close();

Replaces the old mrpt::vision::CVideoFileWriter which depended on OpenCV’s CvVideoWriter. The new implementation writes RIFF/AVI directly and uses STB for JPEG encoding (MJPEG path), so there are no external library dependencies beyond what mrpt_img already pulls in.

Both codecs produce standard AVI files (RIFF, OpenDML-compatible index) readable by ffmpeg, VLC, and any DirectShow/GStreamer stack.

#include <mrpt/img/CVideoFileWriter.h>

class CVideoFileWriter
{
public:
    // structs

    struct Impl;

    // construction

    CVideoFileWriter();
    CVideoFileWriter(const CVideoFileWriter&);
    CVideoFileWriter(CVideoFileWriter&&);

    // methods

    CVideoFileWriter& operator = (const CVideoFileWriter&);
    CVideoFileWriter& operator = (CVideoFileWriter&&);

    bool open(
        const std::string& out_file,
        double fps,
        const TImageSize& frameSize,
        VideoCodec codec = VideoCodec::MJPEG,
        int jpeg_quality = 90
        );

    void close();
    bool isOpen() const;
    VideoCodec codec() const;
    TImageSize frameSize() const;
    uint32_t frameCount() const;
    const CVideoFileWriter& operator << (const mrpt::img::CImage& img);
    bool writeImage(const mrpt::img::CImage& img);
};

Construction

CVideoFileWriter()

Default constructor.

No file is opened yet.

Methods

bool open(
    const std::string& out_file,
    double fps,
    const TImageSize& frameSize,
    VideoCodec codec = VideoCodec::MJPEG,
    int jpeg_quality = 90
    )

Open a file and write the AVI headers.

The old isColor parameter has been removed. Color mode is derived automatically from each frame’s CImage::channels(). Grayscale images are written as 8-bit luminance planes; color images are written as BGR24 (or MJPEG BGR). Mixed sequences are not supported - use a consistent channel count throughout.

Parameters:

out_file

Path to the output .avi file (created/truncated).

fps

Frames per second (e.g. 25.0, 29.97, 30.0).

frameSize

Width × height of every frame that will be written. All subsequent images must match this size exactly.

codec

Encoding to use (default: MJPEG).

jpeg_quality

Quality for MJPEG frames, 1-100 (ignored for UncompressedRGB). Higher = better quality / larger file. Default: 90.

Returns:

true on success, false on any I/O error.

void close()

Flush and finalise the AVI file (writes the idx1 index and fixes up all size fields in the RIFF/LIST headers).

Safe to call more than once.

bool isOpen() const

Returns true if a file is currently open.

VideoCodec codec() const

Returns the codec chosen at open().

Undefined if !isOpen().

TImageSize frameSize() const

Returns the frame size chosen at open().

Undefined if !isOpen().

uint32_t frameCount() const

Returns the number of frames written so far.

const CVideoFileWriter& operator << (const mrpt::img::CImage& img)

Write one frame to the video.

Parameters:

std::runtime_error

on any error (image size mismatch, I/O failure, or writing to a closed writer).

bool writeImage(const mrpt::img::CImage& img)

Write one frame to the video.

Returns:

false on any error (prefer this in performance-critical loops where exception overhead matters).