Title: Count the total number of frames in a video with OpenCV and Python - PyImageSearch

URL Source: https://pyimagesearch.com/2017/01/09/count-the-total-number-of-frames-in-a-video-with-opencv-and-python/

Published Time: 2017-01-09T15:00:17+00:00

Markdown Content:
![Image 1: count_frames_trailer_capture](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20375'%3E%3C/svg%3E)

Today’s blog post is part of a two part series on working with video files using OpenCV and Python.

The first part of this series will focus on a question emailed in by PyImageSearch reader, Alex.

Alex asks:

> I need to count the total number of frames in a video file with OpenCV. The only way I’ve found to do this is to loop over each frame in the video file individually and increment a counter. Is there a faster way?

Great question Alex.

And rest assured, you’re not the first person who has asked me this question.

In the remainder of this blog post I’ll show you how to define a function that can **_quickly_** determine the total number of frames in a video file.

Next week we’ll use this function to aid us in a fun visualization task where I’ll demonstrate how to create “movie barcodes”. In order to generate these movie barcodes we’ll first need to know how many frames there are in our input movie file.

**To learn more about fast, efficient frame counting with OpenCV and Python, _just keep reading._**

![Image 2](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20128%20128'%3E%3C/svg%3E)

#### Looking for the source code to this post?

[Jump Right To The Downloads Section](#download-the-code)

There are two methods to determine the total number of frames in a video file using OpenCV and Python:

1.  **Method #1:** The fast, efficient way using the built-in properties OpenCV provides us to access the video file meta information and return the total number of frames.
2.  **Method #2:** The slow, inefficient technique that requires us to _manually_ loop over each frame and increment a counter for each frame we’ve read.

Method #1 is obviously ideal.

All we need to do is open a pointer to the video file, tell OpenCV which meta property we are interested, and get the returned value.

No looping over frames manually.

No wasted CPU cycles decoding frames.

…however, I’m sure you’ve realized there is a catch.

_**The problem here is that Method #1 is buggy as all hell based on your OpenCV version and video codecs installed.**_

You’ll find there are situations where _more than half_ of the `.get` and `.set` methods on video pointers _simply don’t work_. In situations like these we’ll inevitably have to revert to Method #2.

So, is there a way to encapsulate _both_ of these methods into a single function?

You bet there is.

I’ve already implemented the [count\_frames function inside the imutils library](https://github.com/jrosebr1/imutils/blob/master/imutils/video/count_frames.py), but to ensure you’re understanding what’s going on under the hood we’ll be reviewing the entire function here today.

### The easy way to count frames with OpenCV and Python

The first method to count video frames in OpenCV with Python is very fast — it simply uses the built-in properties OpenCV provides to access a video file and read the meta information of the video.

Let’s go ahead and see how this function is implemented [inside imutils](https://github.com/jrosebr1/imutils/blob/master/imutils/video/count_frames.py#L5) now:

\# import the necessary packages
from ..convenience import is\_cv3
import cv2

def count\_frames(path, override=False):
	# grab a pointer to the video file and initialize the total
	# number of frames read
	video = cv2.VideoCapture(path)
	total = 0

	# if the override flag is passed in, revert to the manual
	# method of counting frames
	if override:
		total = count\_frames\_manual(video)

To start, we import our necessary Python packages on **Lines 2 and 3**. We’ll need the `is_cv3` function to check which version of OpenCV we’re using along with `cv2` for our actual OpenCV bindings.

We define the `count_frames` function on **Line 5**. This method requires a single argument followed by a second optional one:

*   `path` : This is the path to where our video file resides on disk.
*   `override` : A boolean flag used to determine if we should skip Method #1 and go directly to the slower (but guaranteed accurate/error free) Method #2.

We make a call to `cv2.VideoCapture` on **Line 8** to obtain a pointer to the actual video file followed by initializing the `total` number of frames in the video.

We then make a check on **Line 13** to see if we should `override` . If so, we call `count_frames_manual` (which we’ll define in the next section).

Otherwise, let’s see how Method #1 is actually implemented:

	# otherwise, let's try the fast way first
	else:
		# lets try to determine the number of frames in a video
		# via video properties; this method can be very buggy
		# and might throw an error based on your OpenCV version
		# or may fail entirely based on your which video codecs
		# you have installed
		try:
			# check if we are using OpenCV 3
			if is\_cv3():
				total = int(video.get(cv2.CAP\_PROP\_FRAME\_COUNT))

			# otherwise, we are using OpenCV 2.4
			else:
				total = int(video.get(cv2.cv.CV\_CAP\_PROP\_FRAME\_COUNT))

		# uh-oh, we got an error -- revert to counting manually
		except:
			total = count\_frames\_manual(video)

	# release the video file pointer
	video.release()

	# return the total number of frames in the video
	return total

In order to determine the number of frames in a video file via the API by provided by OpenCV, we need to utilize what are called _capture_ _properties_, or what OpenCV calls `CAP_PROP` — anytime you see a constant starting with `CAP_PROP_*` , you should know it’s related to video processing.

In _**OpenCV 3**_ the name of the frame count property is `cv2.CAP_PROP_FRAME_COUNT` while in _**OpenCV 2.4**_ the property is named `cv2.cv.CV_CAP_PROP_FRAME_COUNT` .

Ideally, passing the respective property name into the `.get` method of the `video` pointer will allow us to obtain the total number of frames in the video (**Lines 25-30**).

However, there are cases where this method will fail based on your particular OpenCV install and video codecs.

If this is the case, we have wrapped our critical code section with a `try/except` block. If an exception occurs we simply revert to counting the frames manually (**Lines 33 and 34**).

Finally, we release the video file pointer (**Line 37**) and return the total number of frames to the calling function (**Line 40**).

### The slow way to count frames with OpenCV and Python

We’ve seen the fast, efficient method to counting frames in a video — let’s now move on to the slower method called `count_frames_manual` .

\# import the necessary packages
from ..convenience import is\_cv3
import cv2

def count\_frames(path, override=False):
	# grab a pointer to the video file and initialize the total
	# number of frames read
	video = cv2.VideoCapture(path)
	total = 0

	# if the override flag is passed in, revert to the manual
	# method of counting frames
	if override:
		total = count\_frames\_manual(video)

	# otherwise, let's try the fast way first
	else:
		# lets try to determine the number of frames in a video
		# via video properties; this method can be very buggy
		# and might throw an error based on your OpenCV version
		# or may fail entirely based on your which video codecs
		# you have installed
		try:
			# check if we are using OpenCV 3
			if is\_cv3():
				total = int(video.get(cv2.CAP\_PROP\_FRAME\_COUNT))

			# otherwise, we are using OpenCV 2.4
			else:
				total = int(video.get(cv2.cv.CV\_CAP\_PROP\_FRAME\_COUNT))

		# uh-oh, we got an error -- revert to counting manually
		except:
			total = count\_frames\_manual(video)

	# release the video file pointer
	video.release()

	# return the total number of frames in the video
	return total

def count\_frames\_manual(video):
	# initialize the total number of frames read
	total = 0

	# loop over the frames of the video
	while True:
		# grab the current frame
		(grabbed, frame) = video.read()
	 
		# check to see if we have reached the end of the
		# video
		if not grabbed:
			break

		# increment the total number of frames read
		total += 1

	# return the total number of frames in the video file
	return total

As we can see, `count_frames_manual` requires only a single argument, `video` , which we assume to be a pointer instantiated by `cv2.VideoCapture` .

We then initialize the total number of frames read from the `video` file, loop over the frames until we have reached the end of the video, and increment the `total` counter along the way.

The `total` is then returned to the calling function.

It’s worth mentioning that this method is totally accurate and error free. If you _do_ get an error it’s almost certainly related to a problem with your video codecs or an invalid path to a video file.

There also might be times when using this function you get a total of _zero_ frames returned. When this happens it’s 99% likely that:

1.  You supplied an invalid path to `cv2.VideoCapture` .
2.  You don’t have the proper video codecs installed and thus OpenCV cannot read the file. If this is the case you’ll need to install the proper video codecs, followed by re-compiling and re-installing OpenCV.

### Counting frames in video files with OpenCV

Let’s go ahead and take our `count_frames` method for a test drive.

To start, make sure you have installed the [imutils library](https://github.com/jrosebr1/imutils):

$ pip install imutils

Otherwise, if you already have `imutils` installed you should update to the latest version (_\> v0.3.9_):

$ pip install --upgrade imutils

From there, let’s create our driver script named `frame_counter.py` :

\# import the necessary packages
from imutils.video import count\_frames
import argparse
import os

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add\_argument("-v", "--video", required=True,
	help="path to input video file")
ap.add\_argument("-o", "--override", type=int, default=-1,
	help="whether to force manual frame count")
args = vars(ap.parse\_args())

We start by importing our required Python packages and parsing our command line arguments. We’ll require one switch followed by a second optional one:

*   `--video` : The path to our input video file.
*   `--override` : Whether or not to force a manual frame count. We’ll try to use the faster Method #1 by default.

We then make a call to `count_frames` and display the result to our screen:

\# count the total number of frames in the video file
override = False if args\["override"\] < 0 else True
total = count\_frames(args\["video"\], override=override)

# display the frame count to the terminal
print("\[INFO\] {:,} total frames read from {}".format(total,
	args\["video"\]\[args\["video"\].rfind(os.path.sep) + 1:\]))

To test out this Python script, I’ve decided to use the trailer from my favorite movie, _Jurassic Park:_

How many frames do you think are in this video clip?

500?

5,000?

50,000?

Take your best guess and then let’s execute our Python script to find out.

_**Note:** I used the website [keepvid.com](http://keepvid.com/) to download a .mp4 of the trailer. I do not own the copyrights to this movie or trailer. This script is only for demonstration and educational purposes. Please use responsibly._

To test out the _fast_ method, execute the following command:

$ time python frame\_counter.py --video videos/jurassic\_park\_trailer.mp4
\[INFO\] 4,790 total frames read from jurassic\_park\_trailer.mp4

real	0m0.803s
user	0m0.176s
sys	0m0.112s

![Image 3: Figure 1: Determining the total number of frames in a video file using OpenCV properties.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20372'%3E%3C/svg%3E)

**Figure 1:** Determining the total number of frames in a video file using OpenCV properties.

As my results demonstrate, it takes 0.803s to determine there are a total of 4,790 frames in the video file.

We can test out the slower method using this command:

$ time python frame\_counter.py --video videos/jurassic\_park\_trailer.mp4 --override 1
\[INFO\] 4,978 total frames read from jurassic\_park\_trailer.mp4

real	1m55.289s
user	0m34.833s
sys	0m7.988s

![Image 4: Figure 2: Manually counting the total number of frames in a video file using OpenCV and Python.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20372'%3E%3C/svg%3E)

**Figure 2:** Manually counting the total number of frames in a video file using OpenCV and Python.

Here we see that it takes _**1m55s**_ (an increase of approximately 14,221%) to return a frame count of _**4,978**_ which differs Method #1 by **_188 frames_**.

Why the discrepancy in frame counts?

It all comes down to _fast and approximate_ versus _slow but accurate_.

Using OpenCV’s video capture properties we get a total frame count _very fast_, but it might not be 100% dead on. We also have the potential of this method failing entirely due to OpenCV/video codec versioning.

On the other hand, if we _manually_ count the number of frames it will take us a long time, but the total number of returned frames will be _exact_.

In situations where you absolutely, positively, must have the _exact_ count, go with Method #2.

If you need a rough approximation, go with Method #1 (unless it fails, then you’ll be reverting back to Method #2 anyway).

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

Summary
-------

In this blog post I demonstrated two methods to count the number of frames in a video file using OpenCV and Python.

The first method is _super quick_, relying on OpenCV’s video property functionality, allowing us to (nearly) instantaneously determine the number of frames in a video file.

However, this method is quite buggy, is prone to failure (based on your OpenCV + video codec versions), and may even return nonsensical results.

In that case, we need to revert to our second method: manually counting the total number of frames in a video. While excruciatingly slow, this method has the advantage of being _100%_ _accurate_.

In order to balance speed versus potential failure I have created the `count_frames`  function and placed it inside the [imutils library](https://github.com/jrosebr1/imutils).

This function will attempt Method #1 first, and if it fails, automatically revert to Method #2.

In next week’s blog post we’ll be using this function to aid us in generating and visualizing video barcodes.

See you next week!

**To be notified when the next blog posts goes live, _be sure to enter your email address in the form below!_**

![Image 5](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Download the Source Code and FREE 17-page Resource Guide

Enter your email address below to get a .zip of the code and a **FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning.** Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL!