3

I am trying to change the Frame rate i.e., FPS of an existing video using openCV library in python. Below is the code that I am trying to execute. Even after setting the FPS property using cv2.CAP_PROP_FPS the video is not playing faster in the cv2.imshow() method. Even After setting the FPS property the getter returns the older FPS value. So how do I set the FPS value higher and make the video play faster?

Used version: python = 3.7.4 and opencv-python - 4.1.0.25

import cv2

video = cv2.VideoCapture("yourVideoPath.mp4");
video.set(cv2.CAP_PROP_FPS, int(60))

if __name__ == '__main__':
    print("Frame rate : {0}".format(video.get(cv2.CAP_PROP_FPS)))
    while video.isOpened():
        ret1, frame2 = video.read()
        cv2.imshow("Changed", frame2)

        if cv2.waitKey(10) & 0xFF == ord('q'):  # press q to quit
            break

video.release()
cv2.destroyAllWindows()
Subash J
  • 2,028
  • 3
  • 13
  • 27
  • If I'm getting you correctly - you want to increase FPS, right ? If yes, then - you need to get faster machine, which will process your ```while``` loop faster. What you are doing there is - you read frame one at a time from your video file, and print the image, overwriting it until video is done. The slowness is caused by processing of this image, and not by the video itself. – Grzegorz Skibinski Sep 04 '19 at 14:43

1 Answers1

4

If you're only trying to play the video in the displayed window, the limiting factor is not the fps of the video but the time spent waiting with the code waitKey(10) which makes the program wait for 10ms between each frame.

The read() method of the VideoCapture class simply returns the next frame with no concept of waiting or frame rate. The only thing preventing this code running as fast as it can is the waitKey(10) section, which is thus the main factor determining speed. To change the frame rate as seen through the imshow() method, you'd need to edit the time spent waiting. This is likely the dominant factor, but not the only one as the reading of a frame does take time.

If you're actually trying to change the playback rate of an existing file and have that saved to that file, I am unsure if OpenCV actually supports this, and I imagine it would be dependent on what back end you're using - OpenCV implements the VideoCapture class using different 3rd party backends.. As per the documentation of VideoCapture.set() I'd investigate the return value of video.set(cv2.CAP_PROP_FPS, int(60)) as the documentation suggests it will return true if this has changed something.

As an alternative you could investigate using something like FFMPEG which supports this relatively easily. If you want to stick with OpenCV I know from personal experience you can do this with the VideoWriter class. In this method you would read in the video frame by frame using the VideoCapture class, and then save it at the desired frame rate with VideoWriter. I suspect FFMPEG will likely meet your needs however!

TheBarrometer
  • 356
  • 1
  • 7
  • Thanks for the reply. I have already tried waitKey(10) VideoWriter class. Actually I do not want to save a video with higher FPS. Without using VideoWriter class i would like to change the FPS property of the video file and just play faster using cv2.imshow. WaitKey(10) works fine but I am trying out another way – Subash J Sep 04 '19 at 14:57
  • 1
    The point I'm making is that waitKey(10) pauses the code for 10ms. This is what is determining the playback of your video by use of imshow, which simply places a cv2.Mat on the screen with no care for anything like it coming from a video with some FPS. The execution speed of the code will have some small impact of course, but the code as shown will always play video files with a frame time of 10ms ie it will attempt to play at 100fps. – TheBarrometer Sep 04 '19 at 15:02