View

Rename subtitles script

  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. use Cwd;
  50. #use Getopt::Std;
  51. use Getopt::Long;
  52. my $preview = 0;
  53. my $help = 0;
  54. GetOptions( “dir=s” => \$dir, “sub=s” => \$sub, “preview!” => \$preview, “help!” =>\$help ) or printOptions();
  55. if ($help == 1){
  56.   print “Command list\n;
  57.   printOptions();
  58. }
  59. if ($preview == 1) {print “****** PREVIEW MODE ******\n;}
  60. else {print “****** RENAME MODE *******\n;}
  61. chomp ($currentDir = getcwd());
  62. my $videoDir = $currentDir;
  63. my $subDir = $currentDir;
  64. if (defined $dir){
  65.   $videoDir = $dir;
  66. }
  67. print “dir: $videoDir\n;
  68. if (defined $sub) {
  69.         $subDir = $sub;
  70. }
  71. else {
  72.         $subDir = $videoDir;
  73. }
  74. opendir(DIR, $videoDir) || die “can’t opendir $videoDir: $!”;
  75. while (my $path = readdir(DIR)) {
  76.         push(@videosList, $path) if $path =~ m/\.avi$/;
  77. }
  78. closedir(DIR);
  79. if (!@videosList){
  80.         print\n No videos found ! check your path !\n;
  81.         exit();
  82. }
  83. $temp = getcwd();
  84. opendir(DIR, $subDir) || die “can’t opendir $subDir: $!”;
  85. while (my $path = readdir(DIR)) {
  86.         push(@subsList, $path) if $path =~ m/\.srt$/;
  87. }
  88. closedir(DIR);
  89. if (!@subsList){
  90.         print \n No subtitles found ! check your path !\n;
  91.         exit();
  92. }
  93. if ($videoDir ne $currentDir){
  94.         chdir ($videoDir) || die “Can’t cd to spool: $!\n;
  95. }
  96. foreach $f (@videosList) {
  97.         ################
  98.         # VIDEOS FILES #
  99.         ################
  100.         my @result = getSeasonEpisod($f);
  101.         $season = @result[0];
  102.         $episod = @result[1];
  103.         foreach $s (@subsList) {
  104.                 if ($videoDir ne $subDir) {
  105.                         chdir ($currentDir) || die “Can’t cd to spool: $!\n;
  106.                         chdir ($subDir) || die “Can’t cd to spool: $!\n;
  107.                 }
  108.                 ####################
  109.                 # SUBTITLES FILES  #
  110.                 ####################
  111.                 my @result = getSeasonEpisod($s);
  112.                 $seasonSub = @result[0];
  113.                 $episodSub = @result[1];
  114.                 if ($season == $seasonSub and $episod == $episodSub){
  115.                         my ($vidName) = $f =~ m/(.*)\.avi$/;
  116.                         print “Rename: $s –> $vidName.srt”;
  117.                         print \n;
  118.                         if ($preview != 1){
  119.                                 rename($s,“$vidName.srt”) || die “Cannot rename $s: $!”;
  120.                         }
  121.                 }
  122.         }
  123. }
  124. # get season and episode
  125. sub getSeasonEpisod {
  126.         my $season = 0;
  127.         my $episod = 0;
  128.         # format ABB
  129.         if(@_[0] =~ /(\d)(\d\d)/){
  130.                 $season = $1;
  131.                 $episod = $2;
  132.         }
  133.         # format sAAeBB
  134.         elsif (@_[0] =~ /[sS](\d\d)[eE](\d\d)/){
  135.                 $season = $1;
  136.                 $episod = $2;
  137.         }
  138.         # format AxBB
  139.         elsif (@_[0] =~ /(\d)[xX](\d\d)/){
  140.                 $season = $1;
  141.                 $episod = $2;
  142.         }
  143.         else {
  144.                 print “Video format Not Match ! \n;
  145.         }
  146.         return ($season,$episod);
  147. }
  148. # print options
  149. sub printOptions(){
  150.   print “-h print command list\n;
  151.   print “-d (-dir) <videos directory>\n;
  152.   print “-s (-sub) <subtitles files directory>\n;
  153.   print “-p (–preview)  <without arguments>\n;
  154.   exit();
  155. }

You may also like