Skip to content

Instantly share code, notes, and snippets.

@dmitrysarov
Last active January 17, 2020 14:57
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 dmitrysarov/f1488454593c1b14b38979a4283348b5 to your computer and use it in GitHub Desktop.
Save dmitrysarov/f1488454593c1b14b38979a4283348b5 to your computer and use it in GitHub Desktop.
How to write sequence of frames to video file in cv2
def write_avi(frames, path):
'''
frames - 3D numpy array (seq, height, width)
path - result avi file path. Extantion of file have to be .avi
'''
def norm(x):
x = x - x.min()
x = x / x.max()
return (x*255).astype(np.uint8)
frames = norm(frames)
fourcc_string = 'MJPG'
fourcc = cv2.VideoWriter_fourcc(*fourcc_string)
out = cv2.VideoWriter(path, fourcc, fps, frames.shape[1:][::-1])
for i in range(len(frames)):
out.write(np.repeat(frames[i][...,np.newaxis],3,-1))
out.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment