from moviepy.editor import VideoFileClip, ImageClip, CompositeVideoClip


# Paths to your video and images
video_path = "/Users/jitendersingh/Downloads/background.mp4"
image_paths = [
"/Users/jitendersingh/Downloads/myimages/rounded_image.png",
"/Users/jitendersingh/Downloads/myimages/1.png",
"/Users/jitendersingh/Downloads/myimages/2.png",
"/Users/jitendersingh/Downloads/myimages/3.png",
"/Users/jitendersingh/Downloads/myimages/4.png"
]

# Custom properties for each image (size, direction, position, start time)
images_properties = [
{"size": (800, 800), "direction": "left_to_right", "position": {"x": 100, "y": 250}, "start_time": 0},
{"size": (360, 360), "direction": "right_to_left", "position": {"x": 1000, "y": 250}, "start_time": 0.5},
{"size": (360, 360), "direction": "right_to_left", "position": {"x": 1400, "y": 250}, "start_time": 0.8},
{"size": (360, 360), "direction": "bottom_to_top", "position": {"x": 1000, "y": 650}, "start_time": 1.1},
{"size": (360, 360), "direction": "bottom_to_top", "position": {"x": 1400, "y": 650}, "start_time": 1.3}
]

# Load the video
background_video = VideoFileClip(video_path)

# Function to resize image maintaining aspect ratio
def resize_image(image, max_width, max_height):
image_width, image_height = image.size
if image_width > max_width or image_height > max_height:
aspect_ratio = image_width / image_height
if image_width > max_width:
new_width = max_width
new_height = int(new_width / aspect_ratio)
else:
new_height = max_height
new_width = int(new_height * aspect_ratio)
image = image.resize(newsize=(new_width, new_height))
return image

# Define animations for each image with staggered start times
def image_animation(image, direction, position, start_time):
def animate(t):
duration = 0.5 # Animation duration per image
t_offset = t - start_time
# Set initial positions based on direction
if direction == "left_to_right":
x_start = -image.size[0]
x_end = position['x']
y = position['y']
x = x_start + (x_end - x_start) * min(t_offset / duration, 1)
elif direction == "right_to_left":
x_start = background_video.w
x_end = position['x']
y = position['y']
x = x_start - (x_start - x_end) * min(t_offset / duration, 1)
elif direction == "top_to_bottom":
y_start = -image.size[1]
y_end = position['y']
x = position['x']
y = y_start + (y_end - y_start) * min(t_offset / duration, 1)
elif direction == "bottom_to_top":
y_start = background_video.h
y_end = position['y']
x = position['x']
y = y_start - (y_start - y_end) * min(t_offset / duration, 1)

# After the animation is complete, keep the final position
if t_offset >= duration:
return position['x'], position['y']

return x, y

return animate

# Load and resize images, then apply animation
animated_images = []
for i, image_path in enumerate(image_paths):
# Load and resize the image based on the properties
image = ImageClip(image_path).set_duration(background_video.duration).set_start(images_properties[i]["start_time"])
# Resize the image based on the properties
resized_image = resize_image(image.copy(), images_properties[i]["size"][0], images_properties[i]["size"][1])
# Apply the animation based on the direction and position for each image
animation = image_animation(
resized_image,
images_properties[i]["direction"],
images_properties[i]["position"],
images_properties[i]["start_time"]
)
# Set position animation for each image
animated_image = resized_image.set_position(animation)
animated_images.append(animated_image)

# Combine the video and all animated images
final_video = CompositeVideoClip([background_video] + animated_images)

# Write the output video
output_path = "/Users/jitendersingh/Downloads/output_with_multiple_images.mp4"
final_video.write_videofile(output_path, codec="libx264", fps=30)

print(f"Animation completed! Output saved to: {output_path}")

0 comments:

Post a Comment

 
Top