Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- #
- #script to rename .srt files according to .avi found in same dir according to corresponding serial and episode numbers.
- #With avi and srt containing something like S01E01 or 101 or 01x01.
- # 20110904 sfoutrel[at]gmail.com http://tinozaure-bcs.blogspot.com/2011/09/renommer-les-srt-pour-votre-mediaplayer.html
- #
- <?php
- $oldpwd = getcwd();
- if ( isset($argv[1]) && !empty($argv[1])){
- $wpath = $argv[1];
- }
- else {
- $wpath = ".";
- }
- try {
- chdir($wpath);
- print "going to : $wpath\n\n";
- } catch( exception $err) {
- print "could not chdir to $wpath : $err\n";
- exit;
- }
- $fdir=scandir($wpath);
- unset($fdir[array_search('.',$fdir)]); // Removing the . directory
- unset($fdir[array_search('..',$fdir)]); // Removing the .. directory
- reset($fdir);
- while (list($key, $val) = each($fdir)) {
- if (preg_match("/(\d{1,2}).*?(\d{2,2}).+(avi|mkv|srt)$/", $val, $seasonepisodefile)) {
- $table[(int)$seasonepisodefile[1]][(int)$seasonepisodefile[2]][strtolower($seasonepisodefile[3])]=$val;
- } else {
- print "nothing to do for : $val\n";
- }
- }
- unset($seasonepisodefile);
- if (isset($table)) {
- arsort($table);
- foreach ($table as $seasons) {
- arsort($seasons);
- foreach ($seasons as $episode) {
- if (count($episode) == 1) {
- if ( isset($episode['avi'] )) {
- print chr(27)."[1;31mNO SRT for AVI:".$episode['avi'].chr(27)."[0m \n";
- } else {
- print chr(27)."[1;31mNO AVI for SRT:".$episode['srt'].chr(27)."[0m \n";
- }
- } elseif (count($episode) == 2) {
- print chr(27)."[1;32mSRT:".chr(27)."[0m".$episode['srt'].chr(27)."[1;32m AVI:".chr(27)."[0m".$episode['avi'].chr(27)."[0m\n";
- preg_match("/^(.+)\.avi$/", $episode['avi'], $nameavi);
- preg_match("/^(.+)\.srt$/", $episode['srt'], $namesrt);
- if ( $nameavi[1] == $namesrt[1]) {
- // print chr(27)."[0;36mNothing to do:".$nameavi[1].chr(27)."[0m\n";
- print chr(27)."[0;36mNothing to do.".chr(27)."[0m\n";
- } else {
- print chr(27)."[0;36mTo do.".chr(27)."[0m\n";
- $todo[$episode['srt']]=$nameavi[1].".srt";
- }
- } elseif (count($episode) > 2){
- print chr(27)."[0;31mtoo much files found. No action".chr(27)."[0m";
- }
- }
- }
- if (isset($todo)) {
- print chr(27)."[0m\nTODO LIST:".chr(27)."[0m\n";
- foreach ($todo as $oldname => $newname) {
- try {
- rename($oldname,$newname);
- print chr(27)."[1;32mRenaming $oldname to $newname".chr(27)."[0m\n";
- } catch ( exception $err) {
- print chr(27)."[0;31mcould not rename $oldname to $newname : $err".chr(27)."[0m\n";
- }
- }
- }
- }
- print chr(27)."[0m";
- chdir($oldpwd);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement