I wanted to connect a lot of videos such as reinforcement learning results in one shot, so I made it.
A program that creates a single video by concatenating all the videos in a directory containing multiple videos.
import cv2
import glob
def comb_movie(movie_files,out_path):
#The format is mp4
fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
#Acquisition of video information
movie = cv2.VideoCapture(movie_files[0])
fps = movie.get(cv2.CAP_PROP_FPS)
height = movie.get(cv2.CAP_PROP_FRAME_HEIGHT)
width = movie.get(cv2.CAP_PROP_FRAME_WIDTH)
#Open the output file
out = cv2.VideoWriter(out_path, int(fourcc), fps, (int(width), int(height)))
for movies in (movie_files):
print(movies)
#Read video file, argument is video file path
movie = cv2.VideoCapture(movies)
if movie.isOpened() == True: #Check if the video file can be read normally
ret,frame = movie.read() # read():Read the captured image data for one frame
else:
ret = False
while ret:
#Write the read frame
out.write(frame)
#Read next frame
ret,frame = movie.read()
#Retrieve a list of videos in a directory
files = sorted(glob.glob("./movie_dir/*.mp4"))
#Output file name
out_path = "movie_out1.mp4"
comb_movie(files,out_path)
Recommended Posts