Video Renamer

This script allows you to rename and edit metadata in .mp4 video files, in bulk.

Usage

Optimising videos for ranking for specific keywords on YouTube requires various elements to be configured for best effect. The video file name should include the keyword, as well as the file meta data. If you’re creating videos in bulk, this can be a time-consuming task. But this script takes the pain away!

Prerequisites

You will need to install the “moviepy” and “mutagen” libraries.

pip install moviepy
pip install mutagen

How to use it

  1. Save the script below in the folder of your choice.
  2. Create a new folder called “Videos”, and save all the videos that you want to optimise there.
  3. Open the script in Notepad++, and replace the “INSERT YOUR KEYWORD HERE” sections with your own keyword(s). You can optionally add additional tags in the tags section (TAG1, TAG2 etc, separated by commas).
  4. Run your script – your videos will be replaced by the modified videos.
import os
from moviepy.editor import VideoFileClip
from mutagen.mp4 import MP4

# Specify the folder where your mp4 videos are located
folder_path = "Videos"

# Specify the keyword you want to add to the video names
keyword = "INSERT YOUR KEYWORD HERE"

# Get a list of all files in the folder
files = os.listdir(folder_path)

# Loop through the files, edit metadata, and rename them
for i, file_name in enumerate(files):
    if file_name.endswith(".mp4"):
        # Generate the new file name with the keyword and index
        new_file_name = f"{keyword}-{i + 1}.mp4"

        # Construct the full paths to the old and new files
        old_file_path = os.path.join(folder_path, file_name)
        new_file_path = os.path.join(folder_path, new_file_name)

        # Edit metadata
        clip = VideoFileClip(old_file_path)

        # Create a temporary file with the edited video
        temp_file_path = os.path.join(folder_path, f"temp_{i + 1}.mp4")
        clip.write_videofile(temp_file_path, audio=True)

        # Set metadata using mutagen
        mp4 = MP4(temp_file_path)
        mp4['\xa9nam'] = "INSERT YOUR KEYWORD HERE"  # Title
        mp4['\xa9alb'] = "INSERT YOUR KEYWORD HERE"  # Subtitle
        mp4['\xa9gen'] = "INSERT YOUR KEYWORD HERE,TAG1, TAG2, TAG3"  # Tags
        mp4['\xa9cmt'] = "INSERT YOUR KEYWORD HERE"  # Comments
        mp4.save()

        # Remove the old file
        os.remove(old_file_path)

        # Rename the temporary file to the new name
        os.rename(temp_file_path, new_file_path)

        print(f"Edited and Renamed: {file_name} -> {new_file_name}")