LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-29-2013, 10:37 AM   #1
Vinter
Member
 
Registered: Feb 2005
Location: Germany
Distribution: Aptosid
Posts: 148

Rep: Reputation: 19
Post [Script] Automatically rename TV series episodes


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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to automatically rename files ckent Ubuntu 12 11-24-2008 06:19 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-23-2008 12:25 AM
automatically rename file uthinkicare Programming 5 12-07-2006 04:19 AM
Rename script superior888 Programming 2 07-31-2006 12:53 PM
program to rename mp3 automatically? buffed317 Linux - Software 1 02-22-2005 08:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration