E-Learning Collections in Libraries

I keep seeing posts all over about E-Learning videos and how to add them in an intuitively watchable way. Below is a Python script I wrote to process all the files in the Process folder (you’ll need to change this location to your media location. Mine is G:/Plex/Udemy/Process.) Copy the script below in notepad and save it as whateveryouwant.py mine is processvideo.py

You will need python installed. Run by opening a command prompt at the python file’s location and run python processvideo.py Then I move them to the actual Udemy folder so nothing gets messed up with later files.

The script will take Parent Folder name and loop through each subfolder and each file of those subfolders to rename them to be plex compatible. It recognizes folders that are preceeded by numbers and converts the folders to seasons. Then converts the files within that folder to episodes of that season. Currently it will see 1., 1-, 1 formats (for all numbers, not just 1). It will also remove nonsense like the 01 from 1. 01 Filename.mp4 as long as the 1. prefix is present. If only 01 is present and they are number say 1 - 125 across 12 seasons it will process the the 01 named files by folder. So if file 16 is the first file of season 2 it will rename it to S02E01. This script preserves descriptive episode titles also.

For Example:

Excel 101
1. Introduction
1. Welcome to Excel.mp4
1. 01 Welcome to Excel.mp4
01 Welcome to Excel.mp4

will all be output to

Excel 101
Season 1 - Introduction
S01E01 - Welcome to Excel.mp4

Metadata will always be goobered up. Have to fix that manually. Mine loves to give me the show Seasons from 1960’s Asia,…dunno?

This will work on any system that runs Python e.g. … um all of them

I’m working on scripts that can be added as plugins that will do all of this from within plex. They will be done, er sometime within the decade…

Enjoy :slightly_smiling_face:

import os
import re

def rename_folders_and_files(root_dir):
    print(f'Starting to rename folders and files in: {root_dir}')
    for course_folder_name in os.listdir(root_dir):
        course_folder_path = os.path.join(root_dir, course_folder_name)
        if os.path.isdir(course_folder_path):
            print(f'Processing course folder: {course_folder_path}')
            process_subfolders(course_folder_path)
        else:
            print(f'Skipping non-folder item: {course_folder_path}')

def process_subfolders(course_folder_path):
    season_counter = 1
    for subfolder_name in sorted(os.listdir(course_folder_path)):
        subfolder_path = os.path.join(course_folder_path, subfolder_name)
        if os.path.isdir(subfolder_path):
            print(f'Processing subfolder: {subfolder_path}')
            match = re.match(r'(\d+)[\.\- ](.*)', subfolder_name)
            if match:
                season_number = str(season_counter).zfill(2)
                season_title = match.group(2).strip()
                new_subfolder_name = f'Season {season_number} - {season_title}'
                new_subfolder_path = os.path.join(course_folder_path, new_subfolder_name)
                print(f'Renaming subfolder: {subfolder_path} -> {new_subfolder_path}')
                os.rename(subfolder_path, new_subfolder_path)
                rename_files_in_subfolder(new_subfolder_path, season_number)
                season_counter += 1
            else:
                print(f'No match for subfolder: {subfolder_name}')

def rename_files_in_subfolder(subfolder_path, season_number):
    video_extensions = ['.mp4', '.m4v', '.mov']
    episode_counter = 1
    for file_name in sorted(os.listdir(subfolder_path)):
        file_path = os.path.join(subfolder_path, file_name)
        if os.path.isfile(file_path):
            file_ext = os.path.splitext(file_name)[1].lower()
            if file_ext in video_extensions:
                match = re.match(r'(\d+)[\.\- ](\d+[\.\- ]*)?(.*)', file_name)
                if match:
                    episode_number = match.group(1).zfill(2)
                    redundant_number = match.group(2)
                    episode_title = match.group(3).strip()

                    if redundant_number:
                        new_file_name = f'S{season_number}E{str(episode_counter).zfill(2)} - {episode_title}{file_ext}'
                    else:
                        new_file_name = f'S{season_number}E{str(episode_counter).zfill(2)} - {episode_title}{file_ext}'
                    
                    new_file_path = os.path.join(subfolder_path, new_file_name)
                    print(f'Renaming file: {file_path} -> {new_file_path}')
                    os.rename(file_path, new_file_path)
                    episode_counter += 1
                else:
                    print(f'No match for file: {file_name}')
        else:
            print(f'Skipping non-file item: {file_path}')

root_directory = 'G:/Plex/Udemy/Process'
rename_folders_and_files(root_directory)
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.