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.
cv2.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
# Python program to illustrate # saving an operated video # organize imports import numpy as np import cv2 # This will return video from the first webcam on your computer. cap = cv2.VideoCapture(0) # Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480)) # loop runs if capturing has been initialized. while(True): # reads frames from a camera # ret checks return at each frame ret, frame = cap.read() # Converts to HSV color space, OCV reads colors as BGR # frame is converted to hsv hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # output the frame out.write(hsv) # The original input frame is shown in the window cv2.imshow('Original', frame) # The window showing the operated video stream cv2.imshow('frame', hsv) # Wait for 'a' key to stop the program if cv2.waitKey(1) & 0xFF == ord('a'): break # Close the window / Release webcam cap.release() # After we release our webcam, we also release the output out.release() # De-allocate any associated memory usage cv2.destroyAllWindows() |
Output:
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:
Output Video
The input video can be operated in other color spaces too, like in grayscale
# Python program to illustrate # saving an operated video # organize imports import numpy as np import cv2 # This will return video from the first webcam on your computer. cap = cv2.VideoCapture(0) # Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480)) # loop runs if capturing has been initialized. while(True): # reads frames from a camera # ret checks return at each frame ret, frame = cap.read() # Converts to grayscale space, OCV reads colors as BGR # frame is converted to gray gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # output the frame out.write(gray) # The original input frame is shown in the window cv2.imshow('Original', frame) # The window showing the operated video stream cv2.imshow('frame', gray) # Wait for 'a' key to stop the program if cv2.waitKey(1) & 0xFF == ord('a'): break # Close the window / Release webcam cap.release() # After we release our webcam, we also release the out-out.release() # De-allocate any associated memory usage cv2.destroyAllWindows() |
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:
https://www.geeksforgeeks.org/python-visualizing-image-in-different-color-spaces/
References:
- https://docs.opencv.org/3.4/dd/d9e/classcv_1_1VideoWriter.html
- https://docs.opencv.org/3.1.0/dd/d43/tutorial_py_video_display.html
- https://en.wikipedia.org/wiki/FourCC
- https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html
Recommended Posts:
- Python - Displaying real time FPS at which webcam/video file is processed using OpenCV
- Saving a Video using OpenCV
- Face Detection using Python and OpenCV with webcam
- Live Webcam Drawing using OpenCV
- Detect the RGB color from a webcam using Python - OpenCV
- OpenCV | Saving an Image
- MoviePy –Saving Video File Clip
- MoviePy – Saving a Frame of Video Clip
- MoviePy – Saving Video File as GIF
- WebCam Motion Detector in Python
- OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
- Transition from OpenCV 2 to OpenCV 3.x
- Saving What Saves Our Passwords – Two-Factor Authentication
- Saving a Pandas Dataframe as a CSV
- Saving a file in Git
- Saving Text, JSON, and CSV to a File in Python
- ML - Saving a Deep Learning model in Keras
- PYGLET – Getting Directory for Saving User Paths
- Python | Create video using multiple images using OpenCV
- Python | Play a video in reverse mode using OpenCV
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.
Improved By : rishabhsingh1304


