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
if I replace
cap = cv2.VideoCapture(0)
withcap = 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 withcap = cv2.VideoCapture(0)
writing / encoding frames is quite expensive. try a lower resolution
I think writing / encoding frames is not the problem , I just
ret, frame = cap.read()
notout.write(frame)
,it also used nearly 25 seconds.