Wednesday, December 24, 2008

Rename list of tv episodes

This is a great ruby script for renaming TV episodes. It's based on the Wikipedia tv episode listing format, which looks like this:

== Season 1: 2003 ==
{| class="wikitable" style="width:98%;"
! width="20"| # !! width="250"|Title !! Patient Portrayer(s) !! Surgery Music !! width="120" | Original airdate !! width="80" | Production code

{{Episode list
|OriginalAirDate=July 22, 2003
|Aux1=[[Geoffrey Rivas]]
|Aux2=[[Paint It Black]]—[[The Rolling Stones]]
|ShortSummary=[[Christian Troy|Christian]]'s unethical acceptance of $300,000 from a mobster desperate for a new face put his partnership with [[Sean McNamara|Sean]] in jeopardy and their lives in peril.

This happens to be the listing for the first episode of the first season of Nip/Tuck. Anyway, all you need to do to rename all your episodes from a format like "Show Name s(Season Number)e(Episode Number).avi" to "Season (Number) Episode (Number) - Episode Title.avi" is run this script. First you need to look up the Wikipedia episode listing, then click the "Edit" button at the top of the table. Then save all the text in the editor to a file called "episodes.txt" in the same directory as the video files.


title = ''

ep    = ''

eps   ="episodes.txt").each do |l|

    if l.match(/Title=/)

        title = l.slice(l.index('=')+1, l.length).chop


    if l.match(/ProdCode/)

        ep    = l.slice(l.index('=')+1, l.length).chop

        eps[ep] = title


end'.').each do |f|

    next unless f.match(/\.avi$/)

    if f.match(/s(\d+)e(\d+)/i)

        key = $1.to_i.to_s + $2

        newf = "Season 5 Episode #{$2} - #{eps[key]}.avi"

        File.rename(f, newf)


        puts "No match: #{f}"



Yes, it's short, but I've found myself rewriting it countless times in several languages. It is highly useful.

No comments: