Files
Minat/Minat.php
2019-10-03 01:23:16 -07:00

309 lines
7.4 KiB
PHP
Executable File

<?php
// Minat
// Minat Is Not a Transcoder
$version = file_get_contents(__DIR__."/current_version.txt");
// Includes
require (__DIR__."/functions.php");
// Load preferences
$prefs_file = "/Users/".get_current_user()."/Library/Preferences/org.profiteroles.Minat.php";
if (!file_exists($prefs_file)) {
if (!copy(__DIR__."/prefs.php",$prefs_file)) {
echo "Error creating preferences file";
die;
}
}
$p = unserialize(file_get_contents($prefs_file));
$p['phpbin'] = "/usr/bin/php";
$p['flacbin'] = __DIR__."/bin/flac";
$p['metaflacbin'] = __DIR__."/bin/metaflac";
$p['lamebin'] = __DIR__."/bin/lame";
$p['soxbin'] = __DIR__."/bin/sox";
$p['workdir'] = "/tmp/minat/";
$p['logfile'] = getLogPath("minat.log");
$p['disable_tags'] = 0;
$p['disable_artwork'] = 0;
$p['premature'] = 0;
$p['max_size'] = 1000;
if ($p['mode'] != 1) { $p['premature'] = 1; addline("MODE ".$p['mode']." NOT YET IMPLEMENTED."); }
// Make work dir
if (!is_dir($p['workdir'])) {
addline("mkdir ".$p['workdir']);
mkdir($p['workdir']);
}
// Debug info
addline("---------------------------------------");
addline("Init ".time());
addline(var_export($argv, true));
addline("---------------------------------------");
addline(var_export($p, true));
addline("---------------------------------------");
addline("Minat: ".$version);
addline("PHP: ".PHP_VERSION);
addline(exec("sw_vers | grep ProductVersion"));
// No files dragged
array_shift($argv);
switch (@$argv[0]) {
case NULL:
addline("Launch without argv");
die;
case "Preferences...":
exec(escapeshellarg($p['phpbin'])." ".escapeshellarg(__DIR__."/MinatPrefs.php"));
addline("Launch preferences");
die;
case "Console":
exec("open -n ".__DIR__."/bin/Console.app --args ".$p['logfile']);
die;
case "Check for Updates...":
$curr_version = file_get_contents("https://www.profiteroles.org/git/p/Minat/raw/master/current_version.txt");
if ($curr_version > $version) {
if(askMulti("Minat ".$curr_version." is available (you have ".$version.")", array("Cancel","Download")) == 1) {
exec("open https://www.profiteroles.org/git/p/Minat");
echo "QUITAPP\n";
} else {
die;
}
} else {
echo "\nALERT:Up-to-date|".$version." is the latest version.\n";
die;
}
}
$stamp = md5(serialize($argv))."_".time();
$workdir = $p['workdir'].$stamp."/";
$batchfile = $workdir.$stamp.".sh";
$postfile = $workdir.$stamp.".post.sh";
addline("---------------------------------------");
// Loop over dragged directories
foreach ($argv as $target) {
// If a single .flac file is dragged, treat as if its parent dir was dragged
//if (strtolower(pathinfo($target, PATHINFO_EXTENSION)) == "flac") {
// $target = dirname($target);
// }
$files = array();
foreach(new DirectoryIterator($target) as $file) {
if (strtolower(@array_pop(explode('.', $file))) == "flac") {
$files[] = $file->getpathname();
}
}
if (!$files) {
addline("Dropped folder ".$target." does not contain any flac files");
alert("Dropped folder ".$target." does not contain any flac files");
continue;
}
$label = basename($target);
if (stripos($label,"[flac]") == false) {
$newlabel = $label." [MP3]";
} else {
$newlabel = str_ireplace("[flac]","[MP3]",$label);
}
switch ($p['dest']) {
case 0:
$destdir = $p['workdir'].$stamp."/".$newlabel."/";
break;
case 1:
$destdir = dirname($target)."/".$newlabel."/";
break;
case 2:
$destdir = $p['destpath']."/".$newlabel."/";
break;
}
addline("Dest dir is ".$destdir);
$postdirs[] = $destdir;
if (file_exists($destdir)) {
alert("Destination directory ".$destdir." already exists!");
die;
}
if (!is_dir($workdir)) {
addline("mkdir ".$workdir);
mkdir($workdir);
}
if (!is_dir($destdir) & !$p['premature']) {
addline("mkdir ".$destdir);
mkdir($destdir);
}
if (!$p['disable_artwork']) {
$mimecmd = escapeshellarg($p['metaflacbin'])." --list --block-type=PICTURE ".escapeshellarg($files[0])." | head -10 | grep MIME | sed 's:.*/::'";
addline($mimecmd);
$mime = exec($mimecmd);
if ($mime == " MIME type:") {
$mime = "pic";
}
if (@$mime) {
addline("embedded cover found in ".$files[0]);
$coverdest = $workdir."cover.".$mime;
exec(escapeshellarg($p['metaflacbin'])." --export-picture-to=".escapeshellarg($coverdest)." ".escapeshellarg($files[0]));
if (file_exists($coverdest)) { $usecover = $coverdest; } else { addline ("error extracting file"); }
} elseif ($extcover = findCover($target)) {
addline("external cover file found: ".$extcover);
$width = exec("sips -g pixelWidth ".escapeshellarg($extcover)." | tail -n1 | cut -f4 -d\" \"");
$height = exec("sips -g pixelHeight ".escapeshellarg($extcover)." | tail -n1 | cut -f4 -d\" \"");
if ($width > $p['max_size'] | $height > $p['max_size']) {
addline("resizing ".$extcover);
$coverdest = $workdir."cover.".pathinfo($extcover, PATHINFO_EXTENSION);
exec("sips --resampleHeightWidthMax ".$p['max_size']." ".escapeshellarg($extcover)." --out ".$coverdest." > /dev/null 2>&1");
if (file_exists($coverdest)) { $usecover = $coverdest; } else { addline ("error resizing file"); }
} else {
$usecover = $extcover;
}
}
if ($usecover) {
$covertags = "--ti ".escapeshellarg($usecover);
} elseif ($p['warn_art']) {
alert("No cover artwork was found");
}
}
// Handle one directory
foreach ($files as $file) {
// MODE: Transcode
addline("Processing ".$file);
if ($p['check']) {
if (exec(escapeshellarg($p['flacbin'])." -ts ".escapeshellarg($file)." 2>&1")) {
addline("Skipping corrupted flac: ".$file);
continue;
}
}
$tagcmd = escapeshellarg($p['soxbin'])." --i -a ".escapeshellarg($file);
if (!$p['disable_tags']) {
$rawtags = shell_exec($tagcmd);
addline(var_export($rawtags, true));
$tags = lameFlagBuilder($rawtags);
}
$dest = $destdir.basename($file,".flac").".mp3";
$lockfile = $workdir.md5($target).".".basename($file,".flac").".lock";
$cmd_flac = escapeshellarg($p['flacbin'])." -dcs -- ".escapeshellarg($file);
$cmd_lame = escapeshellarg($p['lamebin'])." -S ".$p['lameopts']." --id3v2-utf16 ".$tags." ".$covertags." - ".escapeshellarg($dest);
$cmd_lock = "touch ".escapeshellarg($lockfile);
$line[] = $cmd_flac." | ".$cmd_lame." ; ".$cmd_lock;
}
}
// No files were found -- nothing to do
if (!@count($line) | $p['premature']) {
addline("No files were found in any dropped folder");
die;
}
// Write batch file for Parallel
addline("Writing to batchfile ".$batchfile);
file_put_contents($batchfile,implode("\n", $line));
$pass[] = escapeshellarg($batchfile);
// Write postflight batch file for Parallel
if ($p['postflight']) {
foreach ($postdirs as $dir) {
switch ($p['postflight']) {
case 1:
$postline[] = "qlmanage -p ".escapeshellarg($dir)."/* > /dev/null 2>&1";
break;
case 2:
$postline[] = "open ".escapeshellarg($dir);
break;
case 3:
$postline[] = "open ".escapeshellarg($dir)." -a ".escapeshellarg($p['handler']);
break;
}
}
addline("Writing to postfile ".$postfile);
file_put_contents($postfile,implode("\n", $postline));
$pass[] = escapeshellarg($postfile);
} else {
$pass[] = 0;
}
$pass[] = escapeshellarg($p['logfile']);
if ($p['ding']) {
$pass[] = 1;
}
$cmd = "open -n ".escapeshellarg(__DIR__."/bin/Parallel.app")." --args ".implode(" ",$pass);
addline($cmd);
exec($cmd);
// We are done
addline("Completed\n");
if (!$p['stay_open']) {
quitme();
}
?>