技术比较菜,目前只会合并成avi格式的视频,使用其他格式都会出现问题,如果有哪位大佬能解决这个问题,欢迎评论!
import os import cv2 def ConvertVideoToImgs(videoPath, savePath): """ 将视频中的每一帧读取然后保存 :param videoPath:视频地址 :param savePath:图片保存地址 :return: """ cap = cv2.VideoCapture(videoPath) count = 0 while True: ret, frame = cap.read() if not ret: break cv2.imwrite(f'{savePath}/{str(count).zfill(5)}.jpg', frame) count = count + 1 print('视频帧存储完成') def ConvertImgsToVideo(imgsPath, savePath, fps): """ 将多张图片封装成视频 :param imgPath: :param savePath: :return: """ fourcc = cv2.VideoWriter_fourcc(*'XVID') imglist = os.listdir('Resources/vis') size = tuple(list(cv2.imread(imgsPath + '/' + imglist[0]).shape)[-2::-1]) videoWriter = cv2.VideoWriter(savePath, fourcc, fps, size) for img in imglist: frame = cv2.imread(imgsPath + '/' + img) videoWriter.write(frame) videoWriter.release() print('视频保存完成') if __name__ == '__main__': videoPath = 'Resources/video01.mp4' imgsPath = 'Resources/vis' saveVideo = 'Resources/saveVideo.avi' ConvertVideoToImgs(videoPath, imgsPath) ConvertImgsToVideo(imgsPath, saveVideo, 10)