Python 3 OpenCV Script to Extract Frames of MP4 Video & Save It as PNG/JPEG Images
# Importing all necessary librariesimport cv2import os # Read the video from specified pathcam = cv2.VideoCapture("video.mp4") try:# creating a folder named dataif not os.path.exists('data'):os.makedirs('data') # if not created then raise errorexcept OSError:print ('Error: Creating directory of data') # framecurrentframe = 0 while(True):# reading from frameret,frame = cam.read() if ret:# if video is still left continue creating imagesname = './data/frame' + str(currentframe) + '.jpg'print ('Creating...' + name) # writing the extracted imagescv2.imwrite(name, frame) # increasing counter so that it will# show how many frames are createdcurrentframe += 1else:break # Release all space and windows once donecam.release()cv2.destroyAllWindows()