Python OpenCV对本地视频文件进行分帧保存的实例
如下所示:
#coding=utf-8
importos
importcv2
videos_src_path="/home/wgp/视频/"
video_formats=[".MP4",".MOV"]
frames_save_path="/home/wgp/视频/"
width=320
height=240
time_interval=50
defvideo2frame(video_src_path,formats,frame_save_path,frame_width,frame_height,interval):
"""
将视频按固定间隔读取写入图片
:paramvideo_src_path:视频存放路径
:paramformats:包含的所有视频格式
:paramframe_save_path:保存路径
:paramframe_width:保存帧宽
:paramframe_height:保存帧高
:paraminterval:保存帧间隔
:return:帧图片
"""
videos=os.listdir(video_src_path)
deffilter_format(x,all_formats):
ifx[-4:]inall_formats:
returnTrue
else:
returnFalse
videos=filter(lambdax:filter_format(x,formats),videos)
foreach_videoinvideos:
print"正在读取视频:",each_video
each_video_name=each_video[:-4]
os.mkdir(frame_save_path+each_video_name)
each_video_save_full_path=os.path.join(frame_save_path,each_video_name)+"/"
each_video_full_path=os.path.join(video_src_path,each_video)
cap=cv2.VideoCapture(each_video_full_path)
frame_index=0
frame_count=0
ifcap.isOpened():
success=True
else:
success=False
print("读取失败!")
while(success):
success,frame=cap.read()
print"--->正在读取第%d帧:"%frame_index,success
ifframe_index%interval==0:
resize_frame=cv2.resize(frame,(frame_width,frame_height),interpolation=cv2.INTER_AREA)
#cv2.imwrite(each_video_save_full_path+each_video_name+"_%d.jpg"%frame_index,resize_frame)
cv2.imwrite(each_video_save_full_path+"%d.jpg"%frame_count,resize_frame)
frame_count+=1
frame_index+=1
cap.release()
if__name__=='__main__':
video2frame(videos_src_path,video_formats,frames_save_path,width,height,time_interval)
以上这篇PythonOpenCV对本地视频文件进行分帧保存的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。