Create function from the code that can be called from other file. function should use…
- User
Create function from the code that can be called from other file. function should use text_center, main_video_path as atributes and return final clip. Use ' instead of “ in final code This is the code :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
def custom_resize_ignore_aspect_ratio(clip, width, height): return clip.resize((width, height))
Set the dimensions for the resized gif
gif_width = size[0]-tools_resized.w-txt_clip.w # desired width of the GIF gif_height = tools_resized.h
Resize the gif using the custom function
gif = custom_resize_ignore_aspect_ratio(gif, gif_width, gif_height)
gif = gif.loop(duration=main_video_length)
gif = gif.set_fps(fps_size)
Set the position of the gif between the text and tools image
txt_clip_position = ('left', 'top') txt_clip_x = 0 txt_clip_y = 0 gif_x = txt_clip_x + txt_clip.size[0] tools_position = ('right', 'top') tools_x = size[0] - tools_resized.size[0] tools_y = 0 gif_y = tools_y gif = gif.set_position((gif_x, gif_y))
DELAY parameter
tools_resized = tools_resized.set_start(delayins) gif = gif.set_start(delayins) txt_clip = txt_clip.set_start(delayins)
Create text clip overlay
image_path = create_image_with_text("AI CREATES WEBSITES OMG wow wow, omg...") text_clip_red = ImageClip(image_path)
Apply the "Summer" color effect to 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_clip_red.set_position(("center", "center"))], size=size)
final_clip = fx.lum_contrast(final_clip, lum=0, contrast=0.2, contrast_thr=190)
Get the audio from the main video
main_audio = main_video.audio
Combine the clips into a final composition with the main video audio
final_clip = final_clip.set_audio(main_audio)
Set the duration of the final clip to the length of the main video
final_clip.duration = main_video_length
#Preview final_clip.resize(0.5).preview()
# Write the final composition to a file with an unused number if file already exists
i = 1
while os.path.exists(os.path.join(dir_path_output, f"my_tiktok_video_{i}.mp4")):
i += 1
final_clip.write_videofile(os.path.join(dir_path_output, f"my_tiktok_video_{i}.mp4"), fps=fps_size)
- Assistant
Here is the code with a function named
create_tiktok_clip: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 def create_tiktok_clip(text_center, main_video_path): # 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/' # 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') # ... rest of the code ... return final_clipNow you can call this function from other files to create a final clip using
text_centerandmain_video_pathas arguments. This function will return the final clip. Please note that you may have to adjust the paths to your local setup and ensure that the necessary modules and functions are imported. - User
Add jumping animation to text image red. Code : 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 = 1
# 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(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, 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) 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 add a jumping animation to the
text_image_red, you can use theset_positionfunction with a lambda function that makes the text bounce up and down. Here's the updated code: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): # ... rest of the 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_duration(0.5).resize(width=int(size[0]*0.9)) # Add jumping animation to text_image_red text_image_red = text_image_red.set_position(lambda t: ('center', 'center' - int(30 * abs(math.sin(2 * math.pi * t))))) # 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) # ... rest of the code ... return final_clip # Testing if it 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()Make sure to import
mathat the beginning of your code:import mathNow the
text_image_redshould have a bouncing animation.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.