原文地址:
python工具-将视频按帧截取图片(附代码) - 剑峰随心 - 博客园
感谢原作者
附原代码(会报错):
import cv2
import argparse
import os
def parse_args():
"""
Parse input arguments
"""
parser = argparse.ArgumentParser(description='Process pic')
parser.add_argument('--input', help='video to process', dest='input', default=None, type=str)
parser.add_argument('--output', help='pic to store', dest='output', default=None, type=str)
#default为间隔多少帧截取一张图片
parser.add_argument('--skip_frame', dest='skip_frame', help='skip number of video', default=100, type=int)
#input为输入视频的路径 ,output为输出存放图片的路径
args = parser.parse_args(['--input','','--output',''])
return args
def process_video(i_video, o_video, num):
cap = cv2.VideoCapture(i_video)
num_frame = cap.get(cv2.CAP_PROP_FRAME_COUNT)
expand_name = '.jpg'
if not cap.isOpened():
print("Please check the path.")
cnt = 0
count = 0
while 1:
ret, frame = cap.read()
cnt += 1
# how
# many
# frame
# to
# cut
if cnt % num == 0:
count += 1
cv2.imwrite(os.path.join(o_video, str(count) + expand_name), frame)
if not ret:
break
if __name__ == '__main__':
args = parse_args()
if not os.path.exists(args.output):
os.makedirs(args.output)
print('Called with args:')
print(args)
process_video(args.input, args.output, args.skip_frame)
原文中的 代码。会报错
这是因为传参处了问题。错误处 ,如下:
修改了一下,顺便修改了截图超出帧数的报错。并加上了 进度显示。附上代码:
import cv2
import argparse
import os
import sys
import time
def parse_args():
"""
Parse input arguments
"""
parser = argparse.ArgumentParser(description='Process pic')
parser.add_argument('--input', help='video to process', dest='input', default=None, type=str)
parser.add_argument('--output', help='pic to store', dest='output', default=None, type=str)
#default为间隔多少帧截取一张图片
parser.add_argument('--skip_frame', dest='skip_frame', help='skip number of video', default=1, type=int)
#input为输入视频的路径 ,output为输出存放图片的路径
args = parser.parse_args()
return args
def process_video(i_video, o_video, num):
cap = cv2.VideoCapture(i_video)
num_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
num_frames = int(num_frames)
print(num_frames)
expand_name = '.jpg'
if not cap.isOpened():
print("Please check the path.")
cnt = 0
count = 0
## while 1:
## ret, frame = cap.read()
## cnt += 1
## # how
## # many
## # frame
## # to
## # cut
## if cnt % num == 0:
## count += 1
## cv2.imwrite(os.path.join(o_video, str(count) + expand_name), frame)
##
## if not ret:
## break
for num_time in range(1, num_frames + 1):
ret, frame = cap.read()
cnt += 1
# how
# many
# frame
# to
# cut
if cnt % num == 0:
count += 1
cv2.imwrite(os.path.join(o_video, str(count) + expand_name), frame)
print("\r", end="")
print("当前进度: {}".format(count), "/","{}".format(num_frames), end="")
sys.stdout.flush()
if not ret:
break
if __name__ == '__main__':
args = parse_args()
print(args)
if not os.path.exists(args.output):
os.makedirs(args.output)
print('Called with args:')
print(args)
process_video(args.input, args.output, args.skip_frame)