Skip to content

Instantly share code, notes, and snippets.

@saksmt
Created December 21, 2014 00:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save saksmt/1dbbc87b538a27684a5c to your computer and use it in GitHub Desktop.
Flac(cue) splitting commandline utility

music-splitter

Simple commandline utility providing support for reqursively walk through all music directories and split large .flac files.

Requirements

Any linux distribution with split2flac, php and find.

Usage

music-splitter <path-to-music-root-dir>

Installation

  1. chmod +x music-splitter
  2. That's it! Now you can see Usage section!
#!/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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment