设计思路:
1、先遍历批处理文件夹,找到每个视频文件。
2、在输出目录下生成一个与视频文件同名的根目录
3、用cv2.VideoCapture()读取视频文件,并将读取的每一帧分别保存到对应根目录。
4、一次处理多个文件时可以用到多线程加快速度。
#!/usr/bin/env python3
# coding=utf-8
import os
import sys
from concurrent.futures import ThreadPoolExecutor
import cv2
def output_img(video_path, img_dir):
"""
由视频逐帧输出图片
:param video_path: 视频文件路径
:param img_dir: 图片保存目录路径,路径不支持中文
:return:
"""
os.makedirs(img_dir, exist_ok=True)
cv = cv2.VideoCapture(video_path)
n = 0
ret, frame = cv.read()
while ret:
n += 1
img_name = "0000000{0}.jpg".format(n)
img_file_path = os.path.join(img_dir, img_name)
if not os.path.exists(img_file_path):
sys.stdout.write("创建文件:" + img_file_path + "\n")
cv2.imwrite(img_file_path, frame, [cv2