Guest User

Untitled

a guest
Oct 18th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. <?php
  2. /**
  3. * Media renamer for Obfuscated file names sometimes found when downloading from usenet.
  4. * This script will find scan all directories in $in_path.
  5. * For each directory, it will search for a media file.
  6. * If a media file is found, it will copy that file into the "output path" while also renaming the file to the "basepath" of the containing directory.
  7. */
  8.  
  9. $find_extensions = ['mkv','avi'];
  10. $remove_from_filename = ['-Obfuscated','-postbot'];
  11.  
  12. $in_path = __DIR__;
  13. $out_path = __DIR__ . DIRECTORY_SEPARATOR . 'renamed';
  14.  
  15. if (!file_exists($in_path)) {
  16. die('Input path does not exist');
  17. }
  18.  
  19. if (!file_exists($out_path)) {
  20. mkdir($out_path, 0755, true);
  21. }
  22.  
  23. $iter = new RecursiveIteratorIterator(
  24. new RecursiveDirectoryIterator($in_path, RecursiveDirectoryIterator::SKIP_DOTS),
  25. RecursiveIteratorIterator::SELF_FIRST,
  26. RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
  27. );
  28.  
  29. // Iterate over all directories in a given path and collect them into an array
  30. $paths = [];
  31. foreach ($iter as $path => $dir) {
  32. if ($dir->isDir()) {
  33. $paths[] = $path;
  34. }
  35. }
  36. // Remove output path from list if it's inside the input path!
  37. if (($_key = array_search($out_path, $paths)) !== false) {
  38. unset($paths[$_key]);
  39. }
  40. unset($_key); // clean up
  41.  
  42.  
  43. // For each directory, find a media file inside it.
  44. foreach ( $paths as $path ) {
  45.  
  46. echo "\n---";
  47. echo "\nWorking on: " . $path;
  48.  
  49. // get all files in the given path.
  50. $scanned = array_diff(scandir($path), array('..', '.'));
  51. $filtered = array_filter ($scanned, function( $filename ) use ($find_extensions){
  52. // skip media files with "sample" in the name.
  53. if (stripos($filename, 'sample') !== false) {
  54. return false;
  55. }
  56. if (is_dir($filename)) {
  57. return false;
  58. }
  59. foreach ($find_extensions as $ext) {
  60. return ( $ext === pathinfo($filename, PATHINFO_EXTENSION) );
  61. }
  62. });
  63.  
  64. if (count($filtered) < 1) {
  65. echo "\nNo media files found.";
  66. }
  67.  
  68. if (count($filtered) > 1) {
  69. echo "\nMore than 1 media file found. Skipping.";
  70. continue;
  71. }
  72.  
  73. // Since we're only expecting an array of 1 item, get first item from array.
  74. $media = array_pop($filtered);
  75. $old_fullpath = $path . DIRECTORY_SEPARATOR . $media;
  76.  
  77. echo "\nFound media: " . $old_fullpath;
  78.  
  79. $new_filename = pathinfo($path, PATHINFO_BASENAME);
  80. $new_filename = str_replace($remove_from_filename, '', $new_filename);
  81.  
  82. $new_extension = pathinfo($media, PATHINFO_EXTENSION);
  83. $new_fullpath = $out_path . DIRECTORY_SEPARATOR . $new_filename . "." . $new_extension;
  84.  
  85. // Rename the media file to the name of the directory (keeping the extension)
  86. echo "\nRenamed to: " . $new_fullpath;
  87.  
  88. copy ($old_fullpath, $new_fullpath);
  89.  
  90. }
  91.  
  92. echo "\nComplete.\n";
Add Comment
Please, Sign In to add comment