Closed opencv-pushbot closed 5 months ago
I have the same question. There is a video of 2 minutes, but frame is None after 1 minutes.
Same issue
Any update on this? I am having this issue now.
Yes, I found a fix for this, you need to convert the file to another file type before reading. I was using this to make a deep learning model that checked how long an object was in motion for. I used another library called ffmpy. The whole file is n my time measurer repo but you should have no troubles figuring it out from the snippet. (odds are this wont run if you just copy paste since I was lazy with making sure you only had what you needed)
import cv2
import time
import ffmpy
#file = "test2.mp4"
def convert(inputted_file):
current_time = time.strftime("%Y%m%d-%H%M%S")
video_name = str(current_time) + ".avi"
ff = ffmpy.FFmpeg(inputs={inputted_file : None}, outputs={video_name: ' -c:a mp3 -c:v mpeg4'})
ff.cmd
ff.run()
return video_name
def getFrame(current_frame, move_frame, col_frame, cap):
cap.set(cv2.CAP_PROP_POS_MSEC,current_frame*1000)
hasFrames,image = cap.read()
return hasFrames
def checkTime(video_name):
cap = cv2.VideoCapture(video_name)
fps = cap.get(cv2.CAP_PROP_FPS)
print(fps)
frameRate = 0.01
move_frame = 0
col_frame = 0
current_frame = 0
count = 1
success = getFrame(current_frame, move_frame, col_frame, cap)
while success:
count = count + 1
current_frame = current_frame + frameRate
success, move_frame, col_frame = getFrame(current_frame, move_frame, col_frame, cap)
cap.release()
cv2.destroyAllWindows()
return move_frame, col_frame, fps
def tellTime(move_frame, col_frame, fps):
col_frame -= 0.01
seconds = ((col_frame - move_frame)*fps)
col_mins = seconds // 60
col_secs = seconds % 60
result_message = ("{:02}:{:02}".format(col_mins, col_secs))
print(result_message)
return result_message
#vname = convert(file)
#move_frame, col_frame = checkTime(vname)
#time = tellTime(move_frame, col_frame)
lazy solution: load your *.mp4 file with VLC media player. Then chose "Covert/Save" from Media menu and at the bottom of that dialog choose "Convert" from drop down. In the Convert dialog export it to MPEG4 format selected from the "Profile" drop down.
Works with zero code and VLC is the video playback king so who doesn't already have it installed? ;)
I have the same issue; the .read()
method is only reading about half the frames before returning None
when reading from .avi
file. Package version: opencv-python-headless v4.1.2.30
Transferred from http://code.opencv.org/issues/1938
Python cv2.VideoCapture.read() does not read all frames
History
Dmitry Kit on 2012-05-24 23:20