fabriquedoc-backend/convert_video.py
2024-08-23 22:50:28 -04:00

46 lines
1.2 KiB
Python

import logging
import os
import cv2
def convert_video(images_path, output_path, width, height, fps, stilltime):
"""
Convert images in output_path into a mp4 file usine OpenCV.
:param images_path:
:param output_path:
:param images_path:
:param width:
:param height:
:return:
"""
# define a frame array
frame_array = []
# list all files in images_path
files = [f for f in os.listdir(images_path) if os.path.isfile(os.path.join(images_path, f))]
# sort the files
files.sort()
# create a video writer object
for i in range(len(files)):
file = os.path.join(images_path, files[i])
logging.log(logging.INFO, f'Converting {file} to mp4')
img = cv2.imread(file)
for j in range(fps * stilltime):
frame_array.append(img)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer = cv2.VideoWriter(output_path,
fourcc,
fps,
(width, height))
for i in range(len(frame_array)):
# writing to a image array
video_writer.write(frame_array[i])
video_writer.release()
logging.log(logging.INFO, f'Finished converting {output_path}')