Created
November 3, 2023 14:46
-
-
Save JupyterJones/bd3fad94517f224d9d76bb98b0d0a1fd to your computer and use it in GitHub Desktop.
Add_Border_Sound_to_Existing_Video
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeVideoClip | |
import random | |
import glob | |
def add_title_image(video_path, title_image_path, output_path): | |
# Load the video file and title image | |
video_clip = VideoFileClip(video_path) | |
print(video_clip.size) | |
width, height = video_clip.size | |
# Set the desired size of the padded video (e.g., video width + padding, video height + padding) | |
padded_size = (width + 90, height + 90) | |
# Calculate the position for centering the video within the larger frame | |
x_position = (padded_size[0] - video_clip.size[0]) / 2 | |
y_position = (padded_size[1] - video_clip.size[1]) / 2 | |
# Create a blue ColorClip as the background | |
blue_background = ColorClip(padded_size, color=(0, 0, 255)) | |
# Add the video clip on top of the red background | |
padded_video_clip = CompositeVideoClip([blue_background, video_clip.set_position((x_position, y_position))]) | |
padded_video_clip = padded_video_clip.set_duration(video_clip.duration) | |
# Load the title image | |
title_image = ImageClip(title_image_path) | |
# Set the duration of the title image | |
title_duration = video_clip.duration | |
title_image = title_image.set_duration(title_duration) | |
print(video_clip.size) | |
# Position the title image at the center and resize it to fit the video dimensions | |
#title_image = title_image.set_position(("left", "top")) | |
title_image = title_image.set_position((0, -5)) | |
#video_clip.size = (620,620) | |
title_image = title_image.resize(padded_video_clip.size) | |
# Position the title image at the center and resize it to fit the video dimensions | |
#title_image = title_image.set_position(("center", "center")).resize(video_clip.size) | |
# Create a composite video clip with the title image overlay | |
composite_clip = CompositeVideoClip([padded_video_clip, title_image]) | |
# Limit the length to 58 seconds | |
composite_clip = composite_clip.subclip(0, 58) | |
# Load a random background music | |
mp_music = random.choice(glob.glob("/mnt/HDD500/collections/Music/*.mp3")) | |
# Load the background music without setting duration | |
music_clip = AudioFileClip(mp_music) | |
music_clip = music_clip.subclip(0, 58) | |
fade_duration = 1.0 | |
music_clip = music_clip.audio_fadein(fade_duration).audio_fadeout(fade_duration) | |
# Set the audio of the composite clip to the background music | |
composite_clip = composite_clip.set_audio(music_clip) | |
# Export the final video with the background music | |
composite_clip.write_videofile(output_path) | |
# Example usage | |
video_path = "/home/jack/Desktop/HDD500/to-vid/building/slideshowT.mp4" | |
title_image_path = "/home/jack/Desktop/EXPER/static/assets/Title_Image02.png" | |
output_path = "Titled_Final_End.mp4" | |
add_title_image(video_path, title_image_path, output_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment