Skip to content

WriteGear API Parameters: Compression Mode¶

 

output¶

This parameter sets the valid filename/path/URL for the video output.

Warning

WriteGear API will throw ValueError if output provided is empty or invalid.

Data-Type: String

Usage:

Its valid input can be one of the following:

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

    writer = WriteGear(output = '/home/foo/foo1') #Define writer 
    
  • Filename (with/without path): Valid filename(with valid extension) of the output video file. In case filename is provided without path, then current working directory will be used. Its parent directory must already exist (WriteGear doesn't create it), and an existing file is overwritten (FFmpeg's -y).

    writer = WriteGear(output = 'output.mp4') #Define writer 
    

    Make sure to provide valid filename with valid file-extension based on the encoder in use.

  • URL: Valid URL of a network stream with a protocol supported by installed FFmpeg (verify with command ffmpeg -protocols) only. This is useful for building a Video-Streaming Server with FFmpeg in WriteGear API. For example, you can stream on a rtmp protocol URL as follows:

    writer = WriteGear(output = 'rtmp://localhost/live/test') #Define writer 
    

 

compression_mode¶

This parameter selects the WriteGear's Primary Mode of Operation, i.e. if this parameter is enabled (.i.e compression_mode = True) WriteGear will use FFmpeg to encode output video, and if disabled (.i.e compression_mode = False), the OpenCV's VideoWriter API will be used for encoding files/streams.

Data-Type: Boolean

Default Value: Its default value is True.

Usage:

WriteGear(output = 'output.mp4', compression_mode=True)

 

custom_ffmpeg¶

This parameter assigns the path or directory where the custom FFmpeg executables are located in Compression Mode only.

Compression Mode Behavior on Windows

In Compression Mode, if a custom FFmpeg executable's path or directory is not provided through custom_ffmpeg parameter on Windows machine, then WriteGear API will automatically attempt to download and extract suitable Static FFmpeg binaries at suitable location on your windows machine. More information can be found here âž¶.

Data-Type: String

Default Value: Its default value is "" (empty string).

Usage:

# if ffmpeg executable is located at "./foo/foo1/ffmpeg" path (On Windows: `./foo/foo1/ffmpeg.exe`)
WriteGear(output = 'output.mp4', compression_mode=True, custom_ffmpeg="./foo/foo1/ffmpeg")
# if ffmpeg executable is located at "./foo/foo1/FFmpeg" directory
WriteGear(output = 'output.mp4', compression_mode=True, custom_ffmpeg="/foo/foo1/FFmpeg/bin")
# if ffmpeg executables are located at "/foo/foo1/FFmpeg"
WriteGear(output = 'output.mp4', custom_ffmpeg="/foo/foo1/FFmpeg")

 

output_params¶

This parameter allows us to exploit almost all FFmpeg supported parameters effortlessly and flexibly for encoding in Compression Mode, by formatting desired FFmpeg Parameters as this parameter's attributes. All supported parameters and encoders for compression mode discussed below:

Kindly read FFmpeg Docs carefully, before passing any values to output_param dictionary parameter. Wrong values may result in undesired Errors or no output at all.

Data-Type: Dictionary

Default Value: Its default value is {}.

Supported Parameters¶

  • FFmpeg Parameters: All parameters based on selected encoder in use, are supported, and can be passed as dictionary attributes in output_param. For example, for using libx264 encoder to produce a lossless output video, we can pass required FFmpeg parameters as dictionary attributes, as follows:

    Extra inputs (-i) only add audio, and end with your frames

    When an extra input is given with -i (in output_params, -clones or -ffpreheaders) and you set no -map/-filter_complex, WriteGear keeps the piped frames as the only video (-map <pipe>:v:0), adds audio from every extra input (-map <n>:a?), pads shorter audio with silence (-af apad, unless you set an audio filter) and adds -shortest. So every frame is written, and infinite inputs (live microphone, anullsrc) end with your frames on close().

    With copied audio (-acodec copy), which can't be padded, WriteGear adds neither -af apad nor -shortest: a finite audio file then simply ends early, but an infinite input never ends, so add -shortest yourself and accept that it cuts the video at the audio's end.

    Valueless FFmpeg flags

    Set a flag's value to None to pass it without a value (e.g. {"-shortest": None} → -shortest). -vcodec/-c:v set to None is the exception (see below).

    All ffmpeg parameters are case-sensitive. Remember to double check every parameter if any error occurs.

    Kindly check H.264 docs âž¶ and other FFmpeg Docs âž¶ for more information on these parameters

     output_params = {"-vcodec":"libx264", "-crf": 0, "-preset": "fast", "-tune": "zerolatency"} 
    
    Discarding -vcodec parameter entirely

    Setting -vcodec (or its alias -c:v) to None in output_params will discard the -vcodec FFmpeg parameter entirely from WriteGear's encoding pipeline. This allows FFmpeg itself to auto-select the best available video encoder for the given output (e.g. for GIF outputs):

    # let FFmpeg auto-select the best video encoder for the output (e.g. GIF)
    output_params = {"-vcodec": None}
    # equivalent using `-c:v` alias
    output_params = {"-c:v": None}
    
  • Special Internal Parameters: In addition to FFmpeg parameters, WriteGear API also supports some Special Parameters to tweak its internal properties. These parameters are discussed below:

    • -ffmpeg_download_path (string): sets the custom directory for downloading FFmpeg Static Binaries in Compression Mode, during the Auto-Installation on Windows Machines Only. If this parameter is not altered, then these binaries will auto-save to the default temporary directory (for e.g. C:/User/temp) on your windows machine. It can be used as follows:

      output_params = {"-ffmpeg_download_path": "C:/User/foo/foo1"} # will be saved to "C:/User/foo/foo1"
      
    • -input_framerate (float/int) : sets the framerate of the incoming frames (FFmpeg's -framerate for the raw pipe input), i.e. how many frames make one second of video. It defaults to 25 fps when omitted or invalid, so set it to your source's real framerate. It can be used as follows:

      output_params = {"-input_framerate": 60.0} # 60 frames make one second of video
      

      For live sources that don't deliver frames at a steady rate, timestamp frames by arrival time instead: {"-ffpreheaders": ["-use_wallclock_as_timestamps", "1"], "-fps_mode": "cfr", "-r": 30} (use -vsync cfr instead of -fps_mode cfr on FFmpeg older than 5.1).

      Its usage example can be found here âž¶

    • -output_dimensions (tuple/list) : scales the output video to custom dimensions(size/resolution) (otherwise input video-frame size will be used). Its value can either be a tuple => (width,height) or a list => [width, height], Its usage is as follows:

      output_params = {"-output_dimensions": (1280,720)} # to produce a 1280x720 resolution/scale output video
      

      With the default yuv420p output of libx264/libx265, odd dimensions are rounded up to the next even number.

    • -input_pixfmt (string) : sets the pixel format of the incoming frames (otherwise auto-derived from their channels and datatype, e.g. bgr24 for 3-channel uint8 frames). It must be a format supported by your FFmpeg (else it is discarded with a warning), and must match the frames' size, else write() raises a ValueError. Its usage is as follows:

      output_params = {"-input_pixfmt": "yuv444p"} # frames are `yuv444p` (3-channel `uint8`)
      
    • -clones (list): required to set special FFmpeg parameters that are repeated more than once in the command (For more info., see this issue) or in cases where you want to preserve order of multiple FFmpeg parameters. This attribute only accepts list datatype as value. Its usage is as follows:

      Turn on logging(logging = True) to see the FFmpeg command that is being executed in WriteGear's pipeline. This helps you debug/address any issues and make adjustments accordingly.

      WriteGear by automatically applies -format or -f, -pix_fmt and -vcodec or -v:f like critical input parameters for every stream. Therefore if you need multiple values for these parameter just define them with -clones attribute.

      output_params = {
          "-i": "plug:dsnoopUSB",
          "-f": "alsa",
          "-ac": "1",
          "-ar": "48000",
          "-clones": ["-vcodec", "mpeg1video", "-f", "mpegts"],
      }
      
    • -ffpreheaders (list): required to set special FFmpeg parameters that are present at the starting of command(such as -re). This attribute only accepts list datatype as value. Its usage is as follows:

      This attribute is quite powerful and can break FFmpeg pipeline easily if not used correctly. User Discretion is advised!

      Turn on logging(logging = True) to see the FFmpeg command that is being executed in WriteGear's pipeline. This helps you debug/address any issues and make adjustments accordingly.

      output_params = {
          "-ffpreheaders": ["-re"], # executes as `ffmpeg -re <rest of command>`
      }
      
    • -disable_ffmpeg_window (bool): sets a special flag to enable detached subprocess creation on Windows OS, and can be useful while creating an .exe file for a python script that uses WriteGear API. On Windows, in certain cases, even after creating the .exe file in windowed mode or no-console mode, the FFmpeg commandline window would pop up while its being used by WriteGear API. Its usage is as follows:

      New in v0.3.2

      This feature was added in v0.3.2.

      -disable_ffmpeg_window is only available on Windows OS with logging disabled(logging=False) in compression mode.

      output_params = {"-disable_ffmpeg_window": True} # disables FFmpeg creation window
      
    • -disable_force_termination (bool): Deprecated, has no effect. WriteGear no longer force-terminates FFmpeg on close() (which dropped buffered frames). Extra inputs get -shortest instead (see above).

      Deprecated in v0.3.8

      This flag is accepted but ignored since v0.3.8, with a warning.

    • -capture_stderr (bool): Deprecated, has no effect. FFmpeg's stderr is now always captured: its last 50 lines (still mirrored to console when logging=True) are exposed via WriteGear's stderr_tail property and appended to the ValueError raised by write() when FFmpeg dies early (e.g. on invalid parameters). FFmpeg's exit code is available via the returncode attribute after close().

      Deprecated in v0.3.8

      Added in v0.3.7, always on since v0.3.8.

Supported Encoders¶

All the encoders that are compiled with FFmpeg in use, are supported by WriteGear API. You can easily check the compiled encoders by running following command in your terminal:

Similarily, supported demuxers and filters depends upons compiled FFmpeg in use.

ffmpeg -encoders           # use `ffmpeg.exe -encoders` on windows

An encoder not listed by your FFmpeg raises a ValueError on first write() (it was silently replaced with libx264 before v0.3.8). copy is also accepted, to store raw frames in a container that takes them (e.g. .nut).

When no encoder is set, WriteGear picks the first available of libx264, libx265, libxvid, mpeg4. For libx264/libx265 it defaults to -crf 18 -preset fast and, for 8-bit frames, -pix_fmt yuv420p (playable in browsers, QuickTime and hardware decoders). Any of these can be overridden in output_params (-c:v wins over -vcodec if both are given).

 

logging¶

This parameter enables logging (if True), essential for debugging.

Data-Type: Boolean

Default Value: Its default value is False.

Usage:

WriteGear(output = 'output.mp4', logging=True)

Â