from moviepy.editor import VideoFileClip, ImageClip, CompositeVideoClip
# Paths to your files
video_path = "/Users/jitendersingh/Downloads/background.mp4"
image_path = "/Users/jitendersingh/Downloads/-original-imah2hyhanydjxva.jpeg"
# Max dimensions for the image
max_width = 400 # Set your desired max width
max_height = 200 # Set your desired max height
# Load the video
background_video = VideoFileClip(video_path)
# Load the image
image = ImageClip(image_path).set_duration(background_video.duration)
# Get image dimensions
image_width, image_height = image.size
# Resize the image while maintaining its aspect ratio
if image_width > max_width or image_height > max_height:
# Calculate aspect ratio and resize accordingly
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))
# Get updated image dimensions after resizing
image_width, image_height = image.size
# Get video dimensions
video_width, video_height = background_video.size
# Define animation for the image
def image_position(t):
"""
Calculate the x, y position of the image at time t.
Moves from the left edge to the first half-center of the screen.
"""
start_x = -image_width # Start off-screen to the left
end_x = (video_width // 4) - (image_width // 2) # Center in the first half
duration = 1 # Duration of the animation in seconds
# Interpolate x position linearly during the animation duration
x = start_x + (end_x - start_x) * min(t / duration, 1)
y = (video_height - image_height) // 2 # Center vertically
return x, y
# Apply the animation to the image
animated_image = image.set_position(image_position)
# Combine the video and the animated image
final_video = CompositeVideoClip([background_video, animated_image])
# Write the output video
output_path = "/Users/jitendersingh/Downloads/output_with_animation.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