fps is not right when i save video from camera

asked 2020-03-31 11:07:09 -0600

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

edit retag flag offensive close merge delete

Comments

if I replace cap = cv2.VideoCapture(0) with cap = cv2.VideoCapture('output5.avi'), it can finish in 2.113304 S. output5.avi is an 4 S video(1080*1920 @30fps) so i think there is somthing wrong with cap = cv2.VideoCapture(0)

wwwzhuanli gravatar imagewwwzhuanli ( 2020-03-31 11:48:07 -0600 )edit

writing / encoding frames is quite expensive. try a lower resolution

berak gravatar imageberak ( 2020-03-31 11:55:16 -0600 )edit

I think writing / encoding frames is not the problem , I just ret, frame = cap.read() not out.write(frame) ,it also used nearly 25 seconds.

wwwzhuanli gravatar imagewwwzhuanli ( 2020-03-31 22:34:59 -0600 )edit