MathanKumar
MathanKumar

Reputation: 623

Stop video by its play time

I created a program to extract the video into frames.I need suggestion to stop the cv2.imshow() after when there is no more frames to show.

video frames count = 88

fps = 10.0

duration= int(8)


    def reading_video():
        read_input = cv2.VideoCapture(r"D:\data\input.mp4")
        frame_rate = int(read_input.get(cv2.CAP_PROP_FRAME_COUNT)) # 88
        print("total number of frames is ", frame_rate)
        fps = read_input.get(cv2.CAP_PROP_FPS) #10.0
        duration = int((frame_rate/fps) %60)# 8
        print(duration) # 8.0

        initial_frame = 0
        while read_input.isOpened():
            ret, frame = read_input.read()
            if ret == True:
                cv2.imshow('video_frame', frame)
                write_path = r"D:\data\frame_output"
                write_name = 'frame0' + str(initial_frame) + '.jpg'
                cv2.imwrite(os.path.join(write_path, write_name), frame)
                initial_frame += 1
                if cv2.waitKey(0) & 0xFF == ord('c'):
                    break
            else:
                break


    instance_variable = reading_video()
    instance_variable.release()
    cv2.destroyAllWindows()

If waitkey(int(duration)) then it played and after no frame i do getting cv2 Assertion error.

If waitkey(0) then i do getting AttributeError: 'NoneType' object has no attribute 'release'

Upvotes: 0

Views: 89

Answers (1)

furas
furas

Reputation: 142982

reading_video() doesn't uses return so it returns None and you have

instance_variable = None

and later

None.release()

Inside reading_video() you should use

return read_input

or maybe even directly

read_input.release() 

without instance_variable.release()

Upvotes: 1

Related Questions