Python - Extracting and Saving Video Frames
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.
So here was the final code that worked:
import cv2 print(cv2.__version__) vidcap = cv2.VideoCapture('big_buck_bunny_720p_5mb.mp4') success,image = vidcap.read() count = 0 success = True while 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
So for this to work, you'll have to get some stuff. First, download OpenCV2. Then install this for Python 2.7.x. Go to the ffmpeg folder inside the 3rd party folder (something like
C:\OpenCV\3rdparty\ffmpeg, but i'm not sure). Copy
opencv_ffmpeg.dll (or the x64 version if your python version is x64) and paste it into your Python folder (probably
C:\Python27). Rename it
opencv_ffmpeg300.dll if your opencv version is 3.0.0 (you can find it here), and change accordingly to your version. Btw, you must have your python folder in your environment path.