Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python and OpenCV - getting the duration time of a video at certain points

Let say I have made a program to detect a green ball in a video. Whenever there is a green ball detected, I want to print out the duration of video at the time the green ball is detected. Is it possible?

like image 299
Hafiz Hilman Mohammad Sofian Avatar asked Jan 24 '26 09:01

Hafiz Hilman Mohammad Sofian


2 Answers

In this answer, you will find a solution to determine the frames per second.

so you'd want to use:

fps = cap.get(cv2.cv.CV_CAP_PROP_FPS)

and count the number of frames you're at. Then you can compute the video time with

videotime = current_frame_number / fps.

EDIT:

@Miki suggested to use CAP_PROP_POS_MSEC which should result in the same time (in [ms])

Corrected my typo as pointed out by @Swiper-CCCVI

like image 118
DomTomCat Avatar answered Jan 27 '26 01:01

DomTomCat


You can simply measure a certain position in the video in milliseconds using

time_milli = cap.get(cv2.CAP_PROP_POS_MSEC)

and then divide time_milli by 1000 to get the time in seconds.

like image 20
Krishna Ojha Avatar answered Jan 27 '26 01:01

Krishna Ojha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!