Skip to content

Instantly share code, notes, and snippets.

@fazlearefin
Last active January 27, 2021 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fazlearefin/5d7ef69feb93b25756acd39a867f8721 to your computer and use it in GitHub Desktop.
Save fazlearefin/5d7ef69feb93b25756acd39a867f8721 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import argparse
import cv2 # pip3 install opencv-python
# Construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-e", "--extension", required=False, default='png', help="extension name. default is 'png'.")
ap.add_argument("-i", "--input", required=False, default='./', help="input directory")
ap.add_argument("-o", "--output", required=False, default='output.mp4', help="output video file")
ap.add_argument("-f", "--fps", required=False, default='30', help="frames per second of the output video file")
args = vars(ap.parse_args())
# Arguments
dir_path = args['input']
ext = args['extension']
output = args['output']
fps = args['fps']
images = []
for f in os.listdir(dir_path):
if f.endswith(ext):
images.append(f)
images.sort()
images_order = "\n".join(images)
print(f"Images have been sorted in this order \n{images_order}")
# Determine the width and height from the first image
image_path = os.path.join(dir_path, images[0])
frame = cv2.imread(image_path)
cv2.imshow('video',frame)
height, width, channels = frame.shape
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Be sure to use lower case
out = cv2.VideoWriter(output, fourcc, fps, (width, height))
for image in images:
image_path = os.path.join(dir_path, image)
frame = cv2.imread(image_path)
out.write(frame) # Write out frame to video
cv2.imshow('video',frame)
if (cv2.waitKey(1) & 0xFF) == ord('q'): # Hit `q` to exit
break
# Release everything if job is finished
out.release()
cv2.destroyAllWindows()
print(f"The output video is {output}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment