Hi!
I just finished a little script that helps me to automatically rename my various TV series episodes, as I constantly got lazy and ended up with hundreds of unnamed episodes on my NAS. If there's anyone out there who is as anal about this as me, have my script:
Code:
#!/bin/bash
VERBOSE="true"
function test {
if [ ! -z "$TEST" ]; then echo $1; fi
}
function real {
if [ -z "$TEST" ]; then echo $1; fi
}
if [ "$1" = "test" ]; then
TEST="true"
else
TEST=""
fi
find . -type d | grep -v "/.*/" | grep "/" | cut -b 3- | while read series; do
cd "$series"
if [ -f .ended ]; then
test "$series has ended."
elif [ -f .url ]; then
real "Updating titles of $series..."
test "Checking $series..."
wget -i .url -O - -o /dev/null | grep "^[[:digit:]]" | cut -d , -f 2,3,6 | sed 's/\"//g' | sed 's/\//-/g' > .eps
errors=`ls | grep -v "^[[:digit:]][[:digit:]]x[[:digit:]][[:digit:]]\.[[:alnum:]][[:alnum:]][[:alnum:]]$" | grep -v "^[[:digit:]][[:digit:]]x[[:digit:]][[:digit:]] - [[:alnum:][:punct:][:space:]]\+\.[[:alnum:]][[:alnum:]][[:alnum:]]$"`
if [ ! -z "$errors" ]; then
echo "Error! Offending files:"
echo "$errors"
else
counter=0
failcounter=0
echo $counter > .count
echo $failcounter > .failcount
ls | grep -v "-" | grep "^[[:digit:]][[:digit:]]x[[:digit:]][[:digit:]]\.[[:alnum:]][[:alnum:]][[:alnum:]]$" | while read file; do
season=`echo $file | cut -b 1-2 | sed 's/^0//'`
episode=`echo $file | cut -b 4-5 | sed 's/^0//'`
extension=`echo $file | cut -b 7-9`
name=`echo ^$season,$episode, | grep -f - .eps | cut -d , -f 3`
if [ ! -z "$name" ]; then
newfile="`printf %02d $season`x`printf %02d $episode` - $name.$extension"
if [ $VERBOSE = "true" ]; then
test "$file would become $newfile."
real "Updating title of $file to $newfile."
fi
counter=$[counter+1]
echo $counter > .count
if [ -z "$TEST" ]; then
echo mv "$file" "$newfile"
fi
else
echo "$file has no title!"
failcounter=$[failcounter+1]
echo $failcounter > .failcount
fi
done
counter=`cat .count`
failcounter=`cat .failcount`
test "$counter files would get new titles, $failcounter would fail."
real "Updated $counter titles, $failcounter unsuccessful."
rm .count
rm .failcount
rm .eps
real "----------"
real
fi
else
test "$series has no URL."
fi
test
cd ..
done
It's really simple. Put it in the base directory of your series and run it, preferably with the parameter "test" first! It will then look through first-level directories and check if there is a .ended file (in which case it won't do anything there) and if there is a .url file pointing to a download URL for the CSV table of the show's episodes. The URLs MUST be from epguides.com - they have a "Titles and Air Dates" site for every show with "list as .csv" in the top menu. A sample would be
http://epguides.com/BreakingBad/ with the CSV
http://epguides.com/common/exportToCSV.asp?rage=18164 . If that URL is present, the script will then look at the files and rename those in the format <%02d $season>x<%02d episode>.<extension> to <%02d $season>x<%02d episode> - <name>.<extension> . If there are any files in the directory that aren't hidden and not in one of those two formats, the series will be skipped and the offending files will be listed. Otherwise, if the parameter "test" is not present, everything is renamed if a title has been found.
Play around with it a bit and adapt it to your needs, it's extremely convenient for me. Tell me about any errors and problems, please. If you're sure that everything will work out alright, remove the "echo" in front of the mv command, which is there as a safeguard.
I thought about automatically getting the URLs and bringing unnamed files into the desired format, but there are way too many things that could go wrong there, unfortunately. And it's not that much work, really.
Regards,
V