How to get the duration of video using cv2

前端 未结 5 1954
北恋
北恋 2021-01-03 19:24

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.

5条回答
  •  醉酒成梦
    2021-01-03 20:04

    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()
    

提交回复
热议问题