From 53f2f0269bd6b308bb51bff0c06e7b383337b997 Mon Sep 17 00:00:00 2001 From: Phongsiri Nirachornkul <89494368+bring-nirachornkul@users.noreply.github.com> Date: Mon, 2 Dec 2024 00:37:37 -0600 Subject: [PATCH] Create render_video --- render_video | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 render_video diff --git a/render_video b/render_video new file mode 100644 index 0000000..7fee249 --- /dev/null +++ b/render_video @@ -0,0 +1,57 @@ +#python render_video.py path_to_image_folder + + +import cv2 +import os +from glob import glob +import sys +from tqdm import tqdm # Progress bar library + +def images_to_video(image_folder, fps=30): + # Check if the folder exists + if not os.path.exists(image_folder): + print(f"Error: Folder '{image_folder}' does not exist.") + return + + # Get all image files in the folder, sorted by filename + images = sorted(glob(os.path.join(image_folder, '*.jpg'))) # Change extension if needed + if not images: + print("No images found in the folder.") + return + + # Extract folder name and define the output video file + folder_name = os.path.basename(os.path.normpath(image_folder)) + os.makedirs("output_video", exist_ok=True) + output_video = os.path.join("output_video", f"{folder_name}-01.mp4") + + + # Ensure the output file doesn't overwrite existing files + counter = 1 + while os.path.exists(output_video): + counter += 1 + output_video = f"{folder_name}-{counter:02d}.mp4" + + # Read the first image to get dimensions + frame = cv2.imread(images[0]) + height, width, channels = frame.shape + + # Define the video codec and create VideoWriter object + fourcc = cv2.VideoWriter_fourcc(*'mp4v') # For .mp4 format + video_writer = cv2.VideoWriter(output_video, fourcc, fps, (width, height)) + + # Process images with a progress bar + print(f"Rendering video: {output_video}") + for image_file in tqdm(images, desc="Processing frames", unit="frame"): + frame = cv2.imread(image_file) + video_writer.write(frame) # Write the frame to the video + + video_writer.release() + print(f"Video successfully created: {output_video}") + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: python script.py ") + sys.exit(1) + + image_folder = sys.argv[1] + images_to_video(image_folder, fps=30)