Ask Your Question

Revision history [back]

fps is not right when i save video from camera

when I want to save video from my camera, i found that the actual fps is lower than I setted. I use opencv-python 3.4.1.15 I use AMCap can record right video(@ 30fps)

my code is following:

    import cv2
import time

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output6.avi',fourcc, 30.0, (1920,1080))

cap = cv2.VideoCapture(0)
print(cap.isOpened())
# cap.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter.fourcc('M','J','P','G')) # MJPG
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,1080)
cap.set(cv2.CAP_PROP_FRAME_WIDTH,1920)

cap.set(cv2.CAP_PROP_FPS,30)

print(cap.get(cv2.CAP_PROP_FPS))
i=0
t0 = time.clock()
while True:
    ret, frame = cap.read()
    if ret == True:
        out.write(frame)
        # cv2.imshow('img',frame)
        # cv2.waitKey(1)
        i+=1
    else:
        print('error')
        break

    if i >120:
        break

print(frame.shape)
cap.release()
out.release()
cv2.destroyAllWindows()
print(time.clock() - t0)

In my code I try to record a video (1080*1920 @30fps), I want to record 120 frames, theory it need 4 seconds, but actually it used the output is :

True
30.00003000003
(1080, 1920, 3)
25.1706875

It used 25.1706875S!!! why?and what can i do ? thank you