Advertisement
Guest User

srtrename.php

a guest
Sep 5th, 2011
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.70 KB | None | 0 0
  1. #!/usr/bin/php
  2. #
  3. #script to rename .srt files according to .avi found in same dir according to corresponding serial and episode numbers.
  4. #With avi and srt containing something like S01E01 or 101 or 01x01.
  5. # 20110904 sfoutrel[at]gmail.com http://tinozaure-bcs.blogspot.com/2011/09/renommer-les-srt-pour-votre-mediaplayer.html
  6. #
  7. <?php
  8. $oldpwd = getcwd();
  9.  
  10. if ( isset($argv[1]) && !empty($argv[1])){
  11.   $wpath = $argv[1];
  12. }
  13. else {
  14.   $wpath = ".";
  15. }
  16.  
  17. try {
  18.   chdir($wpath);
  19.   print "going to : $wpath\n\n";
  20. } catch( exception $err) {
  21.   print "could not chdir to $wpath : $err\n";
  22.   exit;
  23. }
  24.  
  25. $fdir=scandir($wpath);
  26. unset($fdir[array_search('.',$fdir)]);  // Removing the . directory
  27. unset($fdir[array_search('..',$fdir)]); // Removing the .. directory
  28. reset($fdir);
  29.  
  30. while (list($key, $val) = each($fdir)) {
  31.     if (preg_match("/(\d{1,2}).*?(\d{2,2}).+(avi|mkv|srt)$/", $val, $seasonepisodefile)) {
  32.       $table[(int)$seasonepisodefile[1]][(int)$seasonepisodefile[2]][strtolower($seasonepisodefile[3])]=$val;
  33.     } else {
  34.       print "nothing to do for : $val\n";
  35.     }
  36. }
  37. unset($seasonepisodefile);
  38.  
  39. if (isset($table)) {
  40.   arsort($table);
  41.   foreach ($table as $seasons) {
  42.     arsort($seasons);
  43.     foreach ($seasons as $episode) {
  44.       if (count($episode) == 1) {
  45.         if ( isset($episode['avi'] )) {
  46.           print chr(27)."[1;31mNO SRT for AVI:".$episode['avi'].chr(27)."[0m \n";
  47.         } else {
  48.           print chr(27)."[1;31mNO AVI for SRT:".$episode['srt'].chr(27)."[0m \n";      
  49.         }
  50.       } elseif (count($episode) == 2) {
  51.         print chr(27)."[1;32mSRT:".chr(27)."[0m".$episode['srt'].chr(27)."[1;32m AVI:".chr(27)."[0m".$episode['avi'].chr(27)."[0m\n";
  52.         preg_match("/^(.+)\.avi$/", $episode['avi'], $nameavi);
  53.         preg_match("/^(.+)\.srt$/", $episode['srt'], $namesrt);
  54.         if ( $nameavi[1] == $namesrt[1]) {
  55. //          print chr(27)."[0;36mNothing to do:".$nameavi[1].chr(27)."[0m\n";
  56.           print chr(27)."[0;36mNothing to do.".chr(27)."[0m\n";
  57.         } else {
  58.           print chr(27)."[0;36mTo do.".chr(27)."[0m\n";
  59.           $todo[$episode['srt']]=$nameavi[1].".srt";
  60.         }
  61.       } elseif (count($episode) > 2){
  62.         print chr(27)."[0;31mtoo much files found. No action".chr(27)."[0m";
  63.       }
  64.     }
  65.   }
  66.   if (isset($todo)) {
  67.     print chr(27)."[0m\nTODO LIST:".chr(27)."[0m\n";
  68.     foreach ($todo as $oldname => $newname) {
  69.       try {
  70.         rename($oldname,$newname);
  71.         print chr(27)."[1;32mRenaming $oldname to $newname".chr(27)."[0m\n";
  72.       } catch ( exception $err) {
  73.         print chr(27)."[0;31mcould not rename $oldname to $newname : $err".chr(27)."[0m\n";
  74.       }
  75.     }
  76.   }
  77. }
  78.  
  79. print chr(27)."[0m";
  80. chdir($oldpwd);
  81. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement