Created
March 15, 2014 00:34
-
-
Save jmc734/9559973 to your computer and use it in GitHub Desktop.
Use MKVMerge and Handbrake to check every video file in a directory and its subdirectories for the proper audio encoding for playback on a Chromecast and transcode it if it does not.
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 | |
// This is going to take a while | |
set_time_limit(0); | |
/** | |
* Path to MKVMerge executable | |
*/ | |
$mkvmerge = 'mkvmerge.exe'; | |
/** | |
* Path to HandbrakeCLI executable | |
*/ | |
$handbrakeCLI = 'HandbrakeCLI.exe'; | |
/** | |
* Directory to search for files to convert in | |
*/ | |
$directory = 'MOVIES'; | |
/** | |
* Extensions of files to check | |
*/ | |
$extensions = array('mkv','mp4'); | |
/** | |
* Audio encodings supported by Chromecast | |
*/ | |
$allowed = array('AAC', 'MP3'); | |
// Create recursive iterator for all files in the provided directory and its subdirectories | |
$di = new RecursiveDirectoryIterator( | |
$directory, | |
(FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS) | |
); | |
$di = new RecursiveIteratorIterator($di); | |
// Use MKVMerge to check if each file has the proper audio encoding | |
echo "Searching directory...\n"; | |
$toTranscode = array(); | |
foreach($di as $file) { | |
if(in_array(strtolower($file->getExtension()), $extensions)){ | |
$path = $di->getRealPath(); | |
$output = shell_exec("$mkvmerge -i $path\n"); | |
if(preg_match_all('/audio \((.+)\)/i', $output, $matches)){ | |
if(!count(array_intersect($allowed, $matches[1]))){ | |
$toTranscode[] = $path; | |
echo "Added \"$file\" to transcoding queue\n"; | |
} | |
} | |
} | |
} | |
echo 'Press enter to start transcoding...'; | |
if(stream_get_line(STDIN, 1) === false){ | |
exit; | |
} | |
// Transcode each file in the queue. See HandbrakeCLI documentation for explanation of arguments | |
echo "Starting transcoding queue...\n"; | |
foreach($toTranscode as $file) { | |
echo "Transcoding \"$file\"...\n"; | |
passthru("$handbrakeCLI -f mkv --preset \"AppleTV 3\" -i \"$file\" -o \"$file.tmp\""); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment