import cv2
import os
# Saved video path and video size(1920, 1080)
writer = cv2.VideoWriter('D:/achenf/data/0618/data0706/fold2/val/val.mp4', cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), 25, (1920, 1080), True)
# ********** Set the number of frames **********
total_frame = len(os.listdir('D:/achenf/data/0618/data0706/fold2/val/img/'))
print(total_frame)
for frame_num in range(total_frame):
img_path = 'D:/achenf/data/0618/data0706/fold2/val/img/%d.jpg' % frame_num # Picture path
read_img = cv2.imread(img_path)
writer.write(read_img)
writer.release()
use OpenCV To save the video, you need to call its VideoWriter class
VideoWriter(filename, fourcc, fps, frameSize[, isColor]) -> <VideoWriter object>
1. The first parameter is the path of the file to be saved
2.fourcc Specify encoder
3.fps The frame rate of the video to save
4.frameSize The picture size of the file to be saved
5.isColor Indicates whether the picture is black and white or color
The specified size of video storage should be the same as the size of the written image , Otherwise, video storage fails