Skip to content

WriteGear API Usage Examples: Non-Compression Mode

Important Information

  • DO NOT feed frames to WriteGear with different dimensions or channels, or-else WriteGear API will exit with ValueError.

  • In case WriteGear API fails to detect valid FFmpeg executables on your system, it will auto-switches to this(Non-Compression) Mode.

  • Always use writer.close() at the very end of the main code. NEVER USE IT INBETWEEN CODE to avoid undesired behavior.

After going through WriteGear Usage Examples, Checkout more bonus examples here ➶

Bare-Minimum Usage

Following is the bare-minimum code you need to get started with WriteGear API in Non-Compression Mode:

# import required libraries
from vidgear.gears import CamGear
from vidgear.gears import WriteGear
import cv2

# open any valid video stream(for e.g `myvideo.avi` file)
stream = CamGear(source="myvideo.avi").start()

# Define writer with Non-compression mode and suitable output filename for e.g. `Output.mp4`
writer = WriteGear(output="Output.mp4", compression_mode=False)

# loop over
while True:

    # read frames from stream
    frame = stream.read()

    # check for frame if Nonetype
    if frame is None:
        break

    # {do something with the frame here}

    # write frame to writer
    writer.write(frame)

    # Show output window
    cv2.imshow("Output Frame", frame)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

# close output window
cv2.destroyAllWindows()

# safely close video stream
stream.stop()

# safely close writer
writer.close()

 

 

Using Non-Compression Mode with VideoCapture Gears

In Non-Compression mode, WriteGear API provides flexible control over OpenCV's VideoWriter API parameters through its output_param dictionary parameter by formating them as dictionary attributes. Moreover, WriteGear API can be used in conjunction with any other Gears/APIs effortlessly.

All supported attributes for output_param can be found here ➶

The complete usage example is as follows:

# import required libraries
from vidgear.gears import VideoGear
from vidgear.gears import WriteGear
import cv2

# define suitable tweak parameters for writer
output_params = {"-fourcc": "MJPG", "-fps": 30}

# open live video stream on webcam at first index(i.e. 0) device
stream = VideoGear(source=0, logging=True).start()

# Define writer with defined parameters and suitable output filename 
# for e.g. `Output.mp4`
writer = WriteGear(
    output="Output.mp4", compression_mode=False, logging=True, **output_params
)

# loop over
while True:

    # read frames from stream
    frame = stream.read()

    # check for frame if Nonetype
    if frame is None:
        break

    # {do something with the frame here}
    # lets convert frame to gray for this example
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # write gray frame to writer
    writer.write(gray)

    # Show output window
    cv2.imshow("Output Gray Frame", gray)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

# close output window
cv2.destroyAllWindows()

# safely close video stream
stream.stop()

# safely close writer
writer.close()

 

 

Using Non-Compression Mode with OpenCV

You can easily use WriterGear API directly with any Video Processing library(For e.g OpenCV itself) in Non-Compression Mode. The complete usage example is as follows:

# import required libraries
from vidgear.gears import WriteGear
import cv2

# define suitable tweak parameters for writer
output_params = {"-fourcc": "MJPG", "-fps": 30}

# Open suitable video stream, such as webcam on first index(i.e. 0)
stream = cv2.VideoCapture(0)

# Define writer with defined parameters and suitable output filename 
# for e.g. `Output.mp4`
writer = WriteGear(
    output="Output.mp4", compression_mode=False, logging=True, **output_params
)

# loop over
while True:

    # read frames from stream
    (grabbed, frame) = stream.read()

    # check for frame if not grabbed
    if not grabbed:
        break

    # {do something with the frame here}
    # lets convert frame to gray for this example
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # write gray frame to writer
    writer.write(gray)

    # Show output window
    cv2.imshow("Output Gray Frame", gray)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

# close output window
cv2.destroyAllWindows()

# safely close video stream
stream.release()

# safely close writer
writer.close()

 

 

Using Non-Compression Mode with GStreamer Pipeline

WriteGear API's Non-Compression Mode also supports GStreamer Pipeline as input to its output parameter, when GStreamer Pipeline Mode is enabled. This provides flexible way to write video frames to file or network stream with controlled framerate and bitrate. The complete usage example is as follows:

Requirement for GStreamer Pipelining

GStreamer Pipelining in WriteGear requires your OpenCV to be built with GStreamer support. Checkout this FAQ for compiling OpenCV with GStreamer support.

New in v0.2.5

This example was added in v0.2.5.

In this example we will be constructing GStreamer pipeline to write video-frames into a file(foo.mp4) at 1M video-bitrate.

# import required libraries
from vidgear.gears import WriteGear
import cv2

# enable GStreamer Pipeline Mode for writer
output_params = {"-gst_pipeline_mode": True}

# open live video stream on webcam at first index(i.e. 0) device
stream = cv2.VideoCapture(0)

# gst pipeline to write to a file `foo.mp4` at 1M video-bitrate
GSTPipeline = "appsrc ! videoconvert ! avenc_mpeg4 bitrate=100000 ! mp4mux ! filesink location={}".format(
    "foo.mp4"
)

# Define writer with defined parameters and with our Gstreamer pipeline
writer = WriteGear(
    output=GSTPipeline, compression_mode=False, logging=True, **output_params
)

# loop over
while True:

    # read frames from stream
    (grabbed, frame) = stream.read()

    # check for frame if not grabbed
    if not grabbed:
        break

    # {do something with the frame here}

    # write frame to writer
    writer.write(frame)

    # Show output window
    cv2.imshow("Output Frame", frame)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

# close output window
cv2.destroyAllWindows()

# safely close video stream
stream.release()

# safely close writer
writer.close()