-
-
Save corupta/6891a0ef614b375285df8bdf02a697ce to your computer and use it in GitHub Desktop.
Allow handling subtitles with no season markings, add no-season option to allow this.
This file contains 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
#!/usr/bin/env bash | |
# Renames subtitles files according to tv shows names found in a directory and its subdirectories RECURSIVELY | |
# Acceped syntaxes for season/episode are: 304, s3e04, s03e04, 3x04, Season 03 Episode 04 (case insensitive) | |
# | |
# Usage: | |
# Put this gist somewhere in your $PATH, like /usr/local/bin/subtitles-renamer | |
# Chmod +x it | |
# cd ~/YourHolidaysTvShowsWithSubtitles | |
# subtitles-renamer | |
# or if there are no season numbers in the subtitles use below | |
# subtitles-renamer no-season | |
# (it is still ok if the movie files have season strings) | |
# | |
# Note: zipfiles will be unzipped but .zip will not be removed (can edit to make it so) | |
# | |
# There are bashisms to work with regular expressions, | |
# so you really need bash or a shell compatible | |
mode="${1:-season}" | |
if [[ "$mode" != "season" && "$mode" != "no-season" ]]; then | |
echo "Wrong mode parameter $mode given"; | |
echo "use 'subtitles-renamer' or 'subtitles-renamer <mode>' where mode can be 'season' or 'no-season' (defaults to season)" | |
exit 1; | |
fi | |
# switch into case insensitive | |
shopt -s nocasematch | |
shopt -s extglob | |
do_renaming () { | |
# unzip files, maybe there are subtitles in it... | |
for f in *.zip; do | |
if [ -e "$f" ]; then | |
unzip "$f" | |
# rm "$f" | |
fi | |
done | |
# search subtitles | |
for f in *.{srt,ssa,sub,ass,idx} ; do | |
if [ -e "$f" ]; then | |
fOrg="$f" | |
f="${fOrg//@(240|360|480|720|1080)p}"; | |
# echo "original $fOrg to $f" | |
if [[ "$mode" == "season" ]]; then | |
if [[ "$f" =~ s([0-9]+)e([0-9]+) || "$f" =~ ([0-9]+)x([0-9]+) || "$f" =~ ([0-9]+)([0-9][0-9]) ]]; then | |
echo "Found '$fOrg'" | |
let SEASON="10#${BASH_REMATCH[1]}" # eventually delete leading 0 | |
EPISODE=${BASH_REMATCH[2]} | |
echo "SEASON $SEASON EPISODE $EPISODE" | |
# search for a matching film | |
for movie in *.{avi,mkv,mp4} ; do | |
if [ -e "$movie" ]; then | |
mOrg="$movie" | |
movie="${mOrg//@(240|360|480|720|1080)p}"; | |
if [[ "$movie" =~ ${SEASON}${EPISODE} || "$movie" =~ s0?${SEASON}e${EPISODE} || "$movie" =~ ${SEASON}x${EPISODE} || "$movie" =~ (Season 0?${SEASON} Episode ${EPISODE}) ]]; then | |
NEW_NAME=`echo "${mOrg%.*}.${fOrg##*.}"` | |
if [ "$fOrg" = "${NEW_NAME}" ]; then | |
echo " Already ok" | |
elif [ -e "${NEW_NAME}" ]; then | |
echo " A file named '${NEW_NAME}' already exist, skipping" | |
else | |
mv "$fOrg" "${NEW_NAME}" | |
echo " Renamed '$fOrg' in '${NEW_NAME}'" | |
fi | |
break; | |
fi | |
fi | |
done | |
fi | |
elif [[ "$mode" == "no-season" ]]; then | |
if [[ "$f" =~ ([0-9]+) ]]; then | |
echo "Found '$fOrg'" | |
let EPISODE="10#${BASH_REMATCH[1]}" # eventually delete leading 0 | |
echo "SEASON no-season EPISODE $EPISODE" | |
# search for a matching film | |
for movie in *.{avi,mkv,mp4} ; do | |
if [ -e "$movie" ]; then | |
mOrg="$movie" | |
movie="${mOrg//@(240|360|480|720|1080)p}"; | |
echo "check episode $EPISODE with movie $movie" | |
if [[ "$movie" =~ (Episode ${EPISODE}) || "$movie" =~ e([0]*)${EPISODE}[^0-9] || "$movie" =~ x([0]*)${EPISODE}[^0-9] || "$movie" =~ [^0-9]([0]*)${EPISODE}[^0-9] ]]; then | |
NEW_NAME=`echo "${mOrg%.*}.${fOrg##*.}"` | |
if [ "$fOrg" = "${NEW_NAME}" ]; then | |
echo " Already ok" | |
elif [ -e "${NEW_NAME}" ]; then | |
echo " A file named '${NEW_NAME}' already exist, skipping" | |
else | |
mv "$fOrg" "${NEW_NAME}" | |
echo " Renamed '$fOrg' in '${NEW_NAME}'" | |
fi | |
break; | |
fi | |
fi | |
done | |
fi | |
fi | |
fi | |
done | |
} | |
recurse() { | |
for i in "$1"/*;do | |
if [ -d "$i" ];then | |
echo "dir: $i" | |
pushd "$i" | |
do_renaming | |
popd | |
recurse "$i" | |
elif [ -f "$i" ]; then | |
echo "file: $i" | |
fi | |
done | |
} | |
recurse . | |
# reswitch into case sensitive | |
shopt -u nocasematch | |
shopt -u extglob | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment