Saving a Video using OpenCV
OpenCV is an open-source and most popular computer vision library that contains several computer vision algorithms. You can read, display, write and doing lots of other operations on images and videos using OpenCV. The OpenCV module generally used in popular programming languages like C++, Python, Java. To save a video in OpenCV cv.VideoWriter()
method is used.
Note: For more information, refer to Introduction to OpenCV.
Syntax: cv.VideoWriter(filename, fourcc, fps, frameSize)
Parameters:
filename: Input video file
fourcc: 4-character code of codec used to compress the frames
fps: framerate of videostream
framesize: Height and width of frame
Example:
import cv2
video = cv2.VideoCapture( 0 )
if (video.isOpened() = = False ):
print ( "Error reading video file" )
frame_width = int (video.get( 3 ))
frame_height = int (video.get( 4 ))
size = (frame_width, frame_height)
result = cv2.VideoWriter( 'filename.avi' ,
cv2.VideoWriter_fourcc( * 'MJPG' ),
10 , size)
while ( True ):
ret, frame = video.read()
if ret = = True :
result.write(frame)
cv2.imshow( 'Frame' , frame)
if cv2.waitKey( 1 ) & 0xFF = = ord ( 's' ):
break
else :
break
video.release()
result.release()
cv2.destroyAllWindows()
print ( "The video was successfully saved" )
|
Output:
Last Updated :
03 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...