#!/usr/bin/env python
# coding=utf-8
import os
import cv2
# 视频路径和图片保存路径
videos_path = r"D:\WorkSpace\PycharmProjects\pythonProject4\videos"
images_path = r"D:\WorkSpace\PycharmProjects\pythonProject4\images"
if not os.path.exists(images_path):
os.makedirs(images_path)
# 遍历读取视频文件---支持多级目录下的视频文件遍历
i = 0
file_count = 0
for root, dirs, files in os.walk(videos_path):
for file_name in files:
file_count += 1
i += 1
# os.mkdir(images_path + '/' + '0324_%d' % i)
# img_full_path = os.path.join(images_path, '0324_%d' % i) + '/'
s=root.replace(videos_path, images_path)
if not os.path.exists(s):
os.makedirs(s)
s1=root.replace(videos_path,images_path) + '/' + file_name.split('.')[0]
if not os.path.exists(s1):
os.makedirs(s1)
img_full_path = os.path.join(images_path, file_name.split('.')[0]) + '/'
videos_full_path = os.path.join(root, file_name)
cap = cv2.VideoCapture(videos_full_path)
print('\n开始处理第 ', str(i), ' 个视频:' + file_name)
# 以指定帧数抽取图片并保存
frame_count = 0
ret = True
while ret:
ret, frame = cap.read()
frame_count += 1
if frame_count % 50 == 0:
# frame = cv2.resize(frame, (1280, 720))
# name = img_full_path + "0324_%d_%06d.jpg" % (i, frame_count)
name = s1 +'/'+ file_name.split('.')[0] + "_%06d.jpg" % (frame_count)
cv2.imwrite(name, frame)
print('\n一共 ', str(file_count), ' 个视频,', '已全部处理完毕!')
多级目录下视屏抽帧,并保存至对应文件夹中
最新推荐文章于 2024-11-05 16:27:01 发布