隨手記,在win10下用攝像頭錄製視頻沒問題,把代碼直接複製帶ubutnu下,一直花屏,試了很多,最後發現是少了一句
cap.set(6,cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') )
下面貼全部代碼
import cv2
import time
timenow = time.strftime('%Y%m%d_%H%M%S')
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'
fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
cap = cv2.VideoCapture(0)
cap.set(3,1920)
cap.set(4,960)
cap.set(5,25)
cap.set(6,cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') )
out = cv2.VideoWriter(videoname, fourcc, 25, (1920, 960))
time.sleep(1)
aaaa = 0
cc = 0
t0=time.clock()
while aaaa<300000:
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)
out.write(frame)
else:
cap.release()
while 1:
videowork()