Class VideoWriter


  • public class VideoWriter
    extends java.lang.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​(java.lang.String filename, int fourcc, double fps, Size frameSize)  
        VideoWriter​(java.lang.String filename, int fourcc, double fps, Size frameSize, boolean isColor)  
        VideoWriter​(java.lang.String filename, int apiPreference, int fourcc, double fps, Size frameSize)
      The apiPreference parameter allows to specify API backends to use.
        VideoWriter​(java.lang.String filename, int apiPreference, int fourcc, double fps, Size frameSize, boolean isColor)
      The apiPreference parameter allows to specify API backends to use.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      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
      java.lang.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​(java.lang.String filename, int fourcc, double fps, Size frameSize)
      Initializes or reinitializes video writer.
      boolean open​(java.lang.String filename, int fourcc, double fps, Size frameSize, boolean isColor)
      Initializes or reinitializes video writer.
      boolean open​(java.lang.String filename, int apiPreference, int fourcc, double fps, Size frameSize)  
      boolean open​(java.lang.String filename, int apiPreference, int fourcc, double fps, Size frameSize, boolean isColor)  
      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 Detail

      • nativeObj

        protected final long nativeObj
    • Constructor Detail

      • 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 VFW is used;
        • On MacOSX QTKit is used.
      • VideoWriter

        public VideoWriter​(java.lang.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 [Video Codecs by FOURCC](http://www.fourcc.org/codecs.php) page. 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 (the flag is currently supported on Windows only). 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​(java.lang.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 [Video Codecs by FOURCC](http://www.fourcc.org/codecs.php) page. 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 (the flag is currently supported on Windows only). 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​(java.lang.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​(java.lang.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
    • Method Detail

      • getNativeObjAddr

        public long getNativeObjAddr()
      • __fromPtr__

        public static VideoWriter __fromPtr__​(long addr)
      • open

        public boolean open​(java.lang.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​(java.lang.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​(java.lang.String filename,
                            int apiPreference,
                            int fourcc,
                            double fps,
                            Size frameSize,
                            boolean isColor)
      • open

        public boolean open​(java.lang.String filename,
                            int apiPreference,
                            int fourcc,
                            double fps,
                            Size frameSize)
      • 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

        public java.lang.String getBackendName()
        Returns used backend API name Note: Stream should be opened.
        Returns:
        automatically generated
      • finalize

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