import os from moviepy.editor import VideoFileClip, ImageClip, TextClip,…
- User
import os 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
def create_final_clip(text_center, main_video_path): # Set the size of the final video and other parameters size = (1080, 1920) delayins = 0 # delay for elements to appear fps_size = 60
# 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.mov', 'tools.png', 'arrow.gif'] background_path, frame_path, reaction_video_path, tools_path, gif_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 resize the gif file gif = VideoFileClip(gif_path, has_mask=True).resize( (size[0] - tools_resized.w - txt_clip.w, tools_resized.h)).set_fps(fps_size) gif = gif.loop(duration=main_video_length).set_position((txt_clip.size[0], 0)) # Set the start time for tools, gif, and txt_clip tools_resized, gif, txt_clip = (clip.set_start(delayins) for clip in [tools_resized, gif, 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(2).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, gif, text_image_red], 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).set_audio(main_video.audio) #REMOVE LATER final_clip.duration = 5 #final_clip.duration = main_video_length return final_clip#Testing if works text_center= "AI CREATES WEBSITES OMG wow wow, omg..." main_video_path='C:/Python/VideoCreationTiktok/Source/main_video1.mp4' create_final_clip(text_center, main_video_path).resize(0.5).preview()
- Assistant
To create a zoom in and zoom out effect for the "text_image_red" clip, you can use the
resizemethod with a lambda function to change the size over time. Here's how you can update the code for "text_image_red":# 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(2).resize(width=int(size[0]*0.9)) # Update: Add zoom in and zoom out effect zoom_duration = 2 # Set the desired duration for the zoom effect zoom_factor = 1.1 # Set the desired zoom factor (1.1 means 10% zoom in and out) text_image_red = text_image_red.resize( lambda t: int(size[0] * 0.9 * (1 + (zoom_factor - 1) * abs((t % zoom_duration) - (zoom_duration / 2)) / (zoom_duration / 2))) )Replace the existing "text_image_red" code with the updated code above, and you'll see the text_image_red clip smoothly zoom in and zoom out during its display.
- User
- Assistant
It seems like you are running out of memory, most likely because we were resizing each frame while visually inspecting the video. This can be pretty resource-intensive. Since you want to preview the video, it's a good idea to downscale the video first and simplify the zoom in and zoom out effect.
Here's the updated code:
# 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(2).resize(width=int(size[0]*0.9)) # Update: Add zoom in and zoom out effect zoom_duration = 2 # Set the desired duration for the zoom effect zoom_factor = 1.1 # Set the desired zoom factor (1.1 means 10% zoom in and out) # Lambda function to calculate the new size based on the current time (t) new_size_func = lambda t: int(size[0] * 0.9 * (1 + (zoom_factor - 1) * abs((t % zoom_duration) - (zoom_duration / 2)) / (zoom_duration / 2))) # Update text_image_red to include zoom effect text_image_red = text_image_red.fl(lambda g: lambda t: g(new_size_func(t) / (size[0] * 0.9) * t).resize(new_w=new_size_func(t))) - User
Image created: C:\Python\VideoCreationTiktok\output2.png Traceback (most recent call last): File "c:\Python\VideoCreationTiktok\TESTINGvideo_creation.py", line 93, in create_final_clip(text_center, main_video_path).resize(0.5).preview() File "c:\Python\VideoCreationTiktok\TESTINGvideo_creation.py", line 68, in create_final_clip text_image_red = text_image_red.fl(lambda g: lambda t: g(new_size_func(t) / (size[0] * 0.9) * t).resize(new_w=new_size_func(t))) File "C:\Users\hibri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\moviepy\video\VideoClip.py", line 1033, in fl newclip = VideoClip.fl(self, fl, apply_to=apply_to, keep_duration=keep_duration) File "C:\Users\hibri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\moviepy\Clip.py", line 141, in fl newclip = self.set_make_frame(lambda t: fun(self.get_frame, t)) File "", line 2, in set_make_frame File "C:\Users\hibri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\moviepy\decorators.py", line 15, in outplace f(newclip, *a, **k) File "C:\Users\hibri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\moviepy\video\VideoClip.py", line 740, in set_make_frame self.size = self.get_frame(0).shape[:2][::-1] File "", line 2, in get_frame File "C:\Users\hibri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\moviepy\decorators.py", line 89, in wrapper return f(*new_a, **new_kw) File "C:\Users\hibri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\moviepy\Clip.py", line 98, in get_frame return self.make_frame(t) File "C:\Users\hibri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\moviepy\Clip.py", line 141, in newclip = self.set_make_frame(lambda t: fun(self.get_frame, t)) TypeError: create_final_clip..() takes 1 positional argument but 2 were given PS C:\Python\VideoCreationTiktok>
- Assistant
Apologies for the oversight. The issue is caused by the use of nested lambda functions. Let's switch to a single lambda function and modify the logic accordingly.
Please update the "text_image_red" code with the following:
# 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(2).resize(width=int(size[0] * 0.9)) # Update: Add zoom in and zoom out effect zoom_duration = 2 # Set the desired duration for the zoom effect zoom_factor = 1.1 # Set the desired zoom factor (1.1 means 10% zoom in and out) # Lambda function to calculate the new size based on the current time (t) new_size_func = lambda t: int(size[0] * 0.9 * (1 + (zoom_factor - 1) * abs((t % zoom_duration) - (zoom_duration / 2)) / (zoom_duration / 2))) # Update text_image_red to include zoom effect text_image_red = text_image_red.fl(lambda g, t: g(t if t == 0 else new_size_func(t - 1) / (size[0] * 0.9) * t).resize(new_w=new_size_func(t)))Replace the existing "text_image_red" code with the updated code above, and the TypeError issue should be resolved. You can keep the rest of the code unchanged:
- User
- Assistant
To add a blinking border effect around the "text_image_red" clip, you can use the CompositeVideoClip with the background color and Crossfadein effect. Here's how you can update the code for "text_image_red":
from moviepy.video.fx import crossfadein # ... # 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(2).resize(width=int(size[0]*0.9)) # Same as before: Create a clip that blinks between white and yellow colors as the border border_size = 30 # Set the border size blink_duration = 0.1 # Set the blink duration blinking_border = CompositeVideoClip( [ TextClip(txt=" ", bg_color="white", size=( text_image_red.w + border_size, text_image_red.h + border_size), fontsize=1), TextClip(txt=" ", bg_color="yellow", size=( text_image_red.w + border_size, text_image_red.h + border_size), fontsize=1).set_duration(blink_duration).crossfadein(blink_duration) ] ).loop(duration=2) # Update 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, gif, blinking_border.set_position(("center", "center")), # Add the blinking border text_image_red ], size=size) # ... - User
- Assistant
To add a blinking arrow, you can follow these steps:
- Load the arrow.png image.
- Position it where you want in the video.
- Set the visibility duration (1 second visible, 1 second not visible).
Here's how to update the code:
# ... # Set the paths to the source files source_files = ['Background.jpg', 'Frame.png', 'Reaction1.mov', 'tools.png', 'arrow.gif', 'arrow.png'] background_path, frame_path, reaction_video_path, tools_path, gif_path, arrow_image_path = ( os.path.join(dir_path, file) for file in source_files) # ... # Load and position the arrow image arrow_image = ImageClip(arrow_image_path).set_position(('left', 'center')) arrow_blink_duration = 1 # Set the duration for each blink (1 second visible, 1 second not visible) # Duplicate the arrow_image and set its opacity to 0 (invisible) invisible_arrow = arrow_image.set_opacity(0) # Create a looping blinking arrow with the specified blink duration blinking_arrow = CompositeVideoClip([arrow_image.set_duration(arrow_blink_duration), invisible_arrow.set_start(arrow_blink_duration)]).loop(duration=main_video_length) # Update 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, gif, text_image_red, blinking_arrow], size=size) # Add the blinking_arrow to the final clip # ... - User
Image created: C:\Python\VideoCreationTiktok\output2.png Traceback (most recent call last): File "c:\Python\VideoCreationTiktok\TESTINGvideo_creation.py", line 99, in create_final_clip(text_center, main_video_path).resize(0.5).preview() File "c:\Python\VideoCreationTiktok\TESTINGvideo_creation.py", line 61, in create_final_clip blinking_arrow = CompositeVideoClip([arrow_image.set_duration(arrow_blink_duration), invisible_arrow.set_start(arrow_blink_duration)]).loop(duration=main_video_length) File "", line 2, in loop File "C:\Users\hibri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\moviepy\decorators.py", line 54, in requires_duration raise ValueError("Attribute 'duration' not set") ValueError: Attribute 'duration' not set PS C:\Python\VideoCreationTiktok>
- Assistant
I apologize for the mistake. The issue happens because the duration is not set for the
invisible_arrow. Let's set the duration explicitly for both thearrow_imageand theinvisible_arrow. Here's the updated code:# ... # Create a looping blinking arrow with the specified blink duration blinking_arrow = CompositeVideoClip([ arrow_image.set_duration(arrow_blink_duration), invisible_arrow.set_start(arrow_blink_duration).set_duration(arrow_blink_duration) ]).loop(duration=main_video_length) # Update 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, gif, text_image_red, blinking_arrow], size=size) # Add the blinking_arrow to the final clip # ...Replace the relevant parts of your existing code with the updated code above. The arrow will now blink correctly in the final video, alternating between 1-second visible and 1-second not visible.
- User
- Assistant
Apologies for the confusion. The issue is caused by the use of the
loop()function with CompositeVideoClip. To fix this, you can create a sequence of visible and invisible arrow images and concatenate them for the duration of the main video. Here's the updated code:from moviepy.video.compositing.concatenate import concatenate_videoclips # ... # Create a looping blinking arrow with the specified blink duration visible_arrow = arrow_image.set_duration(1) # Set the duration for visible arrow invisible_arrow = arrow_image.set_opacity(0).set_duration(1) # 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) # Repeat the sequence for half of the main video length blinking_arrow = concatenate_videoclips(arrow_sequence) # Update 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, gif, text_image_red, blinking_arrow], size=size) # Add the blinking_arrow to the final clip # ...