Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rename subtitles files according to tv shows names found in a directory
#!/bin/bash
# Renames subtitles files according to tv shows names found in a directory
# Acceped syntaxes for season/episode are: 304, s3e04, s03e04, 3x04 (case insensitive)
#
# Usage:
# Put this gist somewhere in your $PATH, like /usr/local/bin/subtitles-renamer
# Chmod +x it
# cd ~/YourHolidaysTvShowsWithSubtitles
# subtitles-renamer
#
# Note: zipfiles will be unzipped and .zip will be removed
#
# There are bashisms to work with regular expressions,
# so you really need bash or a shell compatible
# unzip files, maybe there are subtitles in it...
for f in *.zip; do
if [ -e "$f" ]; then
unzip "$f"
rm "$f"
fi
done
# switch into case insensitive
shopt -s nocasematch
# search subtitles
for f in *.{srt,ssa,sub} ; do
if [ -e "$f" ]; then
if [[ "$f" =~ ([0-9]+)([0-9][0-9]) || "$f" =~ s([0-9]+)e([0-9]+) || "$f" =~ ([0-9]+)x([0-9]+) ]]; then
echo "Found '$f'"
let SEASON="10#${BASH_REMATCH[1]}" # eventually delete leading 0
EPISODE=${BASH_REMATCH[2]}
# search for a matching film
for movie in *.{avi,mkv} ; do
if [ -e "$movie" ]; then
if [[ "$movie" =~ ${SEASON}${EPISODE} || "$movie" =~ s0?${SEASON}e${EPISODE} || "$movie" =~ ${SEASON}x${EPISODE} ]]; then
NEW_NAME=`echo "${movie%.*}".srt`
if [ "$f" = "${NEW_NAME}" ]; then
echo " Already ok"
elif [ -e "${NEW_NAME}" ]; then
echo " A file named '${NEW_NAME}' already exist, skipping"
else
mv "$f" "${NEW_NAME}"
echo " Renamed '$f' in '${NEW_NAME}'"
fi
break;
fi
fi
done
fi
fi
done
# reswitch into case sensitive
shopt -u nocasematch
exit 0
@jlecour

This comment has been minimized.

Copy link

jlecour commented Feb 10, 2011

Super, je testerai ça un de ces prochains jours. Merci

@dontdieych

This comment has been minimized.

Copy link

dontdieych commented Dec 11, 2014

Nice script!

Thanks.

@TrueOsiris

This comment has been minimized.

Copy link

TrueOsiris commented Dec 31, 2014

Perfect, mate !
I've added some formats to movies, like m4v and iso
added mds to subs

@gmelikov

This comment has been minimized.

Copy link

gmelikov commented Apr 23, 2016

if you need to run it through all directories recursively:
find . -type d -exec bash -c "cd '{}' && /path/to/script.sh" \;

Thanks for great script!

@auval

This comment has been minimized.

Copy link

auval commented Jun 12, 2017

Thanks! I'm glad I've searched Google before writing such a script myself :)

@JimakosOSX

This comment has been minimized.

Copy link

JimakosOSX commented Mar 18, 2018

wow you saved my day ;)

@Eshaan7

This comment has been minimized.

Copy link

Eshaan7 commented Jan 7, 2019

Love the script!
I wrote a simple python script that basically just renames the subtitle files to the video files found in the directory.
https://github.com/Eshaan7/RenameThemSubs

@hugows

This comment has been minimized.

Copy link

hugows commented Mar 6, 2019

Tested in 2019, tks

@whoamikyo

This comment has been minimized.

Copy link

whoamikyo commented Mar 17, 2019

works, thanks

@thxtex

This comment has been minimized.

Copy link

thxtex commented May 11, 2019

There are some issues with renaming S01E08 when you also have 1080p in the filename.

@isevcik

This comment has been minimized.

Copy link

isevcik commented Jul 26, 2019

There are some issues with renaming S01E08 when you also have 1080p in the filename.

I would suggest to change order in condition:

if [[ "$f" =~ s([0-9]+)e([0-9]+) || "$f" =~ ([0-9]+)x([0-9]+) || "$f" =~ ([0-9]+)([0-9][0-9]) ]]; then

@furumc

This comment has been minimized.

Copy link

furumc commented Mar 6, 2020

Great! It working with east-european charaters also!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.