Last active
May 14, 2023 20:43
-
-
Save peteretelej/8b8f02475c2d8909fd86e7e90d3349ba to your computer and use it in GitHub Desktop.
Fix TV Show Episode Names for Plex
This file contains hidden or 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
#!/bin/bash | |
# | |
# Usage (in bash prompt) | |
# ./plex_file_renamer.sh [directory] | |
# If no directory is provided, the script will run on the | |
# current directory. Otherwise, it will run on the specified | |
# directory, processing all subdirectories recursively. | |
# | |
# This script renames TV show episode files to match | |
# the preferred Plex naming convention. It supports several | |
# | |
# Supported Formats: | |
# - ShowName S01E01 EpisodeTitle.ext | |
# - ShowName [1x01] EpisodeTitle.ext | |
# - ShowName 1x01 EpisodeTitle.ext | |
# - ShowName.101.EpisodeTitle.ext | |
# - ShowName 201 EpisodeTitle.ext | |
# - ShowName 201.ext | |
# These will be renamed To: | |
# - ShowName s01e01 EpisodeTitle.ext | |
# | |
# IMPORTANT: | |
# Please back up your files before running this script. While | |
# the script is designed to avoid data loss, it's always better | |
# to be safe. | |
# | |
# License: | |
# MIT License | |
# Copyright (c) 2023 Peter Etelej | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
# log function | |
log() { | |
if [[ "$1" == "error" ]]; then | |
echo -e "\e[31m[ERROR]\e[0m $2" | |
exit 1 | |
elif [[ "$1" == "warn" ]]; then | |
echo -e "\e[33m[WARNING]\e[0m $2" | |
else | |
echo "[INFO] $1" | |
fi | |
} | |
# rename function | |
rename_files() { | |
for file in "$1"/*; do | |
# If the file is a directory, recurse | |
if [ -d "$file" ]; then | |
rename_files "$file" | |
else | |
filename=$(basename -- "$file") | |
extension="${filename##*.}" | |
filename="${filename%.*}" | |
# Extract parts from the filename | |
if [[ $filename =~ (.*)\ (.*s[0-9]+e[0-9]+)(.*) ]]; then | |
# Format: ShowName s01e01 EpisodeTitle | |
# skip valid format | |
continue | |
elif [[ $filename =~ (.*)\ (.*S[0-9]+E[0-9]+)(.*) ]]; then | |
# Format: ShowName S01E01 EpisodeTitle | |
show_name="${BASH_REMATCH[1]}" | |
episode_info="${BASH_REMATCH[2]//[Ss]/s}" | |
episode_info="${episode_info//[Ee]/e}" | |
episode_title="${BASH_REMATCH[3]}" | |
elif [[ $filename =~ (.*)\ \[([0-9]+x[0-9]+)\](.*) ]]; then | |
# Format: ShowName [1x01] EpisodeTitle | |
show_name="${BASH_REMATCH[1]}" | |
episode_info="${BASH_REMATCH[2]//x/e}" | |
episode_info="s${episode_info}" | |
episode_title="${BASH_REMATCH[3]}" | |
elif [[ $filename =~ (.*)\.([0-9]{3,4})(.*) ]]; then | |
# Format: ShowName.101.EpisodeTitle or ShowName 201 EpisodeTitle | |
show_name="${BASH_REMATCH[1]}" | |
number=${BASH_REMATCH[2]} | |
episode_info="s${number:0:${#number}-2}e${number:${#number}-2}" | |
episode_title="${BASH_REMATCH[3]}" | |
else | |
log warn "Skipping file: $filename does not match pattern" | |
continue | |
fi | |
# Rename the file | |
new_filename="${show_name} ${episode_info}${episode_title}.${extension}" | |
mv -- "$file" "${file%/*}/$new_filename" && log "Renamed $filename to $new_filename" || log error "Failed to rename $filename" | |
fi | |
done | |
} | |
# Check if directory argument is provided | |
if [ "$1" ]; then | |
if [ -d "$1" ]; then | |
echo "Starting script on directory $1..." | |
rename_files "$1" | |
else | |
log error "$1 is not a valid directory" | |
fi | |
else | |
echo "Starting script on current directory..." | |
rename_files "." | |
fi | |
echo "Script finished." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment