#-*- coding: utf-8 -*-
# 本脚本的功能:将一个文件夹下的所有.avi视频截取帧保寸为图像
#Auth:cao
import os, sys,argparse, cv2
from datetime import datetime
def checkpath(path):
if not os.path.exists(path):
print("错误的路径: {}".format(path))
sys.exit()
def makepath(path):
if not os.path.exists(path):
os.makedirs(path)
if __name__ == "__main__":
# ==================================================================================================================
# 输入参数,不能有中文路径
# input_path: 视频的存放路径
# output_path: 图片的保存路径
# ==================================================================================================================
parse = argparse.ArgumentParser()
parse.add_argument("--input_path", type=str, default= 'D:\\Project\\videos\\', help="...")#F:\videoE:\CUTOUTIMAGE\image\11111111
parse.add_argument("--output_path", type=str, default='D:\\Project\\videos\\', help="")#F:\OUTPUTSample
parse.add_argument("--imgnumber_pervideo", type = int, default = 1000000, help = "")
parse.add_argument("--skip_frame", type = int, default = 5, help = "")
# ==================================================================================================================
#E:\jiao_video\avi
flags, unparsed = parse.parse_known_args(sys.argv[1:])
input_path = flags.input_path
output_path = flags.output_path
imgnumber_pervideo = flags.imgnumber_pervideo
skip_frame = flags.skip_frame
checkpath(input_path)
makepath(output_path)
#save_path = output_path
files = os.listdir(input_path)
video_type = ['avi', 'mp4', 'MP4', 'AVI', 'mov']
for file in files:
if len(file.split('.')) > 1 and file.split('.')[-1] in video_type:
file_name = file.split('.')[0]
# if file_name != 'CourtTest07':
# continue
file_path = os.path.join(input_path, file)
print(file_path)
cap = cv2.VideoCapture(file_path)
save_path = os.path.join(output_path, file_name)
makepath(save_path)
count = 0
n = 0
success = True
while (success and n < imgnumber_pervideo):
count += 1
success, frame = cap.read()
if 1 == count%skip_frame:
#success, frame = cap.read()
n += 1
print(n)
time = str(datetime.now())
time = '_'.join(time.split(' ')).split('.')[0]
time = ''.join(time.split(':'))
time = ''.join(time.split('-'))
img_save_name =file_name + time + '_' + '_'+str(count).zfill(7)+'.jpg'
cv2.imwrite(os.path.join(save_path, img_save_name), frame)
#cv2.imwrite(os.path.join(output_path, img_save_name), frame)
cap.release()