Python OpenCV Writing To A Video

In this Python OpenCV article i want to show you how you can do Writing in To A Video, so for this you can watch my previous post on How To Read Mp4 Videos In Python OpenCV .

 

 

 

This is the complete source code for this article

 

 

So in the above code first we have imported cv2  and after there are some lines of code that you can use for Reading Videos In OpenCV.

 

 

 

In these line of codes we are going to give name for our OpenCV window.

 

 

 

This section of code is for creating and reading from input file.if the input is a camera pass 0  instead of the video filename.

 

 

 

When we want to write a video in OpenCV we need to toil a bit harder. We need to create a VideoWriter object. first, we should specify the output file name with its format (eg: output.avi). Then, we should specify the FourCC code and the number of frames per second (FPS). Lastly, the frame size should be passed.

 

FourCC is 4-character code of codec used to compress the frames. For example, VideoWriter::fourcc(‘P’,’I’,’M’,’1′) is a MPEG-1 codec, VideoWriter::fourcc(‘M’,’J’,’P’,’G’) is a motion-jpeg codec etc. List of codes can be obtained at Video Codecs by FOURCC page. FFMPEG backend with MP4 container natively uses other values as fourcc code.

 

 

 

In here we have added our resolution and also frame rate.

 

 

 

After that we are checking that if the capture is opened we are reading the frames.

 

 

 

 

Because we want to draw a circle in the recorder video so you can do like this

 

 

 

 

 

Run the code you will see the video

OpenCV Writing To A Video
OpenCV Writing To A Video

 

 

 

 

 

Also you can watch the complete video for this article

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Codeloop
Share via
Copy link
Powered by Social Snap
×