License New BSD license
Lines 54
Keywords
AppleScript (2) file management (6) tv (4)
Permissions
Viewable by Everyone
Editable by All Siafoo Users
Hide
Easily highlight source code for your blog with our Syntax Highlighter. Join Siafoo Now or Learn More

TV Renamer Atom Feed 1

In Brief Renames TV episode files to a standardized format (i.e. Show Name - 1x01). On OS X, uses AppleScript to set the old name of the file to the comments section (if comments is blank) and hides the file extension.... more
# 's
 1#!/usr/bin/perl
2
3# Copyright 2008 David Isaacson
4# BSD License
5
6use File::Spec;
7use File::Copy;
8
9foreach $path (@ARGV){
10$oldpath = $path;
11
12($volume,$directory,$file) = File::Spec->splitpath($path);
13$oldfile = $file;
14
15# S01E01, S1E01
16if ($file =~ /(.+?)[\.\-_ ]?S0?([0-9]+)E([0-9]+).*\.(.{2,4})/i){
17 $file = "$1 - ${2}x${3}.$4";
18}
19
20# 1x01
21elsif ($file =~ /(.+?)[\.-_ ]0?([0-9]+)x([0-9]+).*\.(.{2,4})/i){
22 $file = "$1 - ${2}x${3}.$4";
23}
24# 101
25elsif ($file =~ /(.+?)[\.-_ ]0?([0-9])([0-9]+).*\.(.{2,4})/i){
26 $file = "$1 - ${2}x${3}.$4";
27}
28
29# Remove Periods and Underbars
30$file =~ s/[\._]/ /ig;
31# Not the last period though (assume there was one)
32$file =~ s/\ ([^\ ]*)$/.\1/i;
33
34# Remove more than one dash
35$file =~ s/(- )+/- /g;
36
37# Remove multiple spaces
38$file =~ s/ +/ /g;
39
40# Title Case
41$file =~ s/(\b)([a-z])/\1\u\2/g;
42$file =~ s/\.(.{2,4})$/.\l\1/;
43
44$path = File::Spec->catpath($volume,$directory,$file);
45
46print "$path\n";
47move($oldpath,$path);
48
49# OS X only ###############################
50
51$comments = $oldfile;
52
53# Set Comments to Old Path
54use File::Spec::Unix;
55my $absfile=File::Spec::Unix->rel2abs($path);
56-e $absfile or die "No such file: $absfile";
57
58my $applescript=<<DNE;
59set mypath to "$absfile"
60set macpath to (POSIX file mypath) as string
61tell application "Finder"
62 if the comment of file macpath is "" then
63 set the comment of file macpath to "$comments"
64 end if
65 set extension hidden of file macpath to true
66end tell
67DNE
68
69`/usr/bin/osascript -sh <<'END'\n$applescript\nEND`;
70
71# End OS X Only ###########################
72
73}

Renames TV episode files to a standardized format (i.e. Show Name - 1x01). On OS X, uses AppleScript to set the old name of the file to the comments section (if comments is blank) and hides the file extension.

A newer Python version of this code can be found at TV Renamer 2.

Comments

over 7 years ago (05 Jan 2009 at 02:50 PM) by Stou S.
Can you please port this thing to python for those of us not cool enough to have a mac?

It would be nifty if one can customize the output filename... for example I like my episodes labeled 'Show.Name.s01e01-extra-info'
over 7 years ago (07 Jan 2009 at 11:30 AM) by David Isaacson
It's perl, is that not good enough? The OS X part will never work outside of that OS, as it uses AppleScript and metadata specific (I think) to the HFS+ filesystem. There might be an equivalent or more general way to add metadata in other filesystems, but I don't know what it is.

Customizing the filename is kinda complicated the way things are set up right now; I didn't really intend this to be used by everyone in the world... but I'll see what I can do : )
over 7 years ago (07 Jan 2009 at 01:10 PM) by David Isaacson
Ok, I've ported it to Python, you should be able to do all your fun customization now.

In the future it would be fun to have it recognize episode titles, possibly by using a dictionary to weed out garbage like Xvid-[VTV]