Home > Net >  how much miliseconds to put in waitkey in opencv python
how much miliseconds to put in waitkey in opencv python

Time:03-03

I want to display a video in python with OpenCV, I know how to do that but this is how I do it

import cv2

cap = cv2.VideoCapture("Resorces/test.mp4")

while True:
    success, img = cap.read()
    cv2.imshow("video", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

but the issue is on the 2last line there is a cv2.waitKey there I have inserted 1milesecond but the video's length is 30 seconds by doing this the video is very fast and it ends very quickly

so I want the video to be 30 seconds so what should I put there

this is my video https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_640_3MG.mp4

Please help me thanks

CodePudding user response:

use fps = cap.get(cv2.CAP_PROP_FPS) to get the frames per second & then you can divide 1000 / fps that is 1000 ms per second & convert to int to get the frame delay to get a realistic playback.

Note: there is some overhead reading in an image so this is just approximate but in practice I found is good enough as long as you aren't doing any heavy processing in the while loop.

Full code listing,

import cv2

cap       = cv2.VideoCapture("Resorces/test.mp4")
fps       = cap.get(cv2.CAP_PROP_FPS)
frm_delay = int(1000 / fps)

while True:
    success, img = cap.read()
    cv2.imshow("video", img)
    if cv2.waitKey(frm_delay) & 0xFF == ord('q'):
        break
  • Related