Created
August 30, 2023 06:33
-
-
Save eminorhan/c3f6868c8a4e08abfd2ece0631a5e8a4 to your computer and use it in GitHub Desktop.
make mp4 files from a directory of subdirectories of sequences of png files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import cv2 | |
def create_video_from_pngs(png_folder, output_filepath, frame_rate): | |
real_png_folder = os.path.join(png_folder, 'imgs') | |
images = sorted([img for img in os.listdir(real_png_folder) if img.endswith(".png")]) | |
frame = cv2.imread(os.path.join(real_png_folder, images[0])) | |
height, width, layers = frame.shape | |
output_filename = os.path.join(output_filepath, f"{os.path.basename(png_folder)}.mp4") | |
video = cv2.VideoWriter(output_filename, cv2.VideoWriter_fourcc(*"mp4v"), frame_rate, (width, height)) | |
for image in images: | |
video.write(cv2.imread(os.path.join(real_png_folder, image))) | |
cv2.destroyAllWindows() | |
video.release() | |
def main(): | |
folder_path = "test" | |
output_directory = "mp4_test" | |
frame_rate = 25 | |
subfolders = [subfolder for subfolder in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, subfolder))] | |
if not os.path.exists(output_directory): | |
os.mkdir(output_directory) | |
counter = 1 | |
for subfolder in subfolders: | |
print(f"Processing video {counter} of {len(subfolders)}") | |
subfolder_path = os.path.join(folder_path, subfolder) | |
create_video_from_pngs(subfolder_path, output_directory, frame_rate) | |
counter += 1 | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment