6

Ok, so I am making a video. I want to know exactly how to use the FPS argument. It is a float, so I assumed it was what interval do I want between each frame. Can you give an example? I just want to know how the video would change with varying FPS argument values., because my video I made is way too fast now. Thanks!

Tom
  • 846
  • 5
  • 18
  • 30

1 Answers1

13

It really is just that - frames per second. In other words, how many frames do you want to display every second?

Here is an example:

writer = cv2.VideoWriter(filename="my_video.avi",  #Provide a file to write the video to
fourcc=cv2.cv.CV_FOURCC('i','Y', 'U', 'V'),            #Use whichever codec works for you...
fps=15,                                        #How many frames do you want to display per second in your video?
frameSize=(width, height))                     #The size of the frames you are writing

Example usage:

while True:
    flag, frame = capture.read()
    cv2.imshow("Camera", frame)
    key_pressed = cv2.waitKey(10)
    if key_pressed == 27:                           #Escape key
        break
    writer.write(frame)
cv2.destroyAllWindows()

Thus, you will have a video file that consists of all the still frames that your camera captured stitched together as a single video. The number of frames that are displayed per second will be as you set with the fps parameter. (If your video is too fast, I recommend setting a lower fps)

I wrote this code off the top of my head, so I haven't tested it, but it should work. Let me know if you have any questions or problems. I hope this helps you!

Schneems
  • 14,918
  • 9
  • 57
  • 84
hjweide
  • 11,893
  • 9
  • 45
  • 49
  • thank you for this answer , I use the `kivy` `Clock.schedule_interval( func, 0)` instead of a `while loop` to record my screen , and I've tried setting the `fps` to `15` , `10` , and `5` , but for `fps = 5 or 10` it appears a bit laggy for fast-motion video. Is there any solution for this ? – Paul Lam Aug 09 '21 at 11:12
  • @PaulLam if your screen has a refresh rate of 60Hz but you're only capturing 5 to 10 frames per second you're losing a lot of information. Can you capture more frames per second? – hjweide Aug 10 '21 at 00:50
  • um , I am not sure. currently I'm trying to use `thread` to thread a function that uses a while loop to capture frame. I think it can capture more frame but seems like still not enough. I use `print` to print `True` if already captured for 1 second else `print("")` and I get sth like 13 - 15 blank lines before the `True` is printed out – Paul Lam Aug 10 '21 at 17:18