Skip to content

Instantly share code, notes, and snippets.

@EldonMcGuinness
Last active January 27, 2024 17:30
Show Gist options
  • Save EldonMcGuinness/9720666d2ef9476908b23e33bad1f0af to your computer and use it in GitHub Desktop.
Save EldonMcGuinness/9720666d2ef9476908b23e33bad1f0af to your computer and use it in GitHub Desktop.
renameMovies
#!/bin/bash
#Version 1.16
function guessEdition {
TEMP=$(echo $1 | sed -E 's/.*((special|limited|uncensored|imax|deluxe|criterion|collector|remastered|ultimate|international|german|dircut|uncut|director|alternate|theatrical|extended|unrated|final)).*/\1/gI')
TEMP=$(echo $TEMP | tr '[:upper:]' '[:lower:]')
if [ "$TEMP" != "" ]; then
case $TEMP in
special) TEMP="Special" ;;
limited) TEMP="Limited" ;;
uncensored) TEMP="Uncensored" ;;
imax) TEMP="IMAX" ;;
deluxe) TEMP="Deluxe" ;;
criterion) TEMP="Criterion" ;;
collector) TEMP="Collector's" ;;
ultimate) TEMP="Ultimate Cut" ;;
international) TEMP="International" ;;
german) TEMP="German Cut" ;;
dircut) TEMP="Director's Cut" ;;
uncut) TEMP="Uncut" ;;
director) TEMP="Director's Cut" ;;
alternate) TEMP="Alternate Cut" ;;
theatrical) TEMP="Theatrical Cut" ;;
extended) TEMP="Extended Edition" ;;
unrated) TEMP="Unrated Edition" ;;
final) TEMP="Final Cut" ;;
remastered) TEMP="Remastered" ;;
esac
if [ "$TEMP" == "$(echo $1 | tr '[:upper:]' '[:lower:]')" ]; then
TEMP=""
fi
echo $TEMP
else
echo ""
fi
}
function isYear {
re='^[0-9]{4}$'
if [[ $1 =~ $re ]]; then
return 0
else
return 1
fi
}
function sPrint {
COLOR=0
STYLE=0
if [ "$2" != "" ]; then
case $2 in
black) COLOR=30 ;;
red) COLOR=31 ;;
green) COLOR=32 ;;
yellow) COLOR=33 ;;
blue) COLOR=34 ;;
magenta) COLOR=35 ;;
cyan) COLOR=36 ;;
white) COLOR=37 ;;
esac
fi
if [ "$3" != "" ]; then
case $3 in
normal) STYLE=0 ;;
bold) STYLE=1 ;;
dim) STYLE=2 ;;
inverse) STYLE=3 ;;
underline) STYLE=4 ;;
esac
fi
echo "\033[${STYLE};${COLOR}m${1}\033[0m"
}
function print {
printf "$*" > $(tty)
}
function yesOrNo {
while true; do
read -p "$* "$'\e[1;36m[y/n]:\e[0m'" " -N 1 yn
case $yn in
[Yy]*) return 0 ;;
[Nn]*) return 1 ;;
*) return 0;;
esac
done
}
function getText {
GUESS_CHOICE=0
if [ "$2" != "" ]; then
read -p $'\e[32;1mUse '"$1"$' Guess\e[0m'" $2 "$'\e[1;36m[y/n]:\e[0m'" " -n 1 yn
case $yn in
[Nn]*) GUESS_CHOICE=1;;
esac
else
GUESS_CHOICE=1
fi
if [ $GUESS_CHOICE -eq 1 ]; then
print "\n"
read -p $'\e[31;1m'"$1"$'\e[0m'": " text
test=$(echo $test | sed -E 's/ $//')
print "\033[1A"
#print "\033[1A\033[2K"
else
echo "$2"
fi
echo "$text"
}
function guessTitle {
TEMP=$(echo $1 | sed 's/_/./g' | sed -E 's/\(\)//g' |sed -E 's/\(([0-9]{4})\)/\1/g' | sed -E 's/(.+)[0-9]{4}.*$/\1/' | sed -E 's/(.+)[0-9]{4}[^p].*$/\1/I' | sed -E 's/(.+)(1080|720|576|480)p.*$/\1/' | sed -E 's/(.+)[0-9]{4}$/\1/' | sed -E 's/([^\.]{2,})\./\1 /g' | sed -E 's/^([ai])\./\1 /gI' | sed -E 's/ ([ai0-9])\.([^0-9])/ \1 \2/gI' | sed -E 's/(([a-z0-9]\.)+)/\1 /gI' | sed -E 's/([0-9]+)\. ([0-9]+)/\1.\2/g' | sed -E 's/ / /' | sed -E 's/ $//' | sed -E 's/ ([0-9]+)\./ \1/')
if [ "$TEMP" != "$1" ]; then
echo "$TEMP"
else
echo $1 | sed 's/_/./g' | sed -E 's/\(([0-9]{4})\)/\1/g' | sed -E 's/(.+)[0-9]{4}.*$/\1/' | sed -E 's/(.+)[0-9]{4}[^p].*$/\1/I' | sed -E 's/(.+)(1080|720|576|480)p.*$/\1/' | sed -E 's/(.+)[0-9]{4}$/\1/' | sed -E 's/([^\.]{2,})\./\1 /g' | sed -E 's/^([ai])\./\1 /gI' | sed -E 's/ ([ai0-9])\.([^0-9])/ \1 \2/gI' | sed -E 's/(([a-z0-9]\.)+)/\1 /gI' | sed -E 's/([0-9]+)\. ([0-9]+)/\1.\2/g' | sed -E 's/ / /' | sed -E 's/ $//' | sed -E 's/ ([0-9]+)\./ \1/'
fi
}
function guessYear {
TEMP=$(echo $1 | sed -E 's/\(([0-9]{4})\)/\1/g' | sed -E 's/.+([0-9]{4})[^p].*$/\1/I' | sed -E 's/.+\(?([0-9]{4})\)?$/\1/')
if isYear $TEMP; then
echo "$TEMP"
else
echo ""
fi
}
function listDirContents {
cd "$1"
for j in *; do
print "$(sPrint '|' 'yellow' 'bold')$j \n"
done
cd ..
}
DEPTH=0
function searchForFiles {
SOURCE="*"
if [ $DEPTH -eq 0 ]; then
print "$(sPrint ' Movie Renamer ' 'white' 'inverse')\n"
if [ "$1" != "" ]; then
#print "Source Found: [$1]"
SOURCE="$1"
fi
fi
for i in $SOURCE; do
if [ $DEPTH -eq 0 ]; then
if [ "$i" == "$SOURCE" ]; then
continue;
fi
fi
if [ $DEPTH -eq 0 ]; then
if [ -f "$i" ]; then
print "$(sPrint '[File]' 'yellow' 'bold') $i\n"
elif [ -d "$i" ]; then
print "$(sPrint '[Directory]' 'yellow' 'bold') $i\n"
listDirContents "$i"
print "\n"
fi
if yesOrNo $'\e[34;1mWork on\e[0m'" $i?"; then
print "\n"
else
clear
continue
fi
fi
if [ ! -v TITLE ]; then
TITLE_GUESS=$(guessTitle "$i")
YEAR_GUESS=$(guessYear "$i")
EDITION_GUESS=$(guessEdition "$i")
# if [ "$TITLE_GUESS" == "$i" ]; then
# TITLE_GUESS=""
# fi
if [ "$YEAR_GUESS" == "$i" ]; then
YEAR_GUESS=""
fi
if [ "$EDITION_GUESS" == "$i" ]; then
EDITION_GUESS=""
fi
TITLE=$(getText "Movie Title" "$TITLE_GUESS")
print "\n"
YEAR=$(getText "Movie Year" "$YEAR_GUESS")
print "\n"
EDITION=$(getText "Movie Edition" "$EDITION_GUESS")
print "\n"
TEMPLATE="$TITLE"
if [ "$EDITION" != "" ]; then
TEMPLATE="$TEMPLATE {edition-$EDITION}"
fi
if [ "$YEAR" != "" ]; then
TEMPLATE="$TEMPLATE ($YEAR)"
fi
fi
if [ -d "$i" ]; then
DEPTH=$(($DEPTH+1))
if yesOrNo $'\e[34;1mMove\e[0m'" $i "$'\e[33;1m->\e[0m'" $TEMPLATE"; then
print "\n"
mv "$i" "$TEMPLATE" > /dev/null 2>&1
else
print "Skipping"
DEPTH=$(($DEPTH-1))
if [ $DEPTH -eq 0 ]; then
unset TITLE
clear
fi
continue
fi
cd "$TEMPLATE"
searchForFiles
cd ..
DEPTH=$(($DEPTH-1))
elif [ -f "$i" ]; then
EXTENSION="${i##*.}"
if yesOrNo $'\e[34;1mMove\e[0m'" $i "$'\e[33;1m->\e[0m'" $TEMPLATE.$EXTENSION"; then
print "\n"
if [ $DEPTH -eq 0 ]; then
mkdir "$TEMPLATE" > /dev/null 2>&1
mv "$i" "$TEMPLATE/$TEMPLATE.$EXTENSION" > /dev/null 2>&1
else
mv "$i" "$TEMPLATE.$EXTENSION" > /dev/null 2>&1
fi
else
print "Skipping"
continue
fi
fi
if [ $DEPTH -eq 0 ]; then
unset TITLE
clear
fi
done
}
clear
searchForFiles "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment