Something like this may help.
#include <iostream>
#include <opencv2/opencv.hpp> //for opencv3
#include <opencv/cv.hpp> //for opencv2
int main(int argc, const char * argv[]) {
cv::VideoCapture video("video.mp4");
double fps = video.get(cv::CAP_PROP_FPS);
std::cout << "Frames per second : " << fps << std::endl;
video.release();
return 0;
}
Answer from Murad on Stack Overflowalgorithm - How to find framerate of video using c++ opencv 2.4.10? - Stack Overflow
Python OpenCV video.get(cv2.CAP_PROP_FPS) returns 0.0 FPS - Stack Overflow
Capture speed / framerate of cv2.VideoCapture - Python - OpenCV
[Blog]: How to find frame rate or frames per second (fps) in OpenCV ( Python / C++ ) ?
Something like this may help.
#include <iostream>
#include <opencv2/opencv.hpp> //for opencv3
#include <opencv/cv.hpp> //for opencv2
int main(int argc, const char * argv[]) {
cv::VideoCapture video("video.mp4");
double fps = video.get(cv::CAP_PROP_FPS);
std::cout << "Frames per second : " << fps << std::endl;
video.release();
return 0;
}
You must have a loop in your code where you are doing all the video processing.
Let's say you have something similar to this pseudocode:
//code initialization
cv::VideoCapture cap("some-video-uri");
//video capture/processing loop
while (1)
{
//here we take the timestamp
auto start = std::chrono::system_clock::now();
//capture the frame
cap >> frame;
//do whatever frame processing you are doing...
do_frame_processing();
//measure timestamp again
auto end = std::chrono::system_clock::now();
//end - start is the time taken to process 1 frame, output it:
std::chrono::duration<double> diff = end-start;
std::cout << "Time to process last frame (seconds): " << diff.count()
<< " FPS: " << 1.0 / diff.count() << "\n";
}
thats it ... take into account that calculating FPS in a frame-per-frame basis will likely produce a highly variant result. You should average this result for several frames in order to get a less variant FPS.
Performing pip install python-opencv fixed the problem and the FPS is correctly detected.
EDIT: tested with python 3.8 and indeed it is pip install opencv-python. Cannot remember two years ago what python I was using.
EDIT November 2022: please also check Perry's answer below, if you are using a newer opencv-python version
The recent versions of opencv-python will give an error called AttributeError because cv2 doesn't have any attribute named cv.
Instead use the following
import cv2
vidcap = cv2.VideoCapture('some_video.avi')
fps = vidcap.get(cv2.CAP_PROP_FPS)
print(f"{fps} frames per second")
This will give the frames per second value
In this post, we will go over a useful trick in OpenCV where we find how to use OpenCV to find a common property - the frame per second for any video.
Dive in to https://www.learnopencv.com/how-to-find-frame-rate-or-frames-per-second-fps-in-opencv-python-cpp/ for more details and both the CPP and Python code!
Hi,
I'm using OpenCV with Python.
Is there a way to read frames from an existing Video File with an imposed FPS for the input stream?
Does VideoWriter flow behaves well with float point FPS values? I might only need this if I can't get input stream to accept an imposed FPS.
Thanks.
I've had a very limited experience with OpenCV. So, if the Question seems limited by my know-how of OpenCV.. I'd appreciate any knowledge related to this use case