# import required librariesfromvidgear.gearsimportCamGearfromvidgear.gearsimportWriteGearimportcv2# 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 overwhileTrue:# read frames from streamframe=stream.read()# check for frame if NonetypeifframeisNone:break# {do something with the frame here}# write frame to writerwriter.write(frame)# Show output windowcv2.imshow("Output Frame",frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.stop()# safely close writerwriter.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 ➶
# import required librariesfromvidgear.gearsimportVideoGearfromvidgear.gearsimportWriteGearimportcv2# define suitable tweak parameters for writeroutput_params={"-fourcc":"MJPG","-fps":30}# open live video stream on webcam at first index(i.e. 0) devicestream=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 overwhileTrue:# read frames from streamframe=stream.read()# check for frame if NonetypeifframeisNone:break# {do something with the frame here}# lets convert frame to gray for this examplegray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)# write gray frame to writerwriter.write(gray)# Show output windowcv2.imshow("Output Gray Frame",gray)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.stop()# safely close writerwriter.close()
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 librariesfromvidgear.gearsimportWriteGearimportcv2# define suitable tweak parameters for writeroutput_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 overwhileTrue:# read frames from stream(grabbed,frame)=stream.read()# check for frame if not grabbedifnotgrabbed:break# {do something with the frame here}# lets convert frame to gray for this examplegray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)# write gray frame to writerwriter.write(gray)# Show output windowcv2.imshow("Output Gray Frame",gray)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.release()# safely close writerwriter.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 librariesfromvidgear.gearsimportWriteGearimportcv2# enable GStreamer Pipeline Mode for writeroutput_params={"-gst_pipeline_mode":True}# open live video stream on webcam at first index(i.e. 0) devicestream=cv2.VideoCapture(0)# gst pipeline to write to a file `foo.mp4` at 1M video-bitrateGSTPipeline="appsrc ! videoconvert ! avenc_mpeg4 bitrate=100000 ! mp4mux ! filesink location={}".format("foo.mp4")# Define writer with defined parameters and with our Gstreamer pipelinewriter=WriteGear(output=GSTPipeline,compression_mode=False,logging=True,**output_params)# loop overwhileTrue:# read frames from stream(grabbed,frame)=stream.read()# check for frame if not grabbedifnotgrabbed:break# {do something with the frame here}# write frame to writerwriter.write(frame)# Show output windowcv2.imshow("Output Frame",frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.release()# safely close writerwriter.close()