Spaces:
Sleeping
Sleeping
# import secrets | |
# | |
# import numpy as np | |
# from PIL import Image | |
# from moviepy.audio.io.AudioFileClip import AudioFileClip | |
# from moviepy.video.VideoClip import ImageClip, ColorClip, TextClip | |
# from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip | |
# from moviepy.video.compositing.concatenate import concatenate_videoclips | |
# from moviepy.video.fx.all import resize | |
# from moviepy.video.io.VideoFileClip import VideoFileClip | |
# | |
# | |
# class VideoCreator: | |
# def __init__(self): | |
# self.current_date = secrets.token_hex(40) | |
# self.config = { | |
# "intro_video_url": "", | |
# "outro_video_url": "", | |
# "output_video_path": f"/home/ishwor/Desktop/TCP/Virtual_Makeup/images_to_video/resourcesssss/output/{self.current_date}_jewelmirror_cjm.mp4", | |
# "font_path": "/home/ishwor/Desktop/TCP/Virtual_Makeup/images_to_video/resourcesssss/font/PlayfairDisplay-VariableFont.ttf", | |
# "audio_url": "", | |
# "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_image_clip(self, image_path, text, duration): | |
# """Create a temp_video clip from an image with text overlay""" | |
# # Create image clip and resize using the resize function | |
# print("Image path",image_path) | |
# | |
# img_clip = ImageClip(image_path) | |
# img_clip = resize(img_clip, (1080, 1080)) # Using resize from fx.all | |
# img_clip = img_clip.set_duration(duration) | |
# | |
# # Create text overlay | |
# txt_overlay = self.create_text_overlay(text, (1080, 80), duration) | |
# txt_overlay = txt_overlay.set_position(('center', 'bottom')) | |
# | |
# # Combine image and text | |
# final_clip = CompositeVideoClip([img_clip, txt_overlay]) | |
# | |
# return final_clip | |
# | |
# def create_necklace_clips(self, necklace_image_path, backgrounds=None): | |
# if backgrounds is None: | |
# backgrounds = [ | |
# | |
# # Add to your configurations | |
# (245, 245, 245), # Soft White (Perfect for Gold) | |
# (220, 245, 245), # Rich Black (Premium look) | |
# (230, 230, 235), # Pearl Gray (Elegant) | |
# # Alternative premium colors: | |
# # (25, 25, 112), # Midnight Blue | |
# # (44, 49, 51), # Charcoal | |
# # (189, 172, 152), # Champagne | |
# # (241, 235, 218), # Ivory | |
# ] | |
# | |
# necklace_clips = [] | |
# | |
# for bg_color in backgrounds: | |
# # Create background | |
# bg_clip = ColorClip((1080, 1080), col=bg_color) | |
# bg_clip = bg_clip.set_duration(self.config["image_display_duration"]) | |
# | |
# # Create necklace clip | |
# necklace = ImageClip(necklace_image_path) | |
# necklace = resize(necklace, (800, 800)) # Adjust size as needed | |
# necklace = necklace.set_duration(self.config["image_display_duration"]) | |
# | |
# # Center the necklace | |
# necklace = necklace.set_position('center') | |
# | |
# final_clip = CompositeVideoClip([bg_clip, necklace]) | |
# | |
# txt_overlay = self.create_text_overlay("Necklace Preview", (1080, 80), | |
# self.config["image_display_duration"], | |
# is_category=True) | |
# txt_overlay = txt_overlay.set_position(('center', 'bottom')) | |
# | |
# final_clip = CompositeVideoClip([final_clip, txt_overlay]) | |
# 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(self, image_paths, duration, category_name): | |
# clips = [] | |
# print("Image path") | |
# print(image_paths) | |
# | |
# text = category_name | |
# | |
# img_clip = self.create_image_clip(image_paths, text=text, duration=duration) | |
# clips.append(img_clip) | |
# | |
# return clips | |
# | |
# def create_final_video(self, necklace_image, nto_image, cto_images, makeup_images): | |
# print("Loading and processing main videos...") | |
# intro_clip = resize(VideoFileClip(self.config["intro_video_url"]), (1080, 1080)) | |
# outro_clip = resize(VideoFileClip(self.config["outro_video_url"]), (1080, 1080)) | |
# necklace_clips = self.create_necklace_clips(necklace_image) | |
# | |
# print("Processing image arrays...") | |
# nto_image_clips = self.process_images(nto_image, self.config["image_display_duration"], "Necklace Try-On") | |
# cto_image_clips = self.process_images(cto_images, self.config["image_display_duration"], "Clothing Try-On") | |
# makeup_image_clips = self.process_images(makeup_images, self.config["image_display_duration"], "Makeup Try-On") | |
# | |
# all_clips = [intro_clip] + necklace_clips + nto_image_clips + cto_image_clips + makeup_image_clips + [ | |
# outro_clip] | |
# | |
# final_video = concatenate_videoclips(all_clips, method="compose") | |
# | |
# try: | |
# print("Adding audio...") | |
# audio = AudioFileClip(self.config["audio_url"]) | |
# 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=1, | |
# codec="libx264", | |
# audio_codec="aac", | |
# bitrate="400k", | |
# threads=4, | |
# preset='ultrafast' | |
# ) | |
# | |
# print(f"Video saved to: {self.config['output_video_path']}") | |
# | |
# | |
# if __name__ == "__main__": | |
# sample_nto_images = [np.random.randint(0, 255, (1080, 1080, 3), dtype=np.uint8) for _ in range(5)] | |
# sample_cto_images = [np.random.randint(0, 255, (1080, 1080, 3), dtype=np.uint8) for _ in range(5)] | |
# sample_makeup_images = [np.random.randint(0, 255, (1080, 1080, 3), dtype=np.uint8) for _ in range(5)] | |
# | |
# creator = VideoCreator() | |
# creator.create_final_video(sample_nto_images, sample_cto_images, sample_makeup_images) | |