Skip to content

WriteGear

Abhishek Thakur edited this page Jul 18, 2019 · 32 revisions

WriteGear Class ⚙️

WriteGear handles powerful libraries and tool that allow us almost anything you can imagine with multimedia files and helps us achieving High-Speed Lossless Video Encoding and Compression capabilities within Vidgear. This class basically pipelines, processes, encodes/decodes and finally writes the input data into a valid Multimedia file with proper settings and all in just one line. This class primarily operates in the following two modes:

  1. In Compression Mode: WriteGear utilizes FFmpeg's inbuilt encoders to encode/decode multimedia files provides the ability to do almost anything that is possible with FFmpeg itself. For example, this class can be used to reduce the output to a smaller size, without sacrificing the video quality, encoding audio and video together, etc. Furthermore, This class allows us to exploit almost all available parameters available within FFmpeg that you can think of, with so much ease and flexibility and in doing that it robustly handles errors/warnings all along very quietly. Find more about this mode here.

  2. In Non-Compression Mode: WriteGear utilizes basic OpenCV's inbuilt VideoWriter class in this mode. This class also supports few parameters/properties available with OpenCV VideoWriter, but this mode lacks the ability to manipulate encoding parameters and other important features like video compression, audio encoding etc. Find more about this mode here.


Warning: ⚠️

  • DO NOT feed frames to WriteGear with different dimensions or channels!, otherwise WriteGear will exit with ValueError.

  • In case WriteGear class fails to detect valid FFmpeg executables on your system(even if Compression Mode is enabled), it can automatically fallbacks to Non-Compression Mode.

  • Follow this WIKI Page for FFmpeg installation.


 

Importing:

You can import this class as follows:

from vidgear.gears import WriteGear

 

Important WriteGear Functions: ⛓️

  1. write(self, frame, rgb_mode = False) : pipelines ndarray frames to valid Video File.

    Parameters:

    • rgb_mode: set this flag to enable RGB Mode - specifies that incoming frames are of RGB format(instead of default BGR). Its default value is False. This flag only works in Compression Mode.
  2. close(self): It safely Terminates the Write process. Therefore it is compulsory to call this function at the end of the main function.


    ⚠️ Precaution: Heavy resolution multimedia files takes time to render which can last upto ~.1-to-3 secs, Kindly wait till the function exit itself and DO NOT kill the process instead.


 

Usage: 🔨

1. WriteGear Class(Compression Mode: FFmpeg):

See here for details and complete example

2. WriteGear Class(Non-Compression Mode: OpenCV):

See here for details and complete example

 

Parameters and Attributes: 🔧

  1. output_filename(string): sets the output Video filename. It's Valid Inputs are:

    • path: Valid path of the directory to save the output video file. In this case, the WriteGear class will automatically assign a unique filename (with a default extension i.e..mp4).
    • filename(with/without path): Valid filename(with valid extension) of the output video file.

    ⏹️ Note: Make sure to provide valid filename with valid file-extension based on encoder(in case of FFmpeg)/ FOURCC(in case of OpenCV) whichever being currently in use.


  2. compression_mode(boolean): enables/disables Video Compression Mode, i.e If enabled(means Compression Mode) WriteGear class will utilize the FFmpeg installed/given executables to encode output video and if disabled(means Non-Compression Mode) OpenCV's VideoWriter Class will be used. Its default value is True.

  3. custom_ffmpeg(string): (if specified) sets the custom path/directory where the Custom FFmpeg executables are located and works only in Compression Mode.


    ⏹️ Note: It is compulsory to provide custom binaries on a Windows Machine, otherwise this class with automatically download and extract suitable Static FFmpeg binaries to Temporary directory.


  4. **output_params(dict): This attribute provides the flexibility to manipulate output video properties directly which are supported by FFmpeg(in Compression Mode)/ OpenCV's VideoWriter(in Non-Compression Mode) in WriterGear Class. All the valid User-defined FFmpeg/OpenCV's VideoWriter output video parameters/properties direct manipulation is currently supported. In addition, other special attributes to manipulate WriteGear inbuilt properties are also supported.

    👁️‍🗨️ All supported parameters details and their usage is depicted in the following links:

  5. logging(boolean): set this flag to enable/disable error logging essential for debugging. Its default value is False.

Clone this wiki locally