Top And Tail Video Creator
This program concatenates videos together, adding topvideo.mp4 at the start of any video, and tailvideo.mp4 at the end. It does this in bulk for any number of videos.
Usage
This is great for branding your videos with an opening (“top”) and closing (“tail”) sequence.
Prerequisites
Create your opening and closing videos, and name them “topvideo.mp4” and “tailvideo.mp4”.
Now save the videos you want to brand in a folder named “Videos”. Your branded videos will be created and saved in a new folder called “Completed Videos”
You will need to install the “moviepy” library.
pip install moviepy
How to use it
- Save the script below in the folder of your choice
- Create a new folder called “Videos”, and save all the videos that you want branding with a top and tail video.
- Make sure you have the topvideo.mp4 and tailvideo.mp4 saved in the main folder
- Run your script – your new videso will be found in the “Completed Videos” folder.
import os
from moviepy.editor import VideoFileClip, concatenate_videoclips
# Define the input folder for videos and the top and tail videos
videos_folder = "Videos"
top_video_path = "topvideo.mp4"
tail_video_path = "tailvideo.mp4"
# Create the output folder if it doesn't exist
output_folder = "Completed Videos"
os.makedirs(output_folder, exist_ok=True)
# Function to get video codec and resolution
def get_video_info(file_path):
video = VideoFileClip(file_path)
codec = video.fps
resolution = video.size
video.close()
return codec, resolution
# Get the codec and resolution of the first video in the folder
first_video_path = os.path.join(videos_folder, os.listdir(videos_folder)[0])
first_video_codec, first_video_resolution = get_video_info(first_video_path)
# Load the top video and tail video
top_video = VideoFileClip(top_video_path)
tail_video = VideoFileClip(tail_video_path)
# Check if the top video codec and resolution match the first video
if top_video.fps != first_video_codec or top_video.size != first_video_resolution:
# If not, convert the top video to match
top_video = top_video.set_fps(first_video_codec).resize(first_video_resolution)
# Check if the tail video codec and resolution match the first video
if tail_video.fps != first_video_codec or tail_video.size != first_video_resolution:
# If not, convert the tail video to match
tail_video = tail_video.set_fps(first_video_codec).resize(first_video_resolution)
# List to store concatenated videos
concatenated_videos = []
# Loop through all files in the input folder
for filename in os.listdir(videos_folder):
if filename.endswith(".mp4"):
# Load the current video
video_path = os.path.join(videos_folder, filename)
video = VideoFileClip(video_path)
# Concatenate the top video, current video, and tail video
concatenated_video = concatenate_videoclips([top_video, video, tail_video])
# Set the output filename
output_filename = os.path.splitext(filename)[0] + "-edited.mp4"
output_path = os.path.join(output_folder, output_filename)
# Write the concatenated video to the output folder
concatenated_video.write_videofile(output_path, codec="libx264", fps=video.fps)
# Close the video objects
video.close()
concatenated_video.close()
# Close the top and tail video objects
top_video.close()
tail_video.close()
Remember to ensure that the top and tail videos match the format of the videos you are branding.
Leave a Reply