1
我試圖編寫一些代碼,用於在Windows上通過批處理腳本激活時自動捕獲網絡攝像頭中的視頻。我設法拼湊了一個工作腳本,但它似乎並沒有保存文件。我知道代碼是(至少在基本級別上)工作的,因爲沒有錯誤,當代碼運行時,網絡攝像頭上的激活指示燈亮起。如果有人對如何將它寫入文件有任何建議,我已經複製了下面的代碼,那太棒了!使用Python進行視頻捕獲
import numpy as np
import cv2
import msvcrt
cap = cv2.VideoCapture(0)
w=int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h=int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
sub=raw_input("Subject#: ")
#Define the codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'DIVX')
fourcc = cv2.VideoWriter_fourcc(*'FMP4')
out = cv2.VideoWriter('C:\path\to\output_' + sub + '.mp4', fourcc, 30, (w,h))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
out.write(frame)
if msvcrt.kbhit():
if ord(msvcrt.getch()) != None:
break
else:
break
#Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()