Class CVideoFileWriter

Class Documentation

class CVideoFileWriter

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();

Note

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.

Note

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

Public Functions

CVideoFileWriter()

Default constructor. No file is opened yet.

~CVideoFileWriter()

Destructor - calls close() if the file is still open.

CVideoFileWriter(const CVideoFileWriter&) = delete
CVideoFileWriter &operator=(const CVideoFileWriter&) = delete
CVideoFileWriter(CVideoFileWriter&&) noexcept
CVideoFileWriter &operator=(CVideoFileWriter&&) noexcept
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.

Note

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 noexcept

Returns true if a file is currently open.

VideoCodec codec() const noexcept

Returns the codec chosen at open(). Undefined if !isOpen().

TImageSize frameSize() const noexcept

Returns the frame size chosen at open(). Undefined if !isOpen().

uint32_t frameCount() const noexcept

Returns the number of frames written so far.

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

Write one frame to the video.

Throws:

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).