What is the optimal fps to record an OpenCV video at?
<p>I'm tinkering around with OpenCV and I'm having some trouble figuring out what fps I should be recording webcam footage. When I record it at <code>15 fps</code> the recorded footage goes much faster than "real life". I was wondering if there is an "optimal" fps at which I can record such that the recording will be <strong>exactly</strong> as long as the time it took to shoot the video?</p>

<p>Here's the program I'm running (though I think that's irrelevant to the question):</p>

<pre><code>import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
fps = 15.0  # Controls the fps of the video created: todo look up optimal fps for webcam
out = cv2.VideoWriter()

success = out.open('../assets/output.mp4v',fourcc, fps, (1280,720),True)

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,1)
        # write the flipped frame
        out.write(frame)
        cv2.imshow('frame',frame)

        # If user presses escape key program terminates
        userInput = cv2.waitKey(1)
        if userInput == 27:
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
</code></pre>

<p>Let's say your camera recording with 25 FPS. If you are capturing 15 FPS while your camera is recording with 25 FPS, the video will be approximately 1.6 times faster than real life.</p>

<p>You can find out frame rate with <code>get(CAP_PROP_FPS)</code> or <code>get(CV_CAP_PROP_FPS)</code> but it's invalid unless the source is a video file.</p>

<p>For cameras or webcams you have to calculate(<em>estimate</em>) FPS programmatically:</p>

<pre><code>num_frames = 240; # Number of frames to capture

print "Capturing {0} frames".format(num_frames)

start = time.time()# Start time

# Grab a few frames
for i in xrange(0, num_frames) :
    ret, frame = video.read()

end = time.time() # End time

seconds = end - start # Time elapsed
print "Time taken : {0} seconds".format(seconds)

# Calculate frames per second
fps  = num_frames / seconds;
print "Estimated frames per second : {0}".format(fps);
</code></pre>

<p>So this program estimates frame rate of your video source by recording first 240 frames as sample and then calculates delta time. Lastly, the result of a simple division gives you the FPS.  </p>

python python-3.x opencv