python 拆分最后一帧时,总会出错,不知如何修改?
程序代码:
import cv2 import os def video2frame(videos_path,frames_save_path,time_interval): ''' :param videos_path: 视频的存放路径 :param frames_save_path: 视频切分成帧之后图片的保存路径 :param time_interval: 保存间隔 :return: ''' vidcap = cv2.VideoCapture(videos_path) success, image = vidcap.read() count = 0 while success: success, image = vidcap.read() count += 1 if count % time_interval == 0: cv2.imencode('.jpg', image)[1].tofile(frames_save_path + "/frame%d.jpg" % count) # if count == 20: # break print(count) if __name__ == '__main__': videos_path = 'E:/1.mp4' frames_save_path = 'E:/图片' time_interval = 1 #几帧保存一次 isExists = os.path.exists(frames_save_path) if not isExists: os.makedirs(frames_save_path) video2frame(videos_path, frames_save_path, time_interval)