Tags: rename,perl

Sort by: Date / Title /

  1. 1 year ago by celos
    1. #!/usr/bin/perl
    2. #
    3. # It's my first script in Perl, so be lenient.
    4. #
    5. # This script works only for TV shows and their subtitles !
    6. # When you want to see your favorite tv show, with its sub, you launch the video
    7. # with your favorite video player, and you add the path of the subtitles.
    8. # Many video players, like mplayer, import automatically the subtitles if they
    9. # have the same name. This script renames the subtitles files (.srt) of a directory
    10. # exactly like the corresponding videos (.avi).
    11. # You won't need to specify the path of the subtitles to your video player anymore.
    12. #
    13. # Example:
    14. # Video: my_video.s04e38.something.dot.something_else.avi
    15. # Subtitle file: video.438.srt
    16. # After running the script, the subtitle will be named:
    17. # my_video.s04e38.something.dot.something_else.srt
    18. #
    19. # The formats supported by the script:
    20. # - sAAeBB or SAAEBB with AA the season and BB the episode.
    21. # - ABB with A the season and BB the episode.
    22. # - AxBB with A the season and BB the episode.
    23. #
    24. # If the script is launched without any arguments, then the videos and subtitles
    25. # directories will be set to the current directory.
    26. #
    27. # Options:
    28. #       
    29. #       -h print command list
    30. #
    31. # -d (-dir) <videos directory>
    32. # Specify the directory where the videos are located.
    33. # If this option is not set, the video directory will be the current directory.
    34. #
    35. # -s (-sub) <subtitles files directory>
    36. # Specify the directory of the subtitles files.
    37. # If this option is not set, the subtitles directory will be the same directory as
    38. # the videos directory. Which means that if the videos directory is not set, it will be
    39. # the current directory.
    40. #
    41. # -p (--preview)  <without arguments>
    42. # Preview mode. If this options is set, the subtiles new names will be display on the
    43. # screen without being renamed.
    44. #
    45. # I hope this script will be as useful as it's to me.
    46. #
    47. # Enjoy your tv shows ;-)
    48. #
    49.  
    50. use Cwd;
    51. #use Getopt::Std;
    52. use Getopt::Long;
    53.  
    54. my $preview = 0;
    55. my $help = 0;
    56. GetOptions( "dir=s" => \$dir, "sub=s" => \$sub, "preview!" => \$preview, "help!" =>\$help ) or printOptions();
    57.  
    58. if ($help == 1){
    59.   print "Command list\n";
    60.   printOptions();
    61. }
    62.  
    63.  
    64. if ($preview == 1) {print "****** PREVIEW MODE ******\n";}
    65. else {print "****** RENAME MODE *******\n";}
    66.  
    67. chomp ($currentDir = getcwd());
    68. my $videoDir = $currentDir;
    69. my $subDir = $currentDir;
    70.  
    71. if (defined $dir){
    72.   $videoDir = $dir;
    73. }
    74. print "dir: $videoDir\n";
    75.  
    76. if (defined $sub) {
    77.         $subDir = $sub;
    78. }
    79. else {
    80.         $subDir = $videoDir;
    81. }
    82.  
    83. opendir(DIR, $videoDir) || die "can't opendir $videoDir: $!";
    84. while (my $path = readdir(DIR)) {
    85.         push(@videosList, $path) if $path =~ m/\.avi$/;
    86. }
    87. closedir(DIR);
    88.  
    89. if (!@videosList){
    90.         print"\n No videos found ! check your path !\n";
    91.         exit();
    92. }
    93.  
    94. $temp = getcwd();
    95. opendir(DIR, $subDir) || die "can't opendir $subDir: $!";
    96. while (my $path = readdir(DIR)) {
    97.         push(@subsList, $path) if $path =~ m/\.srt$/;
    98. }
    99. closedir(DIR);
    100.  
    101. if (!@subsList){
    102.         print "\n No subtitles found ! check your path !\n";
    103.         exit();
    104. }
    105.  
    106. if ($videoDir ne $currentDir){
    107.         chdir ($videoDir) || die "Can't cd to spool: $!\n";
    108. }
    109.  
    110. foreach $f (@videosList) {
    111.                
    112.         ################
    113.         # VIDEOS FILES #
    114.         ################
    115.        
    116.         my @result = getSeasonEpisod($f);
    117.         $season = @result[0];
    118.         $episod = @result[1];
    119.  
    120.         foreach $s (@subsList) {
    121.                
    122.                 if ($videoDir ne $subDir) {
    123.                         chdir ($currentDir) || die "Can't cd to spool: $!\n";
    124.                         chdir ($subDir) || die "Can't cd to spool: $!\n";
    125.                 }
    126.  
    127.                 ####################
    128.                 # SUBTITLES FILES  #
    129.                 ####################
    130.        
    131.                 my @result = getSeasonEpisod($s);
    132.                 $seasonSub = @result[0];
    133.                 $episodSub = @result[1];       
    134.  
    135.                 if ($season == $seasonSub and $episod == $episodSub){
    136.                         my ($vidName) = $f =~ m/(.*)\.avi$/;
    137.                         print "Rename: $s --> $vidName.srt";
    138.                         print "\n";
    139.                         if ($preview != 1){
    140.                                 rename($s,"$vidName.srt") || die "Cannot rename $s: $!";
    141.                         }
    142.                 }
    143.         }
    144. }
    145.  
    146. # get season and episode
    147. sub getSeasonEpisod {
    148.         my $season = 0;
    149.         my $episod = 0;
    150.  
    151.         # format ABB
    152.         if(@_[0] =~ /(\d)(\d\d)/){
    153.                 $season = $1;
    154.                 $episod = $2;
    155.         }
    156.  
    157.         # format sAAeBB
    158.         elsif (@_[0] =~ /[sS](\d\d)[eE](\d\d)/){
    159.                 $season = $1;
    160.                 $episod = $2;
    161.         }
    162.  
    163.         # format AxBB
    164.         elsif (@_[0] =~ /(\d)[xX](\d\d)/){
    165.                 $season = $1;
    166.                 $episod = $2;
    167.         }
    168.        
    169.         else { 
    170.                 print "Video format Not Match ! \n";
    171.         }
    172.        
    173.         return ($season,$episod);
    174. }
    175.  
    176. # print options
    177. sub printOptions(){
    178.   print "-h print command list\n";
    179.   print "-d (-dir) <videos directory>\n";
    180.   print "-s (-sub) <subtitles files directory>\n";
    181.   print "-p (--preview)  <without arguments>\n";
    182.   exit();
    183. }

First / Previous / Next / Last / Page 1 of 1 (1 posteets)