python opencv video capture

#从摄像头捕获视频

import cv2
def readvideo():
    cap=cv2.VideoCapture('output.avi')
    print(cap.get(3),cap.get(4))
    while (cap.isOpened):
     ret,frame =cap.read()
     if ret==True:
         frame=cv2.flip(frame,1)
         out.write(frame)
         cv2.imshow('frame',frame)
         if cv2.waitKey(20)==ord('q'):
              break
     else:
        break
    
    
         
cap=cv2.VideoCapture(0)#0为设备索引号,此处也可以为文件
#print(cap.isOpened)#检查是否初始化
#print(cap.get(3),cap.get(4))
fourcc=cv2.VideoWriter_fourcc(*'XVID')
out=cv2.VideoWriter('output.avi',fourcc,20.0,(640,480))
while (cap.isOpened):
     ret,frame =cap.read()
     if ret==True:
         frame=cv2.flip(frame,1)
         out.write(frame)
         cv2.imshow('frame',frame)
         
     #ret=cap.set(3,256)
     #ret-cap.set(4,256)
     #gray =cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
     #cv2.imshow('frame',gray)
         if cv2.waitKey(20)==ord('q'):
              break
     else:
        break
readvideo()     
cap.release()
out.release()
cv2.destroyAllWindows()

你可能感兴趣的:(python)