|
#!/usr/bin/env php |
|
<?php |
|
|
|
function getCueFile($flacDir, $flacFile, $filter = '.*', $i = 0) { |
|
$cueFiles = `find "$flacDir" -type f -regex ".*cue$" | grep -P "$filter"`; |
|
$cueFiles = explode("\n", $cueFiles); |
|
array_pop($cueFiles); |
|
if ($i < 2) { |
|
if (count($cueFiles) > 1) { |
|
return getCueFile($flacDir, $flacFile, 'flac', ++$i); |
|
} |
|
} else { |
|
$result = []; |
|
foreach ($cueFiles as $cueFile) { |
|
$result[$cueFile] = levenshtein($cueFile, $flacFile); |
|
} |
|
return array_keys($result, min($result))[0]; |
|
} |
|
if (!isset($cueFiles[0])) { |
|
return false; |
|
} |
|
return $cueFiles[0]; |
|
} |
|
|
|
set_time_limit(0); |
|
$startdir = getcwd(); |
|
if (isset($argv[1]) && is_dir($argv[1])) { |
|
if (strpos($argv[1], '/') !== false) { |
|
$startdir = $argv[1]; |
|
} else { |
|
$startdir .= '/' . $argv[1]; |
|
} |
|
} |
|
$startdir .= '/'; |
|
$bigFlacs = `find "$startdir" -type f -size +100M -regex ".*flac$" | grep -v splitted`; |
|
$bigFlacs = explode("\n", $bigFlacs); |
|
$command = 'split2flac -cue "%s" -nask -of \'splitted/@performer - @track - @title.@ext\' -o "%s" "%s"'; |
|
$now = 0; |
|
array_pop($bigFlacs); |
|
$of = count($bigFlacs); |
|
foreach ($bigFlacs as $bigFlac) { |
|
$flacDir = dirname($bigFlac); |
|
$flacDir = str_replace('"', '\\"', $flacDir); |
|
if (`find "$flacDir" -type d | grep splitted` !== null) { |
|
echo "\033[1m>>>\033[0m " . 'Skipping ' . ($now++) . '/' . $of . '...' . PHP_EOL; |
|
continue; |
|
} else { |
|
$cueFile = getCueFile($flacDir, $bigFlac); |
|
if ($cueFile === false) { |
|
echo "\033[1m>>>\033[0m " . 'Skipping ' . ($now++) . '/' . $of . '...' . PHP_EOL; |
|
continue; |
|
} |
|
$cueFile = str_replace('"', '\\"', $cueFile); |
|
$task = sprintf($command, $cueFile, $flacDir, $bigFlac); |
|
echo 'Running "' . $task . '"...' . PHP_EOL; |
|
echo `$task` . PHP_EOL; |
|
sleep(1); |
|
echo "\033[1m>>>\033[0m " . ($now++) . '/' . $of . PHP_EOL; |
|
} |
|
} |
|
echo PHP_EOL . " \033[0;32m*\033[0m Done!" . PHP_EOL; |