Setting a frame rate doesn't always work like you expect. It depends on two things:

  1. What your camera is capable of outputting.
  2. Whether the current capture backend you're using supports changing frame rates.

So point (1). Your camera will have a list of formats which it is capable of delivering to a capture device (e.g. your computer). This might be 1920x1080 @ 30 fps or 1920x1080 @ 60 fps and it also specifies a pixel format. The vast majority of consumer cameras do not let you change their frame rates with any more granularity than that. And most capture libraries will refuse to change to a capture format that the camera isn't advertising.

Even machine vision cameras, which allow you much more control, typically only offer a selection of frame rates (e.g. 1, 2, 5, 10, 15, 25, 30, etc). If you want a non-supported frame rate at a hardware level, usually the only way to do it is to use hardware triggering.

And point (2). When you use cv.VideoCapture you're really calling a platform-specific library like DirectShow or V4L2. We call this a backend. You can specify exactly which backend is in use by using something like:

cv2.VideoCapture(0 + cv2.CAP_DSHOW)

There are lots of CAP_X's defined, but only some will apply to your platform (e.g CAP_V4L2 is for Linux only). On Windows, forcing the system to use DirectShow is a pretty good bet. However as above, if your camera only reports it can output 30fps and 60fps, requesting 10fps will be meaningless. Worse, a lot of settings simply report True in OpenCV when they're not actually implemented. You've seen that most of the time reading parameters will give you sensible results though, however if the parameter isn't implemented (e.g exposure is a common one that isn't) then you might get nonsense.

You're better off waiting for a period of time and then reading the last image.

Be careful with this strategy. Don't do this:

while capturing:
    res, image = cap.read()
    time.sleep(1)

you need to make sure you're continually purging the camera's frame buffer otherwise you will start to see lag in your videos. Something like the following should work:

frame_rate = 10
prev = 0

while capturing:

    time_elapsed = time.time() - prev
    res, image = cap.read()

    if time_elapsed > 1./frame_rate:
        prev = time.time()

        # Do something with your image here.
        process_image()

For an application like a hand detector, what works well is to have a thread capturing images and the detector running in another thread (which also controls the GUI). Your detector pulls the last image captured, runs and display the results (you may need to lock access to the image buffer while you're reading/writing it). That way your bottleneck is the detector, not the performance of the camera.

Answer from Josh on Stack Overflow
🌐
OpenCV
forum.opencv.org › c++
How to set camera fps with opencv - C++ - OpenCV
March 10, 2022 - Hi, I want to set camera fps with opencv. I don’t know if using cv::waitKey() is good idea to set it. For example I have video 10fps and when I get the fps I have only 9fps. I want to set the camera to for example 30f…
🌐
LearnOpenCV
learnopencv.com › home › find frame rate (frames per second-fps) in opencv (python/c++)
Find frame rate (frames per second-fps) in OpenCV (Python/C++)
July 31, 2026 - Calling video.get(cv2.CAP_PROP_FPS) in Python or video.get(cv::CAP_PROP_FPS) in C++ asks the backend for its FPS property. A video-file backend commonly derives this value from container or stream metadata.
Discussions

c++ - How to set camera fps in opencv? - Stack Overflow
I'm using a webcam supporting 1280 x 720 @ 60 fps. My computer environment is intel i5-4690K and Windows7, Visual studio 2015, opencv 3.1 When I run the webcam in Kinovea(0.85.15, https://www.kin... More on stackoverflow.com
🌐 stackoverflow.com
python - Changing FPS in OpenCV - Stack Overflow
You set the FPS for the output ... to set the FPS for the input via the VideoCapture. In order to do that you can try to call cv2.VideoCapture, with the cv2.CAP_PROP_FPS property after you create cap. For example: ... However - note that the actual behavior is dependant on the specific capture device you are using. Some support only certain FPSs. See also this post regarding it: change frame rate in opencv ... More on stackoverflow.com
🌐 stackoverflow.com
How to modify the FPS of a video when using cv::VideoCapture to read frames - C++ - OpenCV
Using cv::VideoCapture to process videos. Wondering what the technique is for changing the frame rate when I write out a new video. I assume I could do something simple like drop every 2nd frame to cut from 60 FPS to 30 FPS. But is that the right way to do it? More on forum.opencv.org
🌐 forum.opencv.org
0
May 19, 2021
Increase Camera FPS OpenCV python
Currently we set FPS and then MJPG. I’d like to try swapping those. If that doesn’t work, we can try compiling from source." ... Highly recommend you use cscore (pip install robotpy-cscore) for image capture instead of OpenCV’s builtin stuff, it works a lot better. More on chiefdelphi.com
🌐 chiefdelphi.com
1
0
February 14, 2024
Top answer
1 of 6
47

Setting a frame rate doesn't always work like you expect. It depends on two things:

  1. What your camera is capable of outputting.
  2. Whether the current capture backend you're using supports changing frame rates.

So point (1). Your camera will have a list of formats which it is capable of delivering to a capture device (e.g. your computer). This might be 1920x1080 @ 30 fps or 1920x1080 @ 60 fps and it also specifies a pixel format. The vast majority of consumer cameras do not let you change their frame rates with any more granularity than that. And most capture libraries will refuse to change to a capture format that the camera isn't advertising.

Even machine vision cameras, which allow you much more control, typically only offer a selection of frame rates (e.g. 1, 2, 5, 10, 15, 25, 30, etc). If you want a non-supported frame rate at a hardware level, usually the only way to do it is to use hardware triggering.

And point (2). When you use cv.VideoCapture you're really calling a platform-specific library like DirectShow or V4L2. We call this a backend. You can specify exactly which backend is in use by using something like:

cv2.VideoCapture(0 + cv2.CAP_DSHOW)

There are lots of CAP_X's defined, but only some will apply to your platform (e.g CAP_V4L2 is for Linux only). On Windows, forcing the system to use DirectShow is a pretty good bet. However as above, if your camera only reports it can output 30fps and 60fps, requesting 10fps will be meaningless. Worse, a lot of settings simply report True in OpenCV when they're not actually implemented. You've seen that most of the time reading parameters will give you sensible results though, however if the parameter isn't implemented (e.g exposure is a common one that isn't) then you might get nonsense.

You're better off waiting for a period of time and then reading the last image.

Be careful with this strategy. Don't do this:

while capturing:
    res, image = cap.read()
    time.sleep(1)

you need to make sure you're continually purging the camera's frame buffer otherwise you will start to see lag in your videos. Something like the following should work:

frame_rate = 10
prev = 0

while capturing:

    time_elapsed = time.time() - prev
    res, image = cap.read()

    if time_elapsed > 1./frame_rate:
        prev = time.time()

        # Do something with your image here.
        process_image()

For an application like a hand detector, what works well is to have a thread capturing images and the detector running in another thread (which also controls the GUI). Your detector pulls the last image captured, runs and display the results (you may need to lock access to the image buffer while you're reading/writing it). That way your bottleneck is the detector, not the performance of the camera.

2 of 6
7

I could not set the FPS for my camera so I manage to limit the FPS based on time so that only 1 frame per second made it into the rest of my code. It is not exact, but I do not need exact, just a limiter instead of 30fps. HTH

import time

fpsLimit = 1 # throttle limit
startTime = time.time()
cv = cv2.VideoCapture(0)
While True:
    frame = cv.read
    nowTime = time.time()
    if (int(nowTime - startTime)) > fpsLimit:
        # do other cv2 stuff....
        startTime = time.time() # reset time
🌐
OpenCV Q&A Forum
answers.opencv.org › question › 214375 › changing-the-fps-of-a-video
changing the fps of a video? - OpenCV Q&A Forum
You are using two different flags cv2.CV_CAP_PROP_FPS and cv2.CAP_PROP_FPS. The correct flag is cv2.CAP_PROP_FPS and change it to so when setting your cap.
🌐
PyImageSearch
pyimagesearch.com › home › blog › faster video file fps with cv2.videocapture and opencv
Faster video file FPS with cv2.VideoCapture and OpenCV - PyImageSearch
April 17, 2021 - But before we can appreciate the faster, threaded method to video frame processing, we first need to set a benchmark/baseline with the slower, non-threaded version. The goal of this section is to obtain a baseline on our video frame processing throughput rate using OpenCV and Python. To start, open up a new file, name it read_frames_slow.py , and insert the following code: # import the necessary packages from imutils.video import FPS import numpy as np import argparse import imutils import cv2 # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-v", "--video", required=True, help="path to input video file") args = vars(ap.parse_args()) # open a pointer to the video stream and start the FPS timer stream = cv2.VideoCapture(args["video"]) fps = FPS().start()
Top answer
1 of 2
2

You set the FPS for the output via the VideoWriter,
but you didn't attempt to set the FPS for the input via the VideoCapture.

In order to do that you can try to call cv2.VideoCapture, with the cv2.CAP_PROP_FPS property after you create cap.
For example:

cap.set(cv2.CAP_PROP_FPS, 3)

However - note that the actual behavior is dependant on the specific capture device you are using. Some support only certain FPSs. See also this post regarding it: change frame rate in opencv 3.4.2.

If it does work you will be able to simplify your code a lot - just capture frames, process them and save (without any manual fps management).

2 of 2
1

This method programmatically sets frames per second. A 6.1mb file was created when frame rate was set for 30fps, and a 0.9mb file when set for 3fps.

#!/usr/bin/env python3

import cv2
import numpy as np
import time
import datetime
import pathlib
import imutils

cap = cv2.VideoCapture(0)

if (cap.isOpened() == False): 
  print("Unable to read camera feed")

capture_duration = 15
frame_per_sec = 30
prev = 0
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
out = cv2.VideoWriter('C:\\videoPy\\LZ\\'outpout.avi',cv2.VideoWriter_fourcc('m','j','p','g'),frame_per_sec, (frame_width,frame_height))
start_time = time.time()
while( int(time.time() - start_time) < capture_duration ): 
#start fps
  time_elapsed = time.time() - prev
  while(time_elapsed > 1./frame_per_sec):                   
        ret, frame = cap.read()
        if not ret:
            break
        if time_elapsed > 1./frame_per_sec:
          prev = time.time()
#end fps
        if ret==True:
          frame = imutils.resize(frame, width=frame_width)
          out.write(frame)
          cv2.imshow('frame',frame) 
        if cv2.waitKey(1) & 0xFF == ord('q'):
          break
        else:
           break

cap.release()
out.release()
 
cv2.destroyAllWindows()
Find elsewhere
🌐
OpenCV
forum.opencv.org › c++
How to modify the FPS of a video when using cv::VideoCapture to read frames - C++ - OpenCV
May 19, 2021 - Using cv::VideoCapture to process videos. Wondering what the technique is for changing the frame rate when I write out a new video. I assume I could do something simple like drop every 2nd frame to cut from 60 FPS to 3…
🌐
Chief Delphi
chiefdelphi.com › technical
Increase Camera FPS OpenCV python - Technical - Chief Delphi
February 14, 2024 - My team is trying to increase the camera fps using built in opencv python functions. The cameras should be able to get 60fps, but we are currently getting only 10fps sadly. This is what we’ve tried so far: while True: cap = cv.VideoCapture(path) cap:cv.VideoCapture cap.set(cv.CAP_PROP_FRAME_WIDTH, 1280) cap.set(cv.CAP_PROP_FRAME_HEIGHT, 720) cap.set(cv.CAP_PROP_FPS, 30) cap.set(cv.CAP_PROP_FOURCC, cv.VideoWriter_fourcc('M', 'J', 'P', 'G')) if cap.isOpened(): print("open") return cap else: sleep(0.001)
🌐
Quora
quora.com › How-do-I-decrease-the-frames-per-second-in-OpenCV-python
How to decrease the frames per second in OpenCV python - Quora
Choose the method that fits your ... capture (skip frames), or hardware-configured FPS (cap.set). ... Sales and solutions for every budget. Call now for a free onsite evaluation! ... Changing fps through opencv using waitkey will not decrease actual frames....
🌐
YouTube
youtube.com › watch
How to add FPS to video and live in Opencv Python | Calculate frames per second - YouTube
How to calculate and add FPS to video in opencv python is shown#opencv
Published: October 21, 2022
🌐
OpenCV Q&A Forum
answers.opencv.org › question › 99998 › getting-the-correct-frame-rate
Getting the correct frame rate - OpenCV Q&A Forum
#include <opencv2/highgui.hpp> #include <opencv2/imgproc.hpp> #include <ctime> #include <windows.h> using namespace cv; int main(int argc, char** argv) { char* filename = argc >= 2 ? argv[1] : (char*)"c:/768x576.avi"; VideoCapture capture(filename); // open video file if (!capture.isOpened()) // check if we succeeded return -1; int fps_of_video = (int)capture.get(CAP_PROP_FPS); int time_to_wait = 1000 / fps_of_video; int frameCounter = 0; int tick = 0; int fps = fps_of_video; std::time_t timeBegin = std::time(0); for (;;) { double time_start = (double)getTickCount(); Mat frame; capture >> fram
🌐
ProgramCreek
programcreek.com › python › example › 89372 › cv2.CAP_PROP_FPS
Python Examples of cv2.CAP_PROP_FPS
:param cap: OpenCV capture :param time: delta time from launch to skip to :return: index of requested frame """ if thresh is None: thresh = self.extractor.image_dict[key][2] number_of_frames = int(cap.get(cv2.CAP_PROP_FPS) * time) + self.search_switch(cap, key, thresh) number_of_frames = max(number_of_frames, 0) number_of_frames = min(number_of_frames, cap.get(cv2.CAP_PROP_FRAME_COUNT)) cap.set(cv2.CAP_PROP_POS_FRAMES, number_of_frames) return number_of_frames ·
🌐
GitHub
github.com › opencv › opencv › issues › 24000
.get(cv2.CAP_PROP_FPS) returns incorrect FPS · Issue #24000 · opencv/opencv
July 17, 2023 - video_capture = cv2.VideoCapture('C:\\Users\\AShetty12\\Downloads\\raw_videos\\MOV_2564.mp4') print(video_capture.isOpened()) # returns True print(video_capture.get(cv2.CAP_PROP_FPS)) # returns 30 total_frames = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT)) print(total_frames) # returns 21784 print(video_capture.set(cv2.CAP_PROP_POS_AVI_RATIO, 1)) # returns True _ = video_capture.read() time_msec = video_capture.get(cv2.CAP_PROP_POS_MSEC) print(time_msec/(1000*total_frames)) # returns 0.03335926980046518 video_capture.get(cv2.CAP_PROP_POS_FRAMES) # returns 21785.0 ... EDIT: Slight change to add the 1000 in the denominator when dividing by total frames to manually calculate fps ... I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
🌐
GeeksforGeeks
geeksforgeeks.org › python-displaying-real-time-fps-at-which-webcam-video-file-is-processed-using-opencv
Python - Displaying real time FPS at which webcam/video file is processed using OpenCV - GeeksforGeeks
January 4, 2023 - We will be displaying the real-time processing FPS of the video file or webcam depending upon our choice. FPS or frame per second or frame rate can be defined as number of frames displayed per second. A video can be assumed as a collection of images or we can say frames which are displayed at some rate to produce motion.
🌐
GitHub
github.com › opencv › opencv › issues › 21673
cv::VideoCapture set cv::CAP_PROP_FPS is not working · Issue #21673 · opencv/opencv
March 2, 2022 - Please try to use some sample code to setup the fps for web cameras, like: #include "opencv2/videoio.hpp" cv::VideoCapture capture; capture.open("/dev/video0"); bool success = capture.isOpened(); if (success) { capture.set(cv::CAP_PROP_FPS, 25); }
🌐
GitHub
github.com › opencv › opencv › issues › 16844
opencv with the camera framerate 120 FPS · Issue #16844 · opencv/opencv
March 18, 2020 - video.set(cv::CAP_PROP_FOURCC, VideoWriter::fourcc('M', 'J', 'P', 'G')); video.set(cv::CAP_PROP_FRAME_HEIGHT, 640); video.set(cv::CAP_PROP_FRAME_WIDTH, 480); double fps = video.get(cv::CAP_PROP_FPS); // If you do not care about backward compatibility // You can use the following instead for OpenCV 3 // double fps = video.get(CAP_PROP_FPS); cout << "Frames per second using video.get(CV_CAP_PROP_FPS) : " << fps << endl; // Number of frames to capture int num_frames = 120; // Start and end times time_t start, end; // Variable for storing video frames Mat frame; cout << "Capturing " << num_frames
Author: opencv