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
Create a custom resizing function that ignores the aspect ratio
Assistant
Here is the code with a function named create_tiktok_clip:
python
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_clip
User
Assistant
To add a jumping animation to the text_image_red, you can use the set_position function with a lambda function that makes the text bounce up and down. Here's the updated code:
Now you can call this function from other files to create a final clip using text_center and main_video_path as 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.
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
code
# 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()
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 math at the beginning of your code:
python
import math
Now the text_image_red should have a bouncing animation.