Skip to content

Instantly share code, notes, and snippets.

@paulphys
Last active October 19, 2019 20:18
Show Gist options
  • Save paulphys/e36cb632d5c0f9cc78c82702daf4a152 to your computer and use it in GitHub Desktop.
Save paulphys/e36cb632d5c0f9cc78c82702daf4a152 to your computer and use it in GitHub Desktop.
convert frames to video with OpenCV
import cv2
import os
from os.path import isfile,join
input = '/' #folder of frames
output = 'framestovideo.mp4' #output path
fps = 25 #default 25
frame_array = []
frames = [f for f in os.listdir(input) if isfile(join(input, f))]
frames.sort(key=lambda x: x[5-4])
frames.sort()
for i in range(len(frames)):
filename = input + frames[i]
# reading each files
img = cv2.imread(filename)
height, width, layers = img.shape
size = (width, height)
frame_array.append(img)
out = cv2.VideoWriter(output, cv2.VideoWriter_fourcc('M','J','P','G'), fps, size)
for i in range(len(frame_array)):
# writing to a image array
out.write(frame_array[i])
print("Writing frame {} to video".format(i))
out.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment