Last active
November 15, 2019 22:36
-
-
Save incredimike/cc80d57ed3e5d29fb545174313b55b19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Media renamer for Obfuscated file names sometimes found when downloading from usenet. | |
* This script will find scan all directories in $in_path. | |
* For each directory, it will search for a media file. | |
* 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. | |
*/ | |
$find_extensions = ['mkv','avi']; | |
$remove_from_filename = ['-Obfuscated','-postbot']; | |
$in_path = __DIR__; | |
$out_path = __DIR__ . DIRECTORY_SEPARATOR . 'renamed'; | |
if (!file_exists($in_path)) { | |
die('Input path does not exist'); | |
} | |
if (!file_exists($out_path)) { | |
mkdir($out_path, 0755, true); | |
} | |
$iter = new RecursiveIteratorIterator( | |
new RecursiveDirectoryIterator($in_path, RecursiveDirectoryIterator::SKIP_DOTS), | |
RecursiveIteratorIterator::SELF_FIRST, | |
RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied" | |
); | |
// Iterate over all directories in a given path and collect them into an array | |
$paths = []; | |
foreach ($iter as $path => $dir) { | |
if ($dir->isDir()) { | |
$paths[] = $path; | |
} | |
} | |
// Remove output path from list if it's inside the input path! | |
if (($_key = array_search($out_path, $paths)) !== false) { | |
unset($paths[$_key]); | |
} | |
unset($_key); // clean up | |
// For each directory, find a media file inside it. | |
foreach ( $paths as $path ) { | |
echo "\n---"; | |
echo "\nWorking on: " . $path; | |
// get all files in the given path. | |
$scanned = array_diff(scandir($path), array('..', '.')); | |
$filtered = array_filter ($scanned, function( $filename ) use ($find_extensions){ | |
// skip media files with "sample" in the name. | |
if (stripos($filename, 'sample') !== false) { | |
return false; | |
} | |
if (is_dir($filename)) { | |
return false; | |
} | |
foreach ($find_extensions as $ext) { | |
return ( $ext === pathinfo($filename, PATHINFO_EXTENSION) ); | |
} | |
}); | |
if (count($filtered) < 1) { | |
echo "\nNo media files found."; | |
} | |
if (count($filtered) > 1) { | |
echo "\nMore than 1 media file found. Skipping."; | |
continue; | |
} | |
// Since we're only expecting an array of 1 item, get first item from array. | |
$media = array_pop($filtered); | |
$old_fullpath = $path . DIRECTORY_SEPARATOR . $media; | |
echo "\nFound media: " . $old_fullpath; | |
$new_filename = pathinfo($path, PATHINFO_BASENAME); | |
$new_filename = str_replace($remove_from_filename, '', $new_filename); | |
$new_extension = pathinfo($media, PATHINFO_EXTENSION); | |
$new_fullpath = $out_path . DIRECTORY_SEPARATOR . $new_filename . "." . $new_extension; | |
// Rename the media file to the name of the directory (keeping the extension) | |
echo "\nRenamed to: " . $new_fullpath; | |
copy ($old_fullpath, $new_fullpath); | |
} | |
echo "\nComplete.\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment