Reputation: 3877
I am trying to count total number of Frames in my video file ('foo.h264').
>>> import numpy as nm
>>> import cv2
>>> cap = cv2.VideoCapture('foo.h264')
>>> cap.get(CV_CAP_PROP_FRAME_COUNT)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'CV_CAP_PROP_FRAME_COUNT' is not defined
>>> cap.get(5)
25.0
>>> cap.get(7)
-192153584101141.0
So I think get(5)
is giving frame rate and get(7)
gives total number of frames. Obviously get(7)
is incorrect in the above case. So to verify I tried to find these values in an .avi
file.
>>> cap = cv2.VideoCapture('foo.avi')
>>> cap.get(5)
29.97002997002997
>>> cap.get(7)
256379.0
I can calculate the total number of frames by multiplying FPS
by duration of the video, but I'm not sure if the FPS given for .h264
is right or not. Why does it give negative number of total frames? Is this a bug?
P.S: I recorded this video file (.h264
) using raspberry pi camera.
Upvotes: 5
Views: 10580
Reputation: 552
This worked for me (no opencv used):
import imageio
file="video.mp4" #the path of the video
vid=imageio.get_reader(file, 'ffmpeg')
totalframes = vid.count_frames()
print(totalframes)
It will return the total frames :)
Upvotes: 4
Reputation: 51
Another solution is using imageio, which works for some videos.
import imageio
filename="person15_walking_d1_uncomp.avi"
vid = imageio.get_reader(filename, 'ffmpeg')
# number of frames in video
num_frames=vid._meta['nframes']
Upvotes: 5
Reputation: 1018
As it turns out OpenCV does not support h.264 format (Link). However, the documentation on Video Capture at Python OpenCV documentation mentions an integer argument for the get command. So, you're right on that count on using 5 and 7 instead of 'CV_CAP_PROP_FRAME_COUNT'. You could try changing the capture format on the raspberry pi to avi.
Upvotes: 1