get video duration opencv python
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()
Here is what the above code is Doing:
1. We create a VideoCapture object. Its argument can be either the name of a video file (including its path) or an integer representing the index of a webcam attached to your computer.
2. We get the number of frames per second (fps).
3. We get the total number of frames in the video.
4. We calculate the duration of the video (in seconds).
5. We calculate the duration of the video (in minutes and seconds).
6. We release the VideoCapture object.