2021-07-29

How to close OpenCV automatically after the video finishes playing?

I need help. I want whenever my program reaches the end of the video, I want it to be closed automatically without looping for forever.

I've tried several methods (using ret) but they don't work. I'm not sure why it always went wrong other than I could've probably written them in the wrong scope.

I also need to record the elapsed time difference from the time I opened the video to the time the video closes (using time.time()). But, this code I wrote below didn't print out the time difference whenever I force closed the OpenCV window.

This is the rough structure of my code (not the full code, I only show the important ones):

import stuff

cap = cv2.VideoCapture('file.mp4')

def main():
    start = time.time()
    
    out = cv2.VideoWriter('newvid.mp4')

    while True:
        #Stuff I do on the video in each frame
        video = cap.copy()

        cv2.imshow('video', video)
        out.write(video)

        if cv2.waitKey(33) == 27:
            break

    end = time.time()
    print(end - start)

    cv2.destroyAllWindows()

if __name__ = '__main__':
    main()

Can anyone help me and tell me the solution?



from Recent Questions - Stack Overflow https://ift.tt/3BQ4vEv
https://ift.tt/eA8V8J

No comments:

Post a Comment