image-pluser-webui/processing/bulk.py

47 lines
1.5 KiB
Python
Raw Normal View History

2023-04-16 08:51:19 +03:00
import ffmpeg
import os
2023-04-16 10:36:29 +03:00
from processing.utils import generate_name, get_date_text, generate_name_with_file_name
2023-04-18 13:16:16 +03:00
from methods.bulk_methods import methods_funcs
2023-04-18 13:21:35 +03:00
from tqdm import tqdm
2023-04-16 08:51:19 +03:00
2023-04-16 18:40:08 +03:00
def images_to_video(
directory, fps, img_ext, img_name_format, video_name, video_ext, video_dir
):
2023-04-16 09:55:33 +03:00
images_pattern = os.path.join(directory, f"{img_name_format}.{img_ext}")
2023-04-16 08:51:19 +03:00
2023-04-16 09:55:33 +03:00
if video_dir:
video_path = os.path.join(video_dir, f"{video_name}.{video_ext}")
else:
video_path = generate_name(
2023-04-16 18:40:08 +03:00
extension=video_ext, name=video_name, subfolder="videos"
)
2023-04-16 09:55:33 +03:00
2023-04-16 18:40:08 +03:00
ffmpeg.input(images_pattern, framerate=fps).output(
video_path, pix_fmt="yuv420p"
).global_args("-y").run()
2023-04-16 09:55:33 +03:00
def video_to_images(video_path, img_ext):
images_pattern = generate_name(
2023-04-16 18:40:08 +03:00
extension=img_ext, name=f"%d", subfolder=os.path.join("images", get_date_text())
)
2023-04-16 09:55:33 +03:00
ffmpeg.input(video_path).output(images_pattern).run()
2023-04-16 10:36:29 +03:00
def bulk_processing(directory, out_directory, method):
2023-04-18 13:21:35 +03:00
run_bulk(methods_funcs[method], directory, out_directory, get_date_text())
2023-04-16 10:46:55 +03:00
def run_bulk(func, directory, out_directory, date):
2023-04-18 13:21:35 +03:00
for file in tqdm(os.listdir(directory)):
2023-04-16 10:46:55 +03:00
img = func(os.path.join(directory, file))
if out_directory:
img_out_path = os.path.join(out_directory, file)
else:
img_out_path = generate_name_with_file_name(
2023-04-16 18:40:08 +03:00
name=file, subfolder=os.path.join("images", date)
)
2023-04-16 10:46:55 +03:00
img.save(img_out_path)