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: -
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).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 artmpprotocol URL as follows:
Â
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:
Â
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 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 usinglibx264 encoderto 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 framesWhen an extra input is given with
-i(inoutput_params,-clonesor-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 onclose().With copied audio (
-acodec copy), which can't be padded, WriteGear adds neither-af apadnor-shortest: a finite audio file then simply ends early, but an infinite input never ends, so add-shortestyourself and accept that it cuts the video at the audio's end.Valueless FFmpeg flags
Set a flag's value to
Noneto pass it without a value (e.g.{"-shortest": None}→-shortest).-vcodec/-c:vset toNoneis 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
Discarding
-vcodecparameter entirelySetting
-vcodec(or its alias-c:v) toNoneinoutput_paramswill discard the-vcodecFFmpeg 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): -
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: -
-input_framerate(float/int) : sets the framerate of the incoming frames (FFmpeg's-frameratefor the raw pipe input), i.e. how many frames make one second of video. It defaults to25fps when omitted or invalid, so set it to your source's real framerate. It can be used as follows: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 cfrinstead of-fps_mode cfron 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 videoWith the default
yuv420poutput oflibx264/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.bgr24for 3-channeluint8frames). It must be a format supported by your FFmpeg (else it is discarded with a warning), and must match the frames' size, elsewrite()raises aValueError. Its usage is as follows: -
-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
-formator-f,-pix_fmtand-vcodecor-v:flike critical input parameters for every stream. Therefore if you need multiple values for these parameter just define them with-clonesattribute. -
-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. -
-disable_ffmpeg_window(bool): sets a special flag to enable detached subprocess creation on Windows OS, and can be useful while creating an.exefile for a python script that uses WriteGear API. On Windows, in certain cases, even after creating the.exefile 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_windowis only available on Windows OS with logging disabled(logging=False) in compression mode. -
-disable_force_termination(bool): Deprecated, has no effect. WriteGear no longer force-terminates FFmpeg onclose()(which dropped buffered frames). Extra inputs get-shortestinstead (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 whenlogging=True) are exposed via WriteGear'sstderr_tailproperty and appended to theValueErrorraised bywrite()when FFmpeg dies early (e.g. on invalid parameters). FFmpeg's exit code is available via thereturncodeattribute afterclose().Deprecated in v0.3.8
Added in
v0.3.7, always on sincev0.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.
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:
Â