0

I try to get the first, the middle, and the last frame from the video but it can only get the first and the middle one.

This is my code:

import cv2
import math

vidcap = cv2.VideoCapture("/home/data/input/video.MOV")

# Get number of frame of the video
length = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
print(length)

# Define a tuple of frame need to get
frame = (0, math.ceil(length/2), length - 1)

# Get the frame
for f in frame:
    vidcap.set(1, f)
    _, image = vidcap.read()
    cv2.imwrite("/home/data/output/frame%d.jpg" % f, image)

It does save the last frame image but it doesn't have any data.

Is there anything wrong with my code?

Update:

According to this post, it seems like cv2.CAP_PROP_FRAME_COUNT can count wrong the number of frames.

So is there a better way to get the number of frames without reading through the entire frame?

huy
  • 1,648
  • 3
  • 14
  • 40

1 Answers1

0
vidcap = cv2.VideoCapture("/home/data/input/video.MOV")
count = 1

while 1:
    ret , frame = vidcap.read()
    count += 1
    if ret == False:
       break

print(count)

There can be typing errors, but you can get the idea.

  • This one we have to read through the video, I just want to find a way not to do that. Anyway, thanks for your answer. – huy Jun 26 '20 at 15:57
  • okay, you can use video.get(cap.prob.fps) and use the minus 100 of this value , so after first get the exactly value of last frame, on the other while loop you can stop the loop in this number and get the frame, this way will decrease the processing time – Mesut Şimşek Jun 27 '20 at 15:34