Extract a frame every second in Python
What I want to do is to put a video as input and extract from it a frame every second keeping the original size and saving all the frames into a folder.
What I want to do is to put a video as input and extract from it a frame every second keeping the original size and saving all the frames into a folder.
import cv2
import math
videoFile = "capture.avi"
imagesFolder = "/other/images"
cap = cv2.VideoCapture(videoFile)
frameRate = cap.get(5) #frame rate
while(cap.isOpened()):
frameId = cap.get(1) #current frame number
ret, frame = cap.read()
if (ret != True):
break
if (frameId % math.floor(frameRate) == 0):
filename = imagesFolder + "/image_" + str(int(frameId)) + ".jpg"
cv2.imwrite(filename, frame)
cap.release()
print "Done!"
Images are not stored in folder specified. And if I remove the path to folder than it save all images in the Presented working directory. Please tell me how to give path to extracted images?
import cv2 import math
videoFile = "video.mp4"
cap = cv2.VideoCapture(videoFile)
frameRate = cap.get(5) #frame rate
x=1
while(cap.isOpened()):
frameId = cap.get(1) #current frame number
ret, frame = cap.read()
if (ret != True):
break
if (frameId % math.floor(frameRate) == 0):
filename = './test_images/image' + str(int(x)) + ".jpg";x+=1
cv2.imwrite(filename, frame)
cap.release()
print ("Done!")
@coderI It is saving the last frame every second. If frame rate is 30 fps and x = 30 (it is the 30th frame), it saves the image, one second later after another 30 frames, it will once again save the frame. If it were not conditional, it would try to write as many files per second as the stream's fps which generally would clog the pipeline and hurt throughput.
Asked: 2015-05-16 09:25:37 -0600
Seen: 40,714 times
Last updated: May 16 '15