from moviepy.editor import VideoFileClip, ImageClip, CompositeVideoClip
from PIL import Image, ImageDraw, ImageFilter
import os

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

# Directory to save modified images
temp_dir = "/Users/jitendersingh/Downloads/temp_images"
os.makedirs(temp_dir, exist_ok=True)

# Function to add shadow and rounded corners (unchanged)
def add_shadow_with_rounded_corners(image_path, output_path, corner_radius, shadow_size):
img = Image.open(image_path).convert("RGBA")
original_size = img.size
scaled_size = (original_size[0] - 2 * shadow_size, original_size[1] - 2 * shadow_size)
img = img.resize(scaled_size, Image.ANTIALIAS)
mask = Image.new("L", scaled_size, 0)
draw = ImageDraw.Draw(mask)
draw.rounded_rectangle((0, 0, scaled_size[0], scaled_size[1]), radius=corner_radius, fill=255)
rounded_img = Image.new("RGBA", scaled_size)
rounded_img.paste(img, (0, 0), mask=mask)
shadow = Image.new("RGBA", original_size, (0, 0, 0, 0))
draw = ImageDraw.Draw(shadow)
draw.rounded_rectangle(
(shadow_size, shadow_size, original_size[0] - shadow_size, original_size[1] - shadow_size),
radius=corner_radius,
fill=(0, 0, 0, 128)
)
shadow = shadow.filter(ImageFilter.GaussianBlur(radius=shadow_size // 2))
output = Image.new("RGBA", original_size, (0, 0, 0, 0))
output.paste(shadow, (0, 0))
output.paste(rounded_img, (shadow_size, shadow_size), mask=mask)
output.save(output_path, format="PNG")

# Modify images (unchanged)
corner_radius = 50
shadow_size = 50
modified_image_paths = []

for image_path in image_paths:
output_path = os.path.join(temp_dir, os.path.basename(image_path))
add_shadow_with_rounded_corners(image_path, output_path, corner_radius, shadow_size)
modified_image_paths.append(output_path)

# Load the video
background_video = VideoFileClip(video_path)

# Animation properties (added out animation details)
images_properties = [
{"size": (700, 700), "direction": "first_half", "position": {"x": 0, "y": 0}, "start_time": 0},
{"size": (380, 350), "direction": "right_to_left", "position": {"x": 1000, "y": 180}, "start_time": 0.5},
{"size": (380, 350), "direction": "right_to_left", "position": {"x": 1400, "y": 180}, "start_time": 0.8},
{"size": (380, 350), "direction": "bottom_to_top", "position": {"x": 1000, "y": 620}, "start_time": 1.1},
{"size": (380, 350), "direction": "bottom_to_top", "position": {"x": 1400, "y": 620}, "start_time": 1.3}
]

# Resize function (unchanged)
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

def image_animation(image, direction, position, start_time):
def animate(t):
duration = 0.5 # Animation duration
out_start_time = background_video.duration - 0.5 # Start out animation 1 second before the video ends

# Out animation logic: Trigger if the current time (`t`) is after `out_start_time`
if t >= out_start_time:
out_duration = 0.5
out_t_offset = t - out_start_time
x = position['x']
y_start = position['y']
y_end = background_video.h + image.size[1]
y = y_start + (y_end - y_start) * min(out_t_offset / out_duration, 1)
return x, y

# Existing animations
t_offset = t - start_time
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)
elif direction == "first_half":
first_half_width = background_video.w // 2
first_half_height = background_video.h // 2
image_width, image_height = image.size

start_x = -image_width
end_x = (background_video.w // 4) - (image_width // 2)
x = start_x + (end_x - start_x) * min(t / duration, 1)
y = (background_video.h - image_height) // 2 + 80
else:
x, y = position['x'], position['y'] # Default position

return x, y
return animate

# Ensure all images have full visibility for the entire video duration
animated_images = []
for i, image_path in enumerate(modified_image_paths):
# Load each image
image = ImageClip(image_path).set_duration(background_video.duration) # Set duration equal to video duration
image = image.set_start(0) # Ensure all images start at the beginning
# Resize the image according to specified properties
resized_image = resize_image(image.copy(), images_properties[i]["size"][0], images_properties[i]["size"][1])
# Create an animation for the resized image
animation = image_animation(
resized_image,
images_properties[i]["direction"],
images_properties[i]["position"],
images_properties[i]["start_time"]
)
# Set the position of the animated image
animated_image = resized_image.set_position(animation)
# Append the animated image to the list
animated_images.append(animated_image)

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

# Set the final video duration to match the background video's duration
final_video = final_video.set_duration(background_video.duration)

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

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


0 comments:

Post a Comment

 
Top