Python - Extracting and Saving Video Frames
From here download this video so we have the same video file for the test. Make sure to have that mp4 file in the same directory of your python code. Then also make sure to run the python interpreter from the same directory.
Then modify the code, ditch waitKey
that's wasting time also without a window it cannot capture the keyboard events. Also we print the success
value to make sure it's reading the frames successfully.
import cv2vidcap = cv2.VideoCapture('big_buck_bunny_720p_5mb.mp4')success,image = vidcap.read()count = 0while success: cv2.imwrite("frame%d.jpg" % count, image) # save frame as JPEG file success,image = vidcap.read() print('Read a new frame: ', success) count += 1
How does that go?
To extend on this question (& answer by @user2700065) for a slightly different cases, if anyone does not want to extract every frame but wants to extract frame every one second. So a 1-minute video will give 60 frames(images).
import sysimport argparseimport cv2print(cv2.__version__)def extractImages(pathIn, pathOut): count = 0 vidcap = cv2.VideoCapture(pathIn) success,image = vidcap.read() success = True while success: vidcap.set(cv2.CAP_PROP_POS_MSEC,(count*1000)) # added this line success,image = vidcap.read() print ('Read a new frame: ', success) cv2.imwrite( pathOut + "\\frame%d.jpg" % count, image) # save frame as JPEG file count = count + 1if __name__=="__main__": a = argparse.ArgumentParser() a.add_argument("--pathIn", help="path to video") a.add_argument("--pathOut", help="path to images") args = a.parse_args() print(args) extractImages(args.pathIn, args.pathOut)
This is Function which will convert most of the video formats to number of frames there are in the video. It works on Python3
with OpenCV 3+
import cv2import timeimport osdef video_to_frames(input_loc, output_loc): """Function to extract frames from input video file and save them as separate frames in an output directory. Args: input_loc: Input video file. output_loc: Output directory to save the frames. Returns: None """ try: os.mkdir(output_loc) except OSError: pass # Log the time time_start = time.time() # Start capturing the feed cap = cv2.VideoCapture(input_loc) # Find the number of frames video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) - 1 print ("Number of frames: ", video_length) count = 0 print ("Converting video..\n") # Start converting the video while cap.isOpened(): # Extract the frame ret, frame = cap.read() if not ret: continue # Write the results back to output location. cv2.imwrite(output_loc + "/%#05d.jpg" % (count+1), frame) count = count + 1 # If there are no more frames left if (count > (video_length-1)): # Log the time again time_end = time.time() # Release the feed cap.release() # Print stats print ("Done extracting frames.\n%d frames extracted" % count) print ("It took %d seconds forconversion." % (time_end-time_start)) breakif __name__=="__main__": input_loc = '/path/to/video/00009.MTS' output_loc = '/path/to/output/frames/' video_to_frames(input_loc, output_loc)
It supports .mts
and normal files like .mp4
and .avi
. Tried and Tested on .mts
files. Works like a Charm.