Class VideoWriter

java.lang.Object
org.opencv.videoio.VideoWriter

public class VideoWriter
extends Object
Video writer class. The class provides C++ API for writing video files or image sequences.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected long nativeObj  
  • Constructor Summary

    Constructors 
    Modifier Constructor Description
      VideoWriter()
    Default constructors The constructors/functions initialize video writers.
    protected VideoWriter​(long addr)  
      VideoWriter​(String filename, int fourcc, double fps, Size frameSize)  
      VideoWriter​(String filename, int fourcc, double fps, Size frameSize, boolean isColor)  
      VideoWriter​(String filename, int fourcc, double fps, Size frameSize, MatOfInt params)
    The params parameter allows to specify extra encoder parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ...
      VideoWriter​(String filename, int apiPreference, int fourcc, double fps, Size frameSize)
    The apiPreference parameter allows to specify API backends to use.
      VideoWriter​(String filename, int apiPreference, int fourcc, double fps, Size frameSize, boolean isColor)
    The apiPreference parameter allows to specify API backends to use.
      VideoWriter​(String filename, int apiPreference, int fourcc, double fps, Size frameSize, MatOfInt params)  
  • Method Summary

    Modifier and Type Method Description
    static VideoWriter __fromPtr__​(long addr)  
    protected void finalize()  
    static int fourcc​(char c1, char c2, char c3, char c4)
    Concatenates 4 chars to a fourcc code
    double get​(int propId)
    Returns the specified VideoWriter property
    String getBackendName()
    Returns used backend API name Note: Stream should be opened.
    long getNativeObjAddr()  
    boolean isOpened()
    Returns true if video writer has been successfully initialized.
    boolean open​(String filename, int fourcc, double fps, Size frameSize)
    Initializes or reinitializes video writer.
    boolean open​(String filename, int fourcc, double fps, Size frameSize, boolean isColor)
    Initializes or reinitializes video writer.
    boolean open​(String filename, int fourcc, double fps, Size frameSize, MatOfInt params)  
    boolean open​(String filename, int apiPreference, int fourcc, double fps, Size frameSize)  
    boolean open​(String filename, int apiPreference, int fourcc, double fps, Size frameSize, boolean isColor)  
    boolean open​(String filename, int apiPreference, int fourcc, double fps, Size frameSize, MatOfInt params)  
    void release()
    Closes the video writer.
    boolean set​(int propId, double value)
    Sets a property in the VideoWriter.
    void write​(Mat image)
    Writes the next video frame

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • VideoWriter

      protected VideoWriter​(long addr)
    • VideoWriter

      public VideoWriter()
      Default constructors The constructors/functions initialize video writers.
      • On Linux FFMPEG is used to write videos;
      • On Windows FFMPEG or MSWF or DSHOW is used;
      • On MacOSX AVFoundation is used.
    • VideoWriter

      public VideoWriter​(String filename, int fourcc, double fps, Size frameSize, boolean isColor)
      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 [MSDN](https://docs.microsoft.com/en-us/windows/win32/medfound/video-fourccs) page or with this [archived page](https://web.archive.org/web/20220316062600/http://www.fourcc.org/codecs.php) of the fourcc site for a more complete list). FFMPEG backend with MP4 container natively uses other values as fourcc code: see [ObjectType](http://mp4ra.org/#/codecs), 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. 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.
    • VideoWriter

      public VideoWriter​(String filename, int fourcc, double fps, Size frameSize)
      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 [MSDN](https://docs.microsoft.com/en-us/windows/win32/medfound/video-fourccs) page or with this [archived page](https://web.archive.org/web/20220316062600/http://www.fourcc.org/codecs.php) of the fourcc site for a more complete list). FFMPEG backend with MP4 container natively uses other values as fourcc code: see [ObjectType](http://mp4ra.org/#/codecs), 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. will work with grayscale frames. 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.
    • VideoWriter

      public VideoWriter​(String filename, int apiPreference, int fourcc, double fps, Size frameSize, boolean isColor)
      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.
      Parameters:
      filename - automatically generated
      apiPreference - automatically generated
      fourcc - automatically generated
      fps - automatically generated
      frameSize - automatically generated
      isColor - automatically generated
    • VideoWriter

      public VideoWriter​(String filename, int apiPreference, int fourcc, double fps, Size frameSize)
      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.
      Parameters:
      filename - automatically generated
      apiPreference - automatically generated
      fourcc - automatically generated
      fps - automatically generated
      frameSize - automatically generated
    • VideoWriter

      public VideoWriter​(String filename, int fourcc, double fps, Size frameSize, MatOfInt params)
      The params parameter allows to specify extra encoder parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ... .) see cv::VideoWriterProperties
      Parameters:
      filename - automatically generated
      fourcc - automatically generated
      fps - automatically generated
      frameSize - automatically generated
      params - automatically generated
    • VideoWriter

      public VideoWriter​(String filename, int apiPreference, int fourcc, double fps, Size frameSize, MatOfInt params)
  • Method Details

    • getNativeObjAddr

      public long getNativeObjAddr()
    • __fromPtr__

      public static VideoWriter __fromPtr__​(long addr)
    • open

      public boolean open​(String filename, int fourcc, double fps, Size frameSize, boolean isColor)
      Initializes or reinitializes video writer. The method opens video writer. Parameters are the same as in the constructor VideoWriter::VideoWriter.
      Parameters:
      filename - automatically generated
      fourcc - automatically generated
      fps - automatically generated
      frameSize - automatically generated
      isColor - automatically generated
      Returns:
      true if video writer has been successfully initialized The method first calls VideoWriter::release to close the already opened file.
    • open

      public boolean open​(String filename, int fourcc, double fps, Size frameSize)
      Initializes or reinitializes video writer. The method opens video writer. Parameters are the same as in the constructor VideoWriter::VideoWriter.
      Parameters:
      filename - automatically generated
      fourcc - automatically generated
      fps - automatically generated
      frameSize - automatically generated
      Returns:
      true if video writer has been successfully initialized The method first calls VideoWriter::release to close the already opened file.
    • open

      public boolean open​(String filename, int apiPreference, int fourcc, double fps, Size frameSize, boolean isColor)
    • open

      public boolean open​(String filename, int apiPreference, int fourcc, double fps, Size frameSize)
    • open

      public boolean open​(String filename, int fourcc, double fps, Size frameSize, MatOfInt params)
    • open

      public boolean open​(String filename, int apiPreference, int fourcc, double fps, Size frameSize, MatOfInt params)
    • isOpened

      public boolean isOpened()
      Returns true if video writer has been successfully initialized.
      Returns:
      automatically generated
    • release

      public void release()
      Closes the video writer. The method is automatically called by subsequent VideoWriter::open and by the VideoWriter destructor.
    • write

      public void write​(Mat image)
      Writes the next video frame
      Parameters:
      image - The written frame. In general, color images are expected in BGR format. 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.
    • set

      public boolean 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 REF: videoio_flags_others
      value - Value of the property.
      Returns:
      true if the property is supported by the backend used by the VideoWriter instance.
    • get

      public double get​(int propId)
      Returns the specified VideoWriter property
      Parameters:
      propId - Property identifier from cv::VideoWriterProperties (eg. cv::VIDEOWRITER_PROP_QUALITY) or one of REF: videoio_flags_others
      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.
    • fourcc

      public static int fourcc​(char c1, char c2, char c3, char c4)
      Concatenates 4 chars to a fourcc code
      Parameters:
      c1 - automatically generated
      c2 - automatically generated
      c3 - automatically generated
      c4 - automatically generated
      Returns:
      a fourcc code This static method constructs the fourcc code of the codec to be used in the constructor VideoWriter::VideoWriter or VideoWriter::open.
    • getBackendName

      Returns used backend API name Note: Stream should be opened.
      Returns:
      automatically generated
    • finalize

      protected void finalize() throws Throwable
      Overrides:
      finalize in class Object
      Throws:
      Throwable