Ruben Laguna’s blog

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 are:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<span class='line'>#!/bin/bash
</span><span class='line'>ext=$1
</span><span class='line'>if [ ! $ext ]; then
</span><span class='line'>  ext="avi"
</span><span class='line'>fi
</span><span class='line'>FILES=`find . -iname "*.$ext" -printf "%p\n"`
</span><span class='line'>IFS="
</span><span class='line'>"
</span><span class='line'>for i in $FILES; do
</span><span class='line'>
</span><span class='line'>dirname=`dirname $i`
</span><span class='line'>g=`echo $i|perl -e '&lt;STDIN> =~ m/S\d?(\d)E(\d+)/i; $1 and  print $1 . "x" . $2'`
</span><span class='line'>
</span><span class='line'>if [ ! $g ]; then
</span><span class='line'>  g=`echo $i|perl -e '&lt;STDIN> =~ m/(\d)x(\d\d)/i; $1 and  print $1 . "x" . $2'`
</span><span class='line'>fi
</span><span class='line'>if [ ! $g ]; then
</span><span class='line'>g=`echo $i|perl -e '&lt;STDIN> =~ m/(\d)(\d\d)/i; $1 and print $1 . "x" . $2;'`
</span><span class='line'>fi
</span><span class='line'>if [ ! $g ]; then
</span><span class='line'>  g=`echo $i|perl -e '&lt;STDIN> =~ m/(\d\d)/i; $1 and print "1x" . $1'`
</span><span class='line'>fi
</span><span class='line'>
</span><span class='line'>if [ $g ]; then
</span><span class='line'> g="$dirname/$g.$ext"
</span><span class='line'> if [[ "$g" != "$i" ]]; then
</span><span class='line'>   echo "if [ ! -e \"$g\" ]; then mv  \"$i\" \"$g\"; fi"
</span><span class='line'> fi
</span><span class='line'>fi
</span><span class='line'>
</span><span class='line'>done</span>

The script is also available as a gist

Comments

Copyright © 2015 - Ruben Laguna - Powered by Octopress