OpenCV is a vast library that helps in providing various functions for image and video operations. With OpenCV, we can perform operations on the input video. OpenCV also allows us to save that operated video for further usage. For saving images, we use cv2.imwrite() which saves the image to a specified file location. But, for saving a recorded video, we create a Video Writer object.
Firstly, we specify the fourcc variable. FourCC is a 4-byte code used to specify the video codec. List of codes can be obtained at Video Codecs by FourCC. The codecs for Windows is DIVX and for OSX is avc1, h263. FourCC code is passed as cv2.VideoWriter_fourcc(*’MJPG’) for MJPG and cv2.VideoWriter_fourcc(*’XVID’) for DIVX.
Then, the cv2.VideoWriter() function is used.
v2.VideoWriter( filename, fourcc, fps, frameSize )
The parameters are :
- filename: Specifies the name of the output video file.
- fourcc: (for recording) Defining the codec
- fps: Defined frame rate of the output video stream
- frameSize: Size of the video frames
The output screen shows up two windows. The window named ‘Original’ shows input frames, whereas the ‘frame’ window shows the operated video sequence.
Also, a video is recorded and saved with the name ‘output’ in the same file location with predefined frame rate and frame size.
It is generally of the format .avi
The video saved is like this:
The input video can be operated in other color spaces too, like in grayscale
A video file of this operated video is saved in the same file location as we saw above.
This method can help us to create our own dataset for training data in projects / models, to record from our webcam and do necessary operations and also create the video in different color spaces.
Kindly refer this link for visualizing content in different color spaces:
- Face Detection using Python and OpenCV with webcam
- Python | Play a video using OpenCV
- OpenCV C++ Program to play a video
- OpenCV C++ Program to blur a Video
- Python | Play a video in reverse mode using OpenCV
- OpenCV Python program for Vehicle detection in a Video frame
- WebCam Motion Detector in Python
- Saving a Pandas Dataframe as a CSV
- Saving What Saves Our Passwords – Two-Factor Authentication
- Common Video Format
- Extract images from video in Python
- Operations on Audio/Video files using ffmpeg, avconv, and youtube-dl
- Histograms Equalization in OpenCV
- OpenCV: Segmentation using Thresholding
- Set up Opencv with anaconda environment
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.