import cv2
# from matplotlib import pyplot as plt
# 通过cv2中的类获取视频流操作对象cap
cap = cv2.VideoCapture(r'http://ivi.bupt.edu.cn/hls/cctv1.m3u8')
# 调用cv2方法获取cap的视频帧(帧:每秒多少张图片)
fps = cap.get(cv2.CAP_PROP_FPS)
print(fps)
# 获取cap视频流的每帧大小
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
print(size)
# 定义编码格式mpge-4
fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', '2')
# 定义视频文件输入对象
dir = 0
def save(dir):
# 获取视频流打开状态
if cap.isOpened():
rval, frame = cap.read()
print('ture')
else:
rval = False
print('False')
tot = 1
c = 1
# 循环使用cv2的read()方法读取视频帧
while rval:
rval, frame = cap.read()
# cv2.imshow('live', frame)
# 每间隔200帧保存一张图像帧
if tot % 200 == 0 :
print('tot=', tot)
# cv2.imwrite('cut/'+'cut_'+str(c)+'.jpg',frame)
# c += 1
tot += 1
# print('tot=', tot)
# 使用VideoWriter类中的write(frame)方法,将图像帧写入视频文件
outVideo.write(frame)
cv2.waitKey(25)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
if tot % 12000 == 0 :
dir += 1
print(dir)
outVideo.release()
break
return dir
while True:
outVideo = cv2.VideoWriter(r'video/'+str(dir) + 'saveDir.avi', fourcc, fps, size)
dir = save(dir)
if dir == 36 :
print('输出结束')
break
cap.release()
outVideo.release()
cv2.destroyAllWindows()