import cv2
import os
def split_mp4(inpath, file, outpath):
path =os.path.join(inpath, file)
outfile = os.path.join(outpath, file)
video = cv2.VideoCapture(path)
frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) #获取视频帧数
frame_height = video.get(cv2.CAP_PROP_FRAME_HEIGHT)
frame_width = video.get(cv2.CAP_PROP_FRAME_WIDTH)
fps = video.get(cv2.CAP_PROP_FPS) #读取视频帧率
print('count:',frame_count, 'heigth:', frame_height, 'width:', frame_width, 'fps:', fps)
fps = 60 #保存视频的帧率
size = (816, 1080) #保存视频分辨率的大小 宽高(宽高写反了会导致得到的视频无法打开)
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
#VideoWriter_fourcc对象一些常用的参数,注意:字符顺序不能弄混
#cv2.VideoWriter_fourcc('I', '4', '2', '0'),该参数是YUV编码类型,文件名后缀为.avi
#cv2.VideoWriter_fourcc('P', 'I', 'M', 'I'),该参数是MPEG-1编码类型,文件名后缀为.avi
#cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'),该参数是MPEG-4编码类型,文件名后缀为.avi
#cv2.VideoWriter_fourcc('T', 'H', 'E', 'O'),该参数是Ogg Vorbis,文件名后缀为.ogv
#cv2.VideoWriter_fourcc('F', 'L', 'V', '1'),该参数是Flash视频,文件名后缀为.flv
#cv2.VideoWriter_fourcc('m', 'p', '4', 'v') 文件名后缀为.mp4
videoWriter =cv2.VideoWriter(outfile, fourcc,fps,size)
i = 0
while True:
success,frame = video.read()
if success:
i += 1
print('i = ',i)
if(i>=0 and i <= 99): #截取0帧到99帧的视频,共100 帧
videoWriter.write(frame)
# videoWriter.write(frame)
else:
print('end')
break
if __name__ == "__main__":
for num in range(52,63):
if num == 59:
continue
subdir = 'person0%d'%num
inpath = r'C:\Users\jiaopp\Desktop\tmb\train'
inpath = os.path.join(inpath, subdir)
outpath = r'C:\Users\jiaopp\Desktop\tmb\new_trainmp4'
dir = os.listdir(inpath)
for file in dir:
if file.endswith('.mp4'):
split_mp4(inpath, file, outpath)