1. 程式人生 > >python 合成視訊段

python 合成視訊段

有多個視訊,為了測試方便,從每個視訊中取一段合成為一個視訊。

import cv2
import argparse

def parse_args():
    args = argparse.ArgumentParser()
    args.add_argument('videos',nargs='+')
    return args.parse_args()

def videos2img(videos):
	fps = 25
	size = (1920, 1080)
	fourcc = cv2.VideoWriter_fourcc(*'MJPG')
	videoWriter = cv2.VideoWriter('concat.mp4', fourcc, fps, size)

	for video in videos:
		videoCapture = cv2.VideoCapture(video)
	
		count = 0
		success, frame = videoCapture.read()
		while success:
			if count < 300:
				videoWriter.write(frame)
				# cv2.imshow("winname", frame)
				# cv2.waitKey()
				success, frame = videoCapture.read()
				if frame.shape[1] < 1920:
					frame = cv2.resize(frame, size, interpolation=cv2.INTER_CUBIC)
					# cv2.imshow("winname", frame)
					# cv2.waitKey()
				count += 1
			else:
				break

if __name__ == "__main__":
	args = parse_args()
	videos2img(args.videos)