Reputation: 311
I want to save the frames of video at certain time intervals using python opencv module.
I have to divide the video file into 40 images. But I do not think of algorithms.
My Idea is:
The way of Counting the num of frames, fps and jump interval:
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fps = cap.get(cv2.CAP_PROP_FPS)
After Counting the number of frame, fps and jump(Example):
D:\UCF-101\ApplyEyeMakeup\v_ApplyEyeMakeup_g01_c01.avi's FPS : 25.0
D:\UCF-101\ApplyEyeMakeup\v_ApplyEyeMakeup_g01_c01.avi's Length : 164
D:\UCF-101\ApplyEyeMakeup\v_ApplyEyeMakeup_g01_c01.avi's Running time : 6.56
D:\UCF-101\ApplyEyeMakeup\v_ApplyEyeMakeup_g01_c01.avi's jump : 4 ( 4.1 )
and here is while loop:
while count < length and save < 40:
print("Count : ", count)
success, frame = cap.read()
cv2.imshow('Window', frame)
if count % jump == 0:
cv2.imwrite(save_path + LabelList[LabelNumber] + "\\" + FileList[FileNumber] + "_" + str(count) + ".jpg", frame)
save = save + 1
print("Saved! : ", save)
cv2.waitKey(1)
count = count + 1
And I faced two problems:
Anyway, If you are interested in my problem, I will teach you in detail. I do not know what to say.
The important thing is that I want to save 40 images at regular intervals regardless of the length of the image.
Please help me bro...
Upvotes: 3
Views: 5501
Reputation: 31
IF YOU WANT TO GET FRAMES FOR PARTICULAR INTERVAL
import cv2
cap = cv2.VideoCapture("E:\\VID-20190124-WA0016.mp4")
count = 0
while cap.isOpened():
ret, frame = cap.read()
if ret:
cv2.imwrite('D:\\out2\\frame{:d}.jpg'.format(count), frame)
count += 30 # i.e. at 30 fps, this advances one second
cap.set(1, count)
else:
cap.release()
break
Upvotes: 0
Reputation: 1372
cap = cv2.VideoCapture(vidoname)
time_skips = float(2000) #skip every 2 seconds. You need to modify this
count = 0
success,image = cap.read()
while success:
cv2.imwrite("frame%d.jpg" % count, image)
cap.set(cv2.CAP_PROP_POS_MSEC,(count*time_skips)) # move the time
success,image = cap.read()
count += 1
# release after reading
cap.release()
Upvotes: 0
Reputation: 11232
There is not frame like 3.25 frame(there is just 3 frame, not float number)
If you make jump
a float, then just change your condition to count % jump < 1
. You will get uneven spacings between the frames, but should end up with 40 frames in each case.
A video with a total length of less than 30 frames.
Just set jump
to 1 if the number of frames is <= 40, and you will get all available frames.
Upvotes: 1