|
| 1 | +import cv2 |
| 2 | +import argparse |
| 3 | +import glob |
| 4 | +from pathlib import Path |
| 5 | +import shutil |
| 6 | + |
| 7 | +# Create an ArgumentParser object to handle command-line arguments |
| 8 | +parser = argparse.ArgumentParser(description='Create a video from a set of images') |
| 9 | + |
| 10 | +# Define the command-line arguments |
| 11 | +parser.add_argument('output', type=str, help='Output path for video file') |
| 12 | +parser.add_argument('input', nargs='+', type=str, help='Glob pattern for input images') |
| 13 | +parser.add_argument('-fps', type=int, help='FPS for video file', default=24) |
| 14 | + |
| 15 | +# Parse the command-line arguments |
| 16 | +args = parser.parse_args() |
| 17 | + |
| 18 | +# Create a list of all the input image files |
| 19 | +FILES = [] |
| 20 | +for i in args.input: |
| 21 | + FILES += glob.glob(i) |
| 22 | + |
| 23 | +# Get the filename from the output path |
| 24 | +filename = Path(args.output).name |
| 25 | +print(f'Creating video "{filename}" from images "{FILES}"') |
| 26 | + |
| 27 | +# Load the first image to get the frame size |
| 28 | +frame = cv2.imread(FILES[0]) |
| 29 | +height, width, layers = frame.shape |
| 30 | + |
| 31 | +# Create a VideoWriter object to write the video file |
| 32 | +fourcc = cv2.VideoWriter_fourcc(*'mp4v') |
| 33 | +video = cv2.VideoWriter(filename=filename, fourcc=fourcc, fps=args.fps, frameSize=(width, height)) |
| 34 | + |
| 35 | +# Loop through the input images and add them to the video |
| 36 | +for image_path in FILES: |
| 37 | + print(f'Adding image "{image_path}" to video "{args.output}"... ') |
| 38 | + video.write(cv2.imread(image_path)) |
| 39 | + |
| 40 | +# Release the VideoWriter and move the output file to the specified location |
| 41 | +cv2.destroyAllWindows() |
| 42 | +video.release() |
| 43 | +shutil.move(filename, args.output) |
0 commit comments