from moviepy.editor import VideoFileClip, ImageClip, CompositeVideoClip


# Paths to your files

video_path = "/Users/jitendersingh/Downloads/background.mp4"

image_path_1 = "/Users/jitendersingh/Downloads/-original-imah2hyhanydjxva.jpeg"

image_path_2 = "/Users/jitendersingh/Downloads/myimages/-original-imagvfy8frnaspkg.jpeg"


# Max dimensions for the images

max_width = 800  # Set your desired max width

max_height = 200  # Set your desired max height


# Load the video

background_video = VideoFileClip(video_path)


# Load the first image

image_1 = ImageClip(image_path_1).set_duration(background_video.duration)


# Get image dimensions for the first image

image_1_width, image_1_height = image_1.size


# Resize the first image while maintaining its aspect ratio

if image_1_width > max_width or image_1_height > max_height:

    aspect_ratio_1 = image_1_width / image_1_height

    

    if image_1_width > max_width:

        new_width_1 = max_width

        new_height_1 = int(new_width_1 / aspect_ratio_1)

    else:

        new_height_1 = max_height

        new_width_1 = int(new_height_1 * aspect_ratio_1)

    

    image_1 = image_1.resize(newsize=(new_width_1, new_height_1))


# Get updated dimensions for the first image

image_1_width, image_1_height = image_1.size


# Load the second image

image_2 = ImageClip(image_path_2).set_duration(background_video.duration)


# Get image dimensions for the second image

image_2_width, image_2_height = image_2.size


# Resize the second image while maintaining its aspect ratio

if image_2_width > max_width or image_2_height > max_height:

    aspect_ratio_2 = image_2_width / image_2_height

    

    if image_2_width > max_width:

        new_width_2 = max_width

        new_height_2 = int(new_width_2 / aspect_ratio_2)

    else:

        new_height_2 = max_height

        new_width_2 = int(new_height_2 * aspect_ratio_2)

    

    image_2 = image_2.resize(newsize=(new_width_2, new_height_2))


# Get updated dimensions for the second image

image_2_width, image_2_height = image_2.size


# Get video dimensions

video_width, video_height = background_video.size


# Define animation for the first image (left to center in the first half)

def image_1_position(t):

    start_x = -image_1_width  # Start off-screen to the left

    end_x = (video_width // 4) - (image_1_width // 2)  # Center in the first half

    duration = 0.5  # Duration of the animation in seconds

    x = start_x + (end_x - start_x) * min(t / duration, 1)

    y = (video_height - image_1_height) // 2  # Center vertically

    return x, y


# Define animation for the second image (right to center in the second half)

def image_2_position(t):

    start_x = video_width  # Start off-screen to the right

    end_x = (3 * video_width // 4) - (image_2_width // 2)  # Center in the second half

    duration = 0.5  # Duration of the animation in seconds

    x = start_x - (start_x - end_x) * min(t / duration, 1)

    y = (video_height - image_2_height) // 2  # Center vertically

    return x, y


# Apply the animation to both images

animated_image_1 = image_1.set_position(image_1_position)

animated_image_2 = image_2.set_position(image_2_position)


# Combine the video and the animated images

final_video = CompositeVideoClip([background_video, animated_image_1, animated_image_2])


# Write the output video

output_path = "/Users/jitendersingh/Downloads/output_with_two_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