Explain yourself with reStructured Text, embedded code, graphs, charts, and LaTeX–style math.
Join Siafoo Now
or
Learn More
TV Renamer
| 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 |
| Language | Perl |
# '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
It would be nifty if one can customize the output filename... for example I like my episodes labeled 'Show.Name.s01e01-extra-info'
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 : )
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]