VideoWriter

class VideoWriter(self)

Bases: pybind11_object

Class that enables to write easily a video file or a sequence of images.

This class has its own implementation to write a sequence of PGM and PPM images.

This class may benefit from optional 3rd parties:

  • libpng: If installed this optional 3rd party is used to write a sequence of PNG images. Installation instructions are provided here https://visp.inria.fr/3rd_png .

  • libjpeg: If installed this optional 3rd party is used to write a sequence of JPEG images. Installation instructions are provided here https://visp.inria.fr/3rd_jpeg .

  • OpenCV: If installed this optional 3rd party is used to write a sequence of images where images could be in TIFF, BMP, DIB, PBM, RASTER, JPEG2000 format. If libpng or libjpeg is not installed, OpenCV is also used to consider these image formats. OpenCV allows also to consider AVI, MPEG, MPEG4, MOV, OGV, WMV, FLV, MKV video formats. Installation instructions are provided here https://visp.inria.fr/3rd_opencv .

The following example available in tutorial-video-recorder.cpp shows how this class can be used to record a video from a camera by default in an mpeg file.

#include <visp3/core/vpConfig.h>
#include <visp3/core/vpTime.h>
#include <visp3/gui/vpDisplayFactory.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/io/vpVideoWriter.h>
#include <visp3/sensor/vpV4l2Grabber.h>

#if defined(HAVE_OPENCV_VIDEOIO)
#include <opencv2/videoio.hpp>
#endif

int main(int argc, const char *argv[])
{
#if (defined(VISP_HAVE_V4L2) || defined(HAVE_OPENCV_VIDEOIO)) && defined(VISP_HAVE_DISPLAY)
#ifdef ENABLE_VISP_NAMESPACE
  using namespace VISP_NAMESPACE_NAME ;
#endif
  std::string opt_videoname = "video-recorded.mpg";
  int opt_device = 0;

  for (int i = 0; i < argc; i++) {
    if (std::string(argv[i]) == "--device")
      opt_device = atoi(argv[i + 1]);
    else if (std::string(argv[i]) == "--recorded-name")
      opt_videoname = std::string(argv[i + 1]);
    else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
      std::cout << "\nUsage: " << argv[0]
        << " [--device <device number>] [--recorded-name <video name>] [--help][-h]\n"
        << std::endl;
      return EXIT_SUCCESS;
    }
  }

  std::cout << "Use device: " << opt_device << std::endl;
  std::cout << "Record video in: " << opt_videoname << std::endl;

  try {
    // vpImage<vpRGBa> I; // for color images
    vpImage<unsigned char>  I; // for gray images

#if defined(VISP_HAVE_V4L2)
    std::cout << "Use v4l2 grabber..." << std::endl;
    vpV4l2Grabber  g;
    std::ostringstream device;
    device << "/dev/video" << opt_device;
    g. setDevice (device.str());
    g. setScale (1); // Acquire full resolution images
    g. open (I);
    g. acquire (I);
#elif defined(HAVE_OPENCV_VIDEOIO)
    std::cout << "Use OpenCV grabber..." << std::endl;
    cv::VideoCapture g(opt_device);
    if (!g.isOpened()) { // check if we succeeded
      std::cout << "Failed to open the camera" << std::endl;
      return EXIT_FAILURE;
    }
    cv::Mat frame;
    g >> frame; // get a new frame from camera
    vpImageConvert::convert (frame, I);
#endif

    std::cout << "Image size: " << I. getWidth () << " " << I. getHeight () << std::endl;

#if defined(VISP_HAVE_DISPLAY)
    vpDisplay  *d = vpDisplayFactory::allocateDisplay ();
    d->init(I, 0, 0, "Camera view");
#endif
    vpVideoWriter  writer;

#if defined(HAVE_OPENCV_VIDEOIO)
#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
    writer. setCodec (cv::VideoWriter::fourcc('P', 'I', 'M', '1')); // MPEG-1 codec
#else
    writer. setCodec (CV_FOURCC('P', 'I', 'M', '1'));
#endif
#endif
    writer. setFileName (opt_videoname);
    writer. open (I);
    bool recording = false;

    for (;;) {
#if defined(VISP_HAVE_V4L2)
      g. acquire (I);
#elif defined(HAVE_OPENCV_VIDEOIO)
      g >> frame;
      vpImageConvert::convert (frame, I);
#endif
      vpDisplay::display (I);
      if (recording == false) {
        vpDisplay::displayText (I, 10, 10, "A click to start recording", vpColor::green );
        if ( vpDisplay::getClick (I, false))
          recording = true;
      }
      else {
        writer. saveFrame (I);
        vpDisplay::displayText (I, 10, 10, "Recording: A click to stop and exit", vpColor::red );
        if ( vpDisplay::getClick (I, false))
          break;
      }

      vpDisplay::flush (I);
    }
    std::cout << "The video was recorded in \"" << opt_videoname << "\"" << std::endl;
#ifdef VISP_HAVE_DISPLAY
    delete d;
#endif
  }
  catch (const vpException  &e) {
    std::cout << "Catch an exception: " << e << std::endl;
  }
#else
  (void)argc;
  (void)argv;
  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
#endif
  return EXIT_SUCCESS;
}

The following example shows also how this class can be used to write an image sequence. The images are stored in the folder “./image” and are named “image0000.jpeg”, “image0001.jpeg”, “image0002.jpeg”, …

#include <visp3/core/vpConfig.h>
#include <visp3/io/vpVideoWriter.h>

#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif

int main()
{
  vpImage<vpRGBa> I;

  vpVideoWriter writer;

  //Initialize the writer.
  writer.setFileName("./image/image%04d.jpeg");

  writer.open(I);

  for ( ; ; )
  {
    //Here the code to capture or create an image and stores it in I.

    //Save the image
    writer.saveFrame(I);
  }

  writer.close();

  return 0;
}

The other following example explains how to use the class to write directly an mpeg file.

#include <visp3/io/vpVideoWriter.h>

#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif

int main()
{
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_VIDEOIO) && defined(HAVE_OPENCV_HIGHGUI)
  vpImage<vpRGBa> I;

  vpVideoWriter writer;

  // Set up the framerate to 30Hz. Default is 25Hz.
  writer.setFramerate(30);

#if VISP_HAVE_OPENCV_VERSION >= 0x030000
  writer.setCodec(cv::VideoWriter::fourcc('P', 'I', 'M', '1'));
#else
  writer.setCodec(CV_FOURCC('P', 'I', 'M', '1'));
#endif

  writer.setFileName("./test.mpeg");

  writer.open(I);

  for (; ; ) {
    // Here the code to capture or create an image and store it in I.

    // Save the image
    writer.saveFrame(I);
  }

  writer.close();
#endif
  return 0;
}

Basic constructor.

Methods

__init__

Basic constructor.

close

Deallocates parameters use to write the video or the image sequence.

getCurrentFrameIndex

Gets the current frame index.

getFrameName

Return the name of the file in which the last frame was saved.

open

Overloaded function.

resetFrameCounter

Reset the frame counter and sets it to the first image index.

saveFrame

Overloaded function.

setCodec

setFileName

It enables to set the path and the name of the video or sequence of images which will be saved.

setFirstFrameIndex

Enables to set the first frame index.

setFrameStep

Set frame step between 2 successive images when a sequence of images is considered.

setFramerate

Sets the framerate in Hz of the video when encoding.

Inherited Methods

Operators

__doc__

__init__

Basic constructor.

__module__

Attributes

__annotations__

__init__(self)

Basic constructor.

close(self) None

Deallocates parameters use to write the video or the image sequence.

getCurrentFrameIndex(self) int

Gets the current frame index.

Returns:

Returns the current frame index.

getFrameName(self) str

Return the name of the file in which the last frame was saved.

open(*args, **kwargs)

Overloaded function.

  1. open(self: visp._visp.io.VideoWriter, I: visp._visp.core.ImageRGBa) -> None

Sets all the parameters needed to write the video or the image sequence.

This function will also create recursively the folders to host the video or the sequence of images.

Parameters:
I

One image with the right dimensions.

  1. open(self: visp._visp.io.VideoWriter, I: visp._visp.core.ImageGray) -> None

Sets all the parameters needed to write the video or the image sequence.

This function will also create recursively the folders to host the video or the sequence of images.

Parameters:
I

One image with the right dimensions.

resetFrameCounter(self) None

Reset the frame counter and sets it to the first image index.

By default the first frame index is set to 0.

saveFrame(*args, **kwargs)

Overloaded function.

  1. saveFrame(self: visp._visp.io.VideoWriter, I: visp._visp.core.ImageRGBa) -> None

Saves the image as a frame of the video or as an image belonging to the image sequence.

Each time this method is used, the frame counter is incremented and thus the file name change for the case of an image sequence.

Parameters:
I

The image which has to be saved.

  1. saveFrame(self: visp._visp.io.VideoWriter, I: visp._visp.core.ImageGray) -> None

Saves the image as a frame of the video or as an image belonging to the image sequence.

Each time this method is used, the frame counter is incremented and thus the file name change for the case of an image sequence.

Parameters:
I

The image which has to be saved.

setCodec(self, fourcc_codec: int) None
setFileName(self, filename: str) None

It enables to set the path and the name of the video or sequence of images which will be saved.

If you want to write a sequence of images, filename corresponds to the path followed by the image name template. For example, if you want to write different images named image0001.jpeg , image0002.jpg , … and located in the folder /local/image , filename will be /local/image/image%04d.jpg .

Note

The function open() will create recursively the folders to host the video or the sequence of images.

Parameters:
filename: str

filename template of an image sequence.

setFirstFrameIndex(self, first_frame: int) None

Enables to set the first frame index.

Parameters:
first_frame: int

The first frame index. Value should be positive.

setFrameStep(self, frame_step: int) None

Set frame step between 2 successive images when a sequence of images is considered.

Parameters:
frame_step: int

Step between 2 successive images. The default value is 1.

setFramerate(self, framerate: float) None

Sets the framerate in Hz of the video when encoding.

By default the framerate is set to 25Hz.

Note

Framerate can only be set when OpenCV > 2.1.0.

Parameters:
framerate: float

The expected framerate.