So I've followed this tutorial but this doesn't seem to do anything. Simply nothing. It waits a few seconds and closes the program. What is wrong with this code?
import cv2
vidcap = cv2.VideoCapture('Compton.mp4')
success,image = vidcap.read()
count = 0
success = True
while success:
success,image = vidcap.read()
cv2.imwrite("frame%d.jpg" % count, image) # save frame as JPEG file
if cv2.waitKey(10) == 27: # exit if Escape is hit
break
count += 1
Also, in the comments it says that this limits the frames to 1000? Why?
EDIT:
I tried doing success = True
first but that didn't help. It only created one image that was 0 bytes.
This is Function which will convert most of the video formats to number of frames there are in the video. It works on
Python3
withOpenCV 3+
It supports
.mts
and normal files like.mp4
and.avi
. Tried and Tested on.mts
files. Works like a Charm.This function extracts images from video with 1 fps, IN ADDITION it identifies the last frame and stops reading also:
This is a tweak from previous answer for python 3.x from @GShocked, I would post it to the comment, but dont have enough reputation
The previous answers have lost the first frame. And it will be nice to store the images in a folder.
By the way, you can check the frame rate by VLC. Go to windows -> media information -> codec details
To extend on this question (& answer by @user2700065) for slightly different case, if anyone does not want to extract every frame but wants to extract frame every one second. So 1 minute video will give 60 frames(images).
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 thesuccess
value to make sure it's reading the frames successfully.How does that go?