<PRESIDIOANONYMIZEDEMAIL_ADDRESS> You are python expert, your goal is to optimize code…
- User
<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS> You are python expert, your goal is to optimize code without loosing any functionality and variable, this is the code to optimize: import os from moviepy.editor import * from moviepy.video import fx from moviepy.video.fx import all as fx from PIL import ImageOps from create_image_with_text import create_image_with_text
Set the size of the final video
size = (1080, 1920)
Time to delay top attributes
delayins=2 fps_size=10
Set the directory where all the files are stored
dir_path = "C:/Python/VideoCreationTiktok/Source/" dir_path_output = "C:/Python/VideoCreationTiktok/Output/" #File to edit main_video_path = os.path.join(dir_path, "main_video1.mp4")
Set the paths to the source files
background_path = os.path.join(dir_path, "Background.jpg") frame_path = os.path.join(dir_path, "Frame.png") reaction_video_path = os.path.join(dir_path, "Reaction1.mov") tools_path = os.path.join(dir_path, "tools.png") gif_path = os.path.join(dir_path,"arrow.gif")
Get the length of the main video
main_video = VideoFileClip(main_video_path) main_video_length = main_video.duration
Load the background image and resize it to fit the final video size while preserving aspect ratio
background = ImageClip(background_path) background_resized = background.resize(height=size[1]) background_resized = background_resized.set_position('center')
Resize and position the main video
main_video_resized = main_video.resize(height=size[1]*0.73) main_video_resized = main_video_resized.set_position((60, 40))
Load and resize the frame image
frame = ImageClip(frame_path) frame_resized = frame.resize(height=size[1]*0.82) frame_resized = frame_resized.set_position((10, 20)) frame_resized = frame_resized.set_duration(main_video_length)
Load and trim the reaction video to the same length as the main video
reaction_video = VideoFileClip(reaction_video_path, has_mask=True).subclip(0, main_video_length)
Load and resize the tools image
tools = ImageClip(tools_path) tools_resized = tools.resize(width=int(size[0]*0.70)) tools_resized = tools_resized.set_position(('right', 'top'))
Add the text to the final clip
txt_clip = TextClip("All Links", fontsize=tools_resized.h*0.8, font="Impact", color='white',stroke_width=3, stroke_color="black") txt_clip = txt_clip.set_position(('left', 'top')) #txt_clip = txt_clip.set_position((tools_resized.pos[0] + tools_resized.size[0]*0.1 + 5, tools_resized.pos[1] + tools_resized.size[1]*0.1)) txt_clip = txt_clip.set_duration(main_video_length)
Load the gif file as a video clip
gif = VideoFileClip(gif_path, has_mask=True)
Create a custom resizing function that ignores the aspect ratio
- Assistant
- User
- Assistant