I want to do 4. Optimize your video editing pipeline: Review your video editing pipeline…
- User
I want to do 4. Optimize your video editing pipeline: Review your video editing pipeline (e.g., the create_final_clip function) to ensure that it's optimized for performance. This may include reducing the number of operations, using more efficient algorithms, or caching intermediate results.
This is the code with the function :
import os import shutil from moviepy.editor import VideoFileClip, ImageClip, TextClip, CompositeVideoClip from moviepy.video.fx import all as fx from create_image_with_text import create_image_with_text from moviepy.video.compositing.concatenate import concatenate_videoclips
Set the size of the final video and other parameters
size = (1080, 1920) delayins = 5 # delay for TOP elements to appear arrow_blink_duration = 0.5 # Set the duration for each blink
def create_final_clip(text_center, main_video_path):
fps_size = 30 # Set the directory where all the files are stored dir_path = 'C:/Python/VideoCreationTiktok/Source/' dir_path_output = 'C:/Python/VideoCreationTiktok/Output/' # Set the paths to the source files source_files = ['Background.jpg', 'Frame.png', 'Reaction1.mp4', 'tools.png', 'arrow.png'] background_path, frame_path, reaction_video_path, tools_path, arrow_image_path = ( os.path.join(dir_path, file) for file in source_files) # Get the length of the main video main_video = VideoFileClip(main_video_path) main_video_length = main_video.duration # Load and resize the background and main video background_resized = ImageClip(background_path).resize(height=size[1]).set_position('center') main_video_resized = main_video.resize(height=size[1] * 0.73).set_position((60, 40)) # Load and resize the frame image frame_resized = ImageClip(frame_path).resize(height=size[1] * 0.82).set_position((10, 20)).set_duration( main_video_length) # Load and trim the reaction video reaction_video = VideoFileClip(reaction_video_path, has_mask=True).subclip(0, main_video_length) # Load and resize the tools image tools_resized = ImageClip(tools_path).resize(width=int(size[0] * 0.70)).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').set_position(('left', 'top')).set_duration(main_video_length) # Load and position the arrow image arrow_image = ImageClip(arrow_image_path).set_position(('left', 'center')) visible_arrow = arrow_image.set_duration(arrow_blink_duration) # Set the duration for visible arrow invisible_arrow = arrow_image.set_opacity(0).set_duration(arrow_blink_duration) # Set the duration for invisible arrow # Create a sequence of visible and invisible arrow images and concatenate them arrow_sequence = [visible_arrow, invisible_arrow] * int(main_video_length * 2 / arrow_blink_duration) # Repeat the sequence for all lenght of the main video length blinking_arrow = concatenate_videoclips(arrow_sequence).resize((size[0] - tools_resized.w - txt_clip.w, tools_resized.h)).set_position((txt_clip.size[0], 0)) # Set the start time for tools, blinking_arrow, and txt_clip tools_resized, blinking_arrow, txt_clip = (clip.set_start(delayins) for clip in [tools_resized, blinking_arrow, txt_clip]) # Create text clip overlay + Resize text_clip_red to 90% image_path = create_image_with_text(text_center) text_image_red = ImageClip(image_path).set_position(('center', 'center')).set_duration(0.5).resize(width=int(size[0]*0.9)) # Create the final clip final_clip = CompositeVideoClip([background_resized, main_video_resized, frame_resized, reaction_video.set_audio(None), tools_resized.set_duration(main_video_length), txt_clip, text_image_red, blinking_arrow], size=size) # Apply the 'Summer' color effect and set the audio final_clip = fx.lum_contrast(final_clip, lum=0, contrast=0.2, contrast_thr=190) final_clip = final_clip.set_audio(main_video.audio) #REMOVE LATER #final_clip.duration = 5 final_clip.duration = main_video_length return final_clip - Assistant