|
SageTV Customizations This forums is for discussing and sharing user-created modifications for the SageTV application created by using the SageTV Studio or through the use of external plugins. Use this forum to discuss customizations for SageTV version 6 and earlier, or for the SageTV3 UI. |
|
Thread Tools | Search this Thread | Display Modes |
#1
|
|||
|
|||
rename and move files to the import directory
I use unRaid as a NAS and run a torrent client on it. I used to manually rename and move the TV episodes but that got tiresome and I used the opportunity to learn some perl and regular expressions. I make no claim to be any good at either
It is customized to my quirks and setup but maybe it can be useful to others. It should run fine on linux and windows, I used both to test it. If you find odd comments or variable names it's because I forgot to translate from french usage: Code:
perl tv_rename.pl /path/to/files/to/rename/* Code:
#!/usr/bin/perl #TV series file rename script, v0.9.1, 2011-01-22 #v0.9.1: added forced 2 digits in series and episode numbers, tweaked the handling of " - " for reruns #v0.9.2: use regular expression for "csi" substitution, series name optional in path #v0.9.3: deal with the 6x09 format, # added caps to series first letter of each word, # remove multiple spaces, # check if the dir exists, check if it's a caps issue then create a new one #v0.9.4: replace "_" with " " #v0.9.5: fancy up the ripper info remover a bit using a scalar and deal better with "-" in the ripper's signatures # same / in win and linux, who'd a thunk #v0.9.6: deal with episodes like "201". The episode number MUST be 2 digits for the script to work. #v0.9.7: reworked the seep grabber to limit the problems when a series name ends with numbers # # ToDo # get the parameters from the command line use feature ':5.10'; use strict; use warnings; #config section my $dest_path_tv= "/mnt/disk7/tv"; my $test_mode = 1; my $include_episode_title =1; my $add_series_in_path=1; my $old_name=""; # windows section, deal with * in the arguments which are not expanded in windows # and the "/" vs "\" problem if ($^O eq "MSWin32") { use File::DosGlob; @ARGV = map { my @g = File::DosGlob::glob($_) if /[*?]/; @g ? @g : $_; } @ARGV; #different path under windows $dest_path_tv= "//tower/disk7/tv"; } sub it_is_video($$$) { my $filename = shift; my $extension = shift; $old_name = shift; my $new_name; my $serie; my $seep; my $episode; #print "var <$1><$2><$3><$4><$5><$6><$7><$8><$9><$10>\n"; #remove the path $filename =~ s/(.*\/)//; #replace "_" with " " $filename =~ s/_/ /g; #extract series name, season, episode and episode name. #pattern: s01e01 s1e01 01x01 1x01 $filename =~ m/(.*?)( - )?(S|s)?(\d{1,2})(E|e|X|x)(\d{2})( - )?(.*)/; if (defined($4) and defined ($6)){ print"this is TV pattern 1\n"; $serie=lc($1); $seep= sprintf ('s%#.2ue%#.2u', $4, $6); $episode=$8; } else{ #pattern: 201 separated it, it had the hapit of eating numbers at the end of a series's name ex: V 2009 $filename =~ m/(.*?)( - )?(\d{1,2})(\d{2})( - )?(.*)/; if(defined($3) and defined ($4)){ print"this is TV pattern 2\n"; $serie=lc($1); $seep= sprintf ('s%#.2ue%#.2u', $3, $4); $episode=$6; } else { print "$old_name is not a TV episode\n"; return; } } #episode name cleanup #remove ripper's info my $CRAP = '(HDTV|-H|FEVER|xvid|INTERNAL|SYS|LOL|LOL|\[VTV\]|PROPER|FQM|2HD|notv|FQM|www|therealworld de|tcm[|]|\(|\)|{|})'; #remove "-" between tags but not in "real" episode name $episode =~ s/($CRAP-$CRAP)|$CRAP//gi; #remove leading and lasting "." and " " $episode =~ s/[\. ]*$//g; $episode =~ s/^[\. ]*//g; #remove multiple "." $episode =~ s/\.{2,}/./g; #replace "." by " " $episode =~ s/\./ /g; #remove multiple " " $episode =~ s/\ {2,}/ /g; #series name cleanup #remove leading and lasting "." and " " $serie =~ s/[\. ]*$//g; $serie =~ s/^[\. ]*//g; #remove multiple "." $serie =~ s/\.{2,}/./g; #replace "." by " " $serie =~ s/\./ /g; #remove multiple " " $serie =~ s/\ {2,}/ /g; #rename CSI for BMT data scraper $serie =~ s/^csi$/crime scene investigation/; #capitalize the first letters I wonder if this will help BMT... $serie =~ s/\b([a-z])(\w+)\b/\u$1$2/g; #keep NCIS all in caps because I'm an annoying bastard $serie =~ s/(Ncis)(.*)/\U$1\E$2/; #print "$serie\n"; if ($add_series_in_path==1) { $new_name="$dest_path_tv/$serie/"; my $dir="$dest_path_tv/$serie"; unless(-d $dir){ my $serielc=lc($serie); my $dirlc="$dest_path_tv/$serielc"; #if the directory exists in lower caps, use that one if(-d $dirlc) { $new_name="$dest_path_tv/$serielc/"; } #if not, create with caps else { if ($test_mode==0){ use File::Path; mkpath($dir,1) or print "unable to create $dir ($!)"; } else { print "test mode, will not ";} print "make dir *$dir* here ($!)\n"; } } } else{ $new_name="$dest_path_tv/"; } if ($episode ne "" and $include_episode_title==1) { $new_name="$new_name$serie - $seep - $episode.$extension"; } else{ $new_name="$new_name$serie - $seep.$extension"; } if ($old_name eq $new_name) { print "Name hasn't change, skipping $old_name\n"; } elsif ($test_mode==1) { print "test mode, file is not renamed\n$old_name\n$new_name\n\n"; } elsif (rename($old_name, $new_name)){ print "file moved:\n$old_name to\n$new_name\n\n"; } else { print "unable to rename $old_name to $new_name\nCheck that the destination folder exists and matches caps/now_caps"; } } foreach (@ARGV) { my $old_name = $_; die $@ if $@; my @video = ("avi", "mpg", "mpeg", "mp4"); my $working = $old_name; #remove the file type and save it $working =~ m/(.*\.)(.*$)/; $working = $1; my $extension = $2; #check if the extension matches the known video file type foreach (@video) { if ($_ eq lc($extension)){ it_is_video($working,$extension, $old_name); } } } exit(0); Code:
*/5 * * * * perl /boot/custom/tv_rename.pl /mnt/disk7/torrent/copy_to_tv/* edits: #v0.9.1: added forced 2 digits in series and episode numbers, tweaked the handling of " - " for reruns #v0.9.2: use regular expression for "csi" substitution, series name optional in path #v0.9.3: deal with the 6x09 format, # added caps to series first letter of each word, # remove multiple spaces, # check if the dir exists, check if it's a caps issue then create a new one #v0.9.4: replace "_" with " " #v0.9.5: fancy up the ripper info remover a bit using a scalar and deal better with "-" in the ripper's signatures # same / in win and linux, who'd a thunk #v0.9.6: deal with episodes like "201". The episode number MUST be 2 digits for the script to work. #v0.9.7: reworked the seep grabber to limit the problems when a series name ends with numbers Last edited by Gog; 02-03-2011 at 08:09 PM. |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Some wrong files in video import directory | rerooks | SageTV Software | 4 | 04-27-2009 12:16 PM |
Record Directory Nested in Import Directory | jsonnabend | SageTV Software | 2 | 08-04-2008 06:59 AM |
Any way to move from a recording directory with compressed files? | edbmdave | SageTV Software | 22 | 01-05-2008 05:24 PM |
Import files into SageTV recordings instead of video directory? | rbarr110 | SageTV Software | 11 | 02-10-2006 06:26 PM |
Is it possible to move a file to a specific directory | ldavis | SageTV Software | 0 | 09-20-2003 08:45 AM |