v8hid's picture
Helper functions
6171e4c
import cv2
import numpy as np
def write_video(file_path, frames, fps, reversed=True, start_frame_dupe_amount=15, last_frame_dupe_amount=30):
"""
Writes frames to an mp4 video file
:param file_path: Path to output video, must end with .mp4
:param frames: List of PIL.Image objects
:param fps: Desired frame rate
:param reversed: if order of images to be reversed (default = True)
"""
if reversed == True:
frames.reverse()
w, h = frames[0].size
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
# fourcc = cv2.VideoWriter_fourcc('h', '2', '6', '4')
# fourcc = cv2.VideoWriter_fourcc(*'avc1')
writer = cv2.VideoWriter(file_path, fourcc, fps, (w, h))
# start frame duplicated
for x in range(start_frame_dupe_amount):
np_frame = np.array(frames[0].convert('RGB'))
cv_frame = cv2.cvtColor(np_frame, cv2.COLOR_RGB2BGR)
writer.write(cv_frame)
for frame in frames:
np_frame = np.array(frame.convert('RGB'))
cv_frame = cv2.cvtColor(np_frame, cv2.COLOR_RGB2BGR)
writer.write(cv_frame)
# last frame duplicated
for x in range(last_frame_dupe_amount):
np_frame = np.array(frames[len(frames) - 1].convert('RGB'))
cv_frame = cv2.cvtColor(np_frame, cv2.COLOR_RGB2BGR)
writer.write(cv_frame)
writer.release()