Scripts to rename avi files from TV Shows

If you got a lot of ill-named files like:

$ ls
S01E03.The.Sopranos.S01E03.Denial.Anger.Acceptance.avi
The.Sopranos.1x04.Meadowlands.avi
The.Sopranos.S01E01.The.Sopranos.avi
The.Sopranos.S01E02.46.Long.avi
The.Sopranos.S01E06.Pax.Soprana.avi
The_Sopranos.105.College.avi

that your divx player can’t sort properly and you want to end up with something like:

$ ls
1x01.avi  1x02.avi  1x03.avi  1x04.avi  1x05.avi  1x06.avi  

you can use the following script in this way:

cd /path/to/avi/files
../script.sh|sh

and if you want to do the same thing to srt files then:

cd /path/to/srt/files
../script.sh srt|sh

As an example the script will normalize any of the following patterns S01E03, 103,1×03, or just 03 to 1×03.

The contents of script.sh :

#!/bin/bash
ext=$1
if [ ! $ext ]; then
  ext="avi"
fi
FILES=`find . -iname "*.$ext" -printf "%p\n"`
IFS="
"
for i in $FILES; do
 
dirname=`dirname $i`
g=`echo $i|perl -e '<STDIN> =~ m/S\d?(\d)E(\d+)/i; $1 and  print $1 . "x" . $2'`
 
if [ ! $g ]; then
  g=`echo $i|perl -e '<STDIN> =~ m/(\d)x(\d\d)/i; $1 and  print $1 . "x" . $2'`
fi
if [ ! $g ]; then
g=`echo $i|perl -e '<STDIN> =~ m/(\d)(\d\d)/i; $1 and print $1 . "x" . $2;'`
fi
if [ ! $g ]; then
  g=`echo $i|perl -e '<STDIN> =~ m/(\d\d)/i; $1 and print "1x" . $1'`
fi
 
if [ $g ]; then
 g="$dirname/$g.$ext"
 if [[ "$g" != "$i" ]]; then
   echo "if [ ! -e \"$g\" ]; then mv  \"$i\" \"$g\"; fi"
 fi
fi
 
done

Tags: , , , , , , , ,

One Response to “Scripts to rename avi files from TV Shows”

  1. John says:

    Thanks for the script. Just a note on a modification if someone would like a different renaming output, you must change all the “g= (so and so)” lines after

    dirname=dirname $i

    and before

    if [ $g ]; then

    So for example, I like my shows named in the format: “Showname-101″ to account for that you must change:

    g=echo $i|perl -e '<STDIN> =~ m/S\d?(\d)E(\d+)/i; $1 and print $1 . "x" . $2'

    to:

    g=echo $i|perl -e '<STDIN> =~ m/S\d?(\d)E(\d+)/i; $1 and print "Showname-" . $1 . $2'

    Hope this helps!

Leave a Reply