Skip to content

Instantly share code, notes, and snippets.

@007revad
Last active November 26, 2022 01:46
Show Gist options
  • Save 007revad/234b5bd938c032f1f5dd8d88dcc240f7 to your computer and use it in GitHub Desktop.
Save 007revad/234b5bd938c032f1f5dd8d88dcc240f7 to your computer and use it in GitHub Desktop.
Rename subs in season downloads and move up to the season's mkv/mp4 folder
#!/usr/bin/env bash
#--------------------------------------------------------------------------------------------
# Rename English subtitles in TV season's Subs folder and move up to season's mkv/mp4 folder
#
# Author: 007revad
# Gist on Github: https://gist.github.com/007revad
# Script verified at https://www.shellcheck.net/
#--------------------------------------------------------------------------------------------
if [[ -n $1 ]] && [[ -d $1 ]]; then
Show="$1"
else
echo Invalid argument: "$1"
exit 255
fi
cd "$Show" || { echo "cd $Show failed"; exit 255; }
if [[ -d ${Show}/Subs ]]; then
cd Subs || { echo "cd Subs failed"; exit 255; }
Subs_List=$(ls)
else
echo No Subs folder exists
exit
fi
copysubs() {
count=$(ls -Uba1 | grep -cE '.srt|.ssa|.ass')
if [[ ${count} -eq 1 ]]; then
# Only 1 subtitle file in directory so rename to match video file
# Skip if $Show"/"$Dir"."$extension exists already in video directory
if [[ ! -f ${Show}/${Dir}"."${extension} ]]; then
cp "${File}" "${Show}"/"${Dir}"."${extension}"
echo Copied: "${Dir}"."${extension}"
else
echo Skipping existing "${language}" ."${extension}" file:
echo "${Dir}"."${extension}"
fi
elif [[ ${count} -gt 1 ]]; then
# Multiple subtitle files in directory so append original sub name
# Skip if ${Show}/${Dir}_${File} exists already in video directory
if [[ ! -f ${Show}/${Dir}_${File} ]]; then
cp "${File}" "${Show}"/"${Dir}"_"${File}"
echo "Copied: ${Dir}_${File}"
else
echo Skipping existing "${language}" ."${extension}" file:
echo "${Dir}"_"${File}"
fi
fi
}
# TV season with subtitle files in episode directories, in Subs directory
for Dir in ${Subs_List}
do
(
if [[ -d ${Dir} ]]; then
cd "${Dir}" || { echo cd "${Dir}" failed; exit; }
Srt_List=$(ls)
# Check if any subs in Subs folder have eng in their name
# so we can skip subs with other languages in their name
engexist=$(ls -Uba1 | grep -cE 'eng|Eng|ENG')
for File in ${Srt_List}
do
if [[ -f ${File} ]]; then
extension="${File##*.}"
filename="${File%.*}"
# .srt .ssa or .ass subtitles only
if [[ ${extension} =~ (srt|ssa|ass) ]]; then
# Only want English subtitles
# Check if lowercase filename contains eng
if [[ ${filename,,} == *"eng"* ]]; then
language="English"
copysubs
elif [[ ! ${engexist} -gt 0 ]]; then
# Subtitles don't have any language in their name
# so grab what's there and hope they're English
language="unknown language"
copysubs
fi
fi
fi
done
fi
)
done
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment