话不多说,直接上代码。
#! encoding: UTF-8
import os
import cv2
videos_src_path = '/home/zhutianhao/mp4/' //视频路径
videos_save_path = '/home/zhutianhao/jpg' //图片路径
videos = os.listdir(videos_src_path)
jpg_list= os.listdir(videos_save_path)
#print(len(videos))
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
success = True
while(success):
success, frame = cap.read()
if success==True:
cv2.imwrite(each_video_save_full_path + "%06d.jpg" % frame_count, frame)
frame_count = frame_count + 1
print('Final frame:' ,frame_count)
print(len(jpg_list))