Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the duration of video using cv2

Tags:

python

video

cv2

I can only get the number of frames CAP_PROP_FRAME_COUNT using CV2.

However, I cannot find the parameter to get the duration of the video using cv2.

How to do that?

Thank you very much.

like image 508
Frankie Avatar asked Mar 01 '18 10:03

Frankie


People also ask

How do I get the length of a youtube video in Python?

Two ways to get a youtube video duration Corresponds to 6 minutes and 22 seconds. If needed, you can parse the ISO duration using duration = isodate. parse_duration(duration) and then video_dur = duration. total_seconds() with the isodate package.

How do you read a video on cv2?

To read a video with OpenCV, we can use the cv2. VideoCapture(filename, apiPreference) class. In the case where you want to read a video from a file, the first argument is the path to the video file (eg: my_videos/test_video. mp4).


1 Answers

In OpenCV 3, the solution is:

import cv2  cap = cv2.VideoCapture("./video.mp4") fps = cap.get(cv2.CAP_PROP_FPS)      # OpenCV2 version 2 used "CV_CAP_PROP_FPS" frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) duration = frame_count/fps  print('fps = ' + str(fps)) print('number of frames = ' + str(frame_count)) print('duration (S) = ' + str(duration)) minutes = int(duration/60) seconds = duration%60 print('duration (M:S) = ' + str(minutes) + ':' + str(seconds))  cap.release() 
like image 54
Ryan Loggerythm Avatar answered Sep 29 '22 14:09

Ryan Loggerythm