#coding=utf-8
import os
import cv2
# mp4存放的路徑,路徑下只有mp4
videos_src_path = r'D:\python_tool\tools/video/'
# 保存的路徑,會在路徑下創建mp4文件名的文件夾保存圖片
videos_save_path = r'D:\python_tool\tools/video'
videos = os.listdir(videos_src_path)
#videos = filter(lambda x: x.endswith('MP4'), videos)
for each_video in videos:
print('Video Name :', each_video)
# get the name of each video, and make the directory to save frames
each_video_name, _ = each_video.split('.')
os.mkdir(videos_save_path + '/' + each_video_name)
each_video_save_full_path = os.path.join(videos_save_path, each_video_name) + '/'
# get the full path of each video, which will open the video tp extract frames
each_video_full_path = os.path.join(videos_src_path, each_video)
cap = cv2.VideoCapture(each_video_full_path)
# 第幾幀
frame_count = 1
# 隔著多少幀取一張
frame_rate=30
success = True
# 計數
num=0
while (success):
success, frame = cap.read()
if success == True:
if frame_count%frame_rate==0:
cv2.imwrite(each_video_save_full_path + each_video_name+"%06d.jpg" % num, frame)
num+=1
frame_count = frame_count + 1
print('Final frame:', num)