Python分解视频,第一频会缺失,如何解决?
待分解的频共10帧,分解出来应该是1-10这10个数字的图片。
test.avi.zip
(524.12 KB)
程序代码:
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() if not success: break 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)