主要是定义视频的编码格式,和设置一个保存视频的路径,在通过write写入到视频文件中,最后在进行file.close().
import cv2
fourcc = cv2.VideoWriter_fourcc(*'XVID')#视频编码格式
out = cv2.VideoWriter('D:/pycharm/arithmetic/1/save.avi',fourcc,20,(640,480))#第三个参数为帧率,第四个参数为每帧大小
cap = cv2.VideoCapture(0)
while(True):
ret,frame = cap.read()
if(ret):
cv2.imshow('input',frame)
out.write(frame)
else:
break
if(cv2.waitKey(1)==27):
break
cap.release()
out.release()
cv2.destroyAllWindows()
保存结果:
jetson code 实现保存视频和将每一帧保存在文件夹中
from jetcam.csi_camera import CSICamera
import cv2,time
camera0 = CSICamera(capture_device=0, width=400, height=400)
#camera1 = CSICamera(capture_device=1, width=224, height=224)
image0 = camera0.read()
output='/home/z/Documents/pic/'
fourcc = cv2.VideoWriter_fourcc(*'XVID')#视频编码格式
out = cv2.VideoWriter('/home/z/Documents/save.avi',fourcc,20,(400,400))#第三个参数为帧率,第四个参数为每帧大小
print(image0.shape)
#image1 = camera1.read()
#print(image1.shape)
print(camera0.value.shape)
#print(camera1.value.shape)
while 1:
image0 = camera0.read()
a=time.strftime('%Y%m%d%H%M%S',time.localtime())
#image1 = camera1.read()
cv2.imshow("CSI Camera0", image0)
out.write(image0)
cv2.imwrite(output+str(a)+'.jpg',image0)
#cv2.imshow("CSI Camera1", image1)
kk = cv2.waitKey(1)
if kk == ord('q'): # 按下 q 键,退出
break
out.release()