Spaces:
Running
Running
# """ | |
# project @ images_to_video | |
# created @ 2024-12-12 | |
# author @ github.com/ishworrsubedii | |
# """ | |
# import os | |
# from datetime import datetime | |
# | |
# from moviepy.audio.io.AudioFileClip import AudioFileClip | |
# from moviepy.temp_video.VideoClip import ImageClip, ColorClip, TextClip | |
# from moviepy.temp_video.compositing.CompositeVideoClip import CompositeVideoClip | |
# from moviepy.temp_video.compositing.concatenate import concatenate_videoclips | |
# from moviepy.temp_video.fx.resize import resize | |
# from moviepy.temp_video.io.VideoFileClip import VideoFileClip | |
# | |
# | |
# class VideoCreator: | |
# def __init__(self): | |
# self.current_date = str(datetime.now().strftime("%Y-%m-%d-%H-%M-%S")) | |
# self.config = { | |
# "intro_video_path": "/home/ishwor/Desktop/TCP/Virtual_Makeup/images_to_video/resourcesssss/temp_video/ChamundiJewelsMandir_intro.mp4", | |
# "outro_video_path": "/home/ishwor/Desktop/TCP/Virtual_Makeup/images_to_video/resourcesssss/temp_video/intro.mp4", | |
# "outro_video_path1": "/home/ishwor/Desktop/TCP/Virtual_Makeup/images_to_video/resourcesssss/temp_video/outro.mp4", | |
# "nto_images_dir": "/home/ishwor/Desktop/TCP/Virtual_Makeup/images_to_video/resourcesssss/02_medium_long_haram/nto_output", | |
# "nto_cto_images_dir": "/home/ishwor/Desktop/TCP/Virtual_Makeup/images_to_video/resourcesssss/02_medium_long_haram/nto_cto_output", | |
# "makeup_images_dir": "/home/ishwor/Desktop/TCP/Virtual_Makeup/images_to_video/resourcesssss/02_medium_long_haram/makeup", | |
# "output_video_path": f"/home/ishwor/Desktop/TCP/Virtual_Makeup/images_to_video/resourcesssss/02_medium_long_haram/output/{self.current_date}_jewelmirror_cjm.mp4", | |
# "font_path": "/home/ishwor/Desktop/TCP/Virtual_Makeup/images_to_video/resourcesssss/font/PlayfairDisplay-VariableFont.ttf", | |
# "necklace_image": "/home/ishwor/Desktop/TCP/Virtual_Makeup/images_to_video/resourcesssss/01_lean_short_necklace/necklace_image/KAR2201CR001.png", | |
# "audio_path": "/home/ishwor/Desktop/TCP/Virtual_Makeup/images_to_video/resourcesssss/audio/TraditionalIndianVlogMusic.mp3", | |
# "transition_duration": 1.0, | |
# "image_display_duration": 2.5, | |
# "text_color": "white", | |
# "box_color": (131, 42, 48), | |
# "box_opacity": 0.8, | |
# "font_size": 28, | |
# "category_font_size": 70 | |
# } | |
# | |
# def create_necklace_clips(self, backgrounds=None): | |
# if backgrounds is None: | |
# backgrounds = [ | |
# (245, 245, 245), | |
# (220, 245, 245), | |
# (230, 230, 235), | |
# ] | |
# | |
# necklace_clips = [] | |
# | |
# for bg_color in backgrounds: | |
# bg_clip = ColorClip((1080, 1080), col=bg_color).set_duration(self.config["image_display_duration"]) | |
# necklace = ImageClip(self.config["necklace_image"]).resize((800, 800)).set_duration( | |
# self.config["image_display_duration"]).set_position('center') | |
# final_clip = CompositeVideoClip([bg_clip, necklace]) | |
# | |
# txt_overlay = self.create_text_overlay("Necklace Preview", (1080, 80), | |
# self.config["image_display_duration"]) | |
# final_clip = CompositeVideoClip([final_clip, txt_overlay.set_position(('center', 'bottom'))]) | |
# | |
# necklace_clips.append(final_clip) | |
# | |
# return necklace_clips | |
# | |
# def create_text_overlay(self, text, size, duration, is_category=False): | |
# box_height = 120 if is_category else 80 | |
# box = ColorClip((1080, box_height), col=self.config["box_color"]).set_opacity( | |
# self.config["box_opacity"]).set_duration(duration) | |
# txt = TextClip( | |
# text, | |
# font=self.config["font_path"], | |
# fontsize=self.config["category_font_size"] if is_category else self.config["font_size"], | |
# color=self.config["text_color"], | |
# size=(1080, box_height), | |
# method='label' | |
# ).set_position('center').set_duration(duration) | |
# | |
# return CompositeVideoClip([box, txt]) | |
# | |
# def process_images_in_directory(self, directory, duration, category_name): | |
# clips = [] | |
# | |
# for image_file in sorted(os.listdir(directory)): | |
# if image_file.lower().endswith((".png", ".jpg", ".jpeg", ".webp")): | |
# image_path = os.path.join(directory, image_file) | |
# text = os.path.splitext(image_file)[0].replace('_', ' ').title() | |
# img_clip = ImageClip(image_path).resize((1080, 1080)).set_duration(duration) | |
# txt_overlay = self.create_text_overlay(text, (1080, 80), duration).set_position(('center', 'bottom')) | |
# clips.append(CompositeVideoClip([img_clip, txt_overlay])) | |
# | |
# return clips | |
# | |
# def create_final_video(self): | |
# print("Loading and processing main videos...") | |
# intro_clip = resize(VideoFileClip(self.config["intro_video_path"]), (1080, 1080)) | |
# outro_clip = resize(VideoFileClip(self.config["outro_video_path"]), (1080, 1080)) | |
# | |
# necklace_clips = self.create_necklace_clips() | |
# | |
# nto_image_clips = self.process_images_in_directory( | |
# self.config["nto_images_dir"], self.config["image_display_duration"], "Necklace Try-On") | |
# nto_cto_image_clips = self.process_images_in_directory( | |
# self.config["nto_cto_images_dir"], self.config["image_display_duration"], "Clothing Try-On") | |
# makeup_image_clips = self.process_images_in_directory( | |
# self.config["makeup_images_dir"], self.config["image_display_duration"], "Makeup Try-On") | |
# | |
# all_clips = [intro_clip] + necklace_clips + nto_image_clips + nto_cto_image_clips + makeup_image_clips | |
# | |
# final_video = concatenate_videoclips(all_clips, method="compose") | |
# | |
# try: | |
# print("Adding audio...") | |
# audio = AudioFileClip(self.config["audio_path"]) | |
# if audio.duration > final_video.duration: | |
# audio = audio.subclip(0, final_video.duration) | |
# final_video = final_video.set_audio(audio) | |
# except Exception as e: | |
# print(f"Error adding audio: {e}") | |
# | |
# print("Rendering final temp_video...") | |
# final_video.write_videofile( | |
# self.config["output_video_path"], | |
# fps=30, | |
# codec="libx264", | |
# audio_codec="aac", | |
# bitrate="8000k", | |
# threads=4, | |
# preset='ultrafast' | |
# ) | |
# | |
# print(f"Video saved to: {self.config['output_video_path']}") | |
# | |
# | |
# if __name__ == "__main__": | |
# creator = VideoCreator() | |
# creator.create_final_video() | |