import cv2
import os
file_path = 'F:/zmz/ta-chi_data/'
for name in os.listdir(file_path):
pth = os.path.join(file_path,name)
nm = name.split('.')[0] # name = "**.mp4"
print(nm)
cap = cv2.VideoCapture(pth)
cap.isOpened()
if cap.isOpened(): # 当成功打开视频时cap.isOpened()返回True,否则返回False
# get方法参数按顺序对应下表(从0开始编号)
rate = cap.get(5) # 帧速率
FrameNumber = int(cap.get(7)) # 视频文件的帧数
print(FrameNumber)
duration = FrameNumber / rate # 帧速率/视频总帧数 是时间,除以60之后单位是分钟
fps = int(rate) #每一段小视频帧数
print(duration, fps)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
i = 0 # 第i帧
while (True):
success, frame = cap.read()
if success:
i += 1
# 1s是25帧,fps * 10是250帧即10s的总帧数
if (i % (fps*10) == 1): # 10s保存一次视频
# i = 1,251, 501.......
if not os.path.exists(f'F:/zmz/sublime/{nm}'):
os.makedirs(f'F:/zmz/sublime/{nm}')
# videoWriter = cv2.VideoWriter('F:/zmz/sublime/001/' + str(i) + '.avi',cv2.VideoWriter_fourcc('X','V','I','D'), fps,frameSize=(320,240))
# videoWriter = cv2.VideoWriter(f'F:/zmz/sublime/{nm}/{nm}_' + str(i // 250 + 1) + '.mp4',cv2.VideoWriter_fourcc('D', 'I', 'V', 'X'), fps,(int(width),int(height)))
videoWriter = cv2.VideoWriter(f'F:/zmz/sublime/{nm}/{nm}_' + str(i // 250 + 1) + '.avi', cv2.VideoWriter_fourcc('X','V','I','D'), fps,(int(width), int(height)))
videoWriter.write(frame)
else:
videoWriter.write(frame)
else:
print('end')
break
cap.release()
|