要求是想通过手上现有的摄像头来保存25fps的视频,在win10和Ubuntu下的代码稍微有些不同。
倒是目前遇到的问题是不能保证fps的稳定和也不是想要的25fps,我已经在fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')设置了25fps,但是拍出来是接近30fps,我只能用sleep来人为控制fps,有人知道为什么不能得到制定的fps吗?
import cv2
import time
from multicpu import multi_cpu
timenow = time.strftime('%Y%m%d_%H%M%S')
# 28号,试了,1号机器,MJPG,time.sleep(0.0005),可以比较稳定的25fps
# 2号机器好像不是很稳定
def get_time_stamp():
ct = time.time()
local_time = time.localtime(ct)
data_head = time.strftime("%H:%M:%S", local_time)
data_secs = (ct - int(ct)) * 1000
time_stamp = "%s.%03d" % (data_head, data_secs)
return time_stamp
def videowork():
videoname = './data_0527/' + time.strftime('%Y%m%d_%H%M%S') + '.avi'
cap = cv2.VideoCapture(0)
# height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
# width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
# fps = cap.get(cv2.CAP_PROP_FPS)
# set_fourcc = cap.get(cv2.CAP_PROP_FOURCC)
# print(height,width,fps,set_fourcc)
cap.set(3,1920)
cap.set(4,960)
cap.set(5,25)
# 下面这一段在win10下可以不加,在Ubuntu下一定要加
# cap.set(6,cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
out = cv2.VideoWriter(videoname, fourcc, 25, (1920,960))
time.sleep(1)
cc = 0
t0=time.clock()
while 1:
cc += 1
t2 = time.clock()
if t2 - t0 > 1:
t0 = time.clock()
print(cc)
cc = 0
# aaaa += 1
ret, frame = cap.read() # get frame
font = cv2.FONT_HERSHEY_SIMPLEX # 使用默认字体
time_temp = get_time_stamp()
#print('video' + str(time_temp), aaaa)
frame = cv2.putText(frame, time_temp, (100, 850), font, 2, (0, 0, 255), 3)
# cv2.namedWindow('video', cv2.WINDOW_NORMAL)
# cv2.imshow("video", frame)
# cv2.waitKey(1)
# time.sleep(0.0005)
out.write(frame)
# else:
# cap.release()
while 1:
videowork()