Created
June 16, 2020 00:12
-
-
Save miracleyoo/8d19a043b1245a69ef542b4ed9f27841 to your computer and use it in GitHub Desktop.
[Video Frame Count] #python #video
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
def frame_count(video_path, manual=False): | |
def manual_count(handler): | |
frames = 0 | |
while True: | |
status, frame = handler.read() | |
if not status: | |
break | |
frames += 1 | |
return frames | |
cap = cv2.VideoCapture(video_path) | |
# Slow, inefficient but 100% accurate method | |
if manual: | |
frames = manual_count(cap) | |
# Fast, efficient but inaccurate method | |
else: | |
try: | |
frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) | |
except: | |
frames = manual_count(cap) | |
cap.release() | |
return frames |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment