License GNU General Public License, version 2
Lines 50
Keywords
AppleScript (2) file management (6) tv (4)
Permissions
Viewable by Everyone
Editable by All Siafoo Users
Note: You are viewing an old version of this snippet. View Latest Version

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

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.

Comments

over 11 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 11 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 11 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]