Skip to content

Instantly share code, notes, and snippets.

@miracleyoo
Created June 16, 2020 00:12
Show Gist options
  • Save miracleyoo/8d19a043b1245a69ef542b4ed9f27841 to your computer and use it in GitHub Desktop.
Save miracleyoo/8d19a043b1245a69ef542b4ed9f27841 to your computer and use it in GitHub Desktop.
[Video Frame Count] #python #video
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