diff --git a/src/library/Command/AbstractCommand.php b/src/library/Command/AbstractCommand.php index 23aeac4..7273d4a 100644 --- a/src/library/Command/AbstractCommand.php +++ b/src/library/Command/AbstractCommand.php @@ -32,6 +32,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Throwable; use Twig\Environment as Twig_Environment; use Twig\Error\LoaderError; use Twig\Error\SyntaxError; @@ -218,7 +219,6 @@ public function __construct(string $name = null) $this->metaHandler = new BinaryWrapper($this->ffmpeg, $this->mp4v2, $fdkaac, $this->tone); - // todo: merge these two classes? $this->chapterHandler = new ChapterHandler($this->metaHandler); @@ -366,7 +366,6 @@ protected function initExecution(InputInterface $input, OutputInterface $output) } - $platformCharset = strtolower($this->input->getOption(static::OPTION_PLATFORM_CHARSET)); if ($platformCharset === "" && $this->isWindows()) { $platformCharset = AbstractMp4v2Executable::CHARSET_WIN_1252; @@ -408,13 +407,13 @@ protected function loadArguments() private function loadImprovers($getOption) { - if($getOption == null || !is_string($getOption)) { + if ($getOption == null || !is_string($getOption)) { return []; } - return array_filter(array_map(function($element) { + return array_filter(array_map(function ($element) { return strtolower(trim($element)); - }, explode(",", $getOption)), function($element) { + }, explode(",", $getOption)), function ($element) { return $element !== ""; }); @@ -527,7 +526,7 @@ protected function dumpTagAsLines(Tag $tag) * @throws LoaderError * @throws SyntaxError */ - protected function buildFileName(string $template, string $extension, array $templateParameters=[]) + protected function buildFileName(string $template, string $extension, array $templateParameters = []) { $env = new Twig_Environment(new Twig_Loader_Array([])); $template = $env->createTemplate($template); @@ -554,11 +553,33 @@ protected static function normalizeDirectory($directory, $suffix = "/") return $normalized; } - public static function replaceDirReservedChars($name) { - if(!is_string($name)) { + public static function replaceDirReservedChars($name) + { + if (!is_string($name)) { return $name; } return strtr($name, static::DIRECTORY_SPECIAL_CHAR_REPLACEMENTS); + + + } + + protected static function parseMinChapterLength(string $optMinChapterLength) + { + $parts = explode("[", $optMinChapterLength); + $optMinChapterLength = isset($parts[0]) ? (float)$parts[0] : 2; + $optKeepIndexes = null; + if (isset($parts[1])) { + try { + $keepIndexes = json_decode("[" . $parts[1]); + if (is_array($keepIndexes)) { + $optKeepIndexes = $keepIndexes; + } + } catch (Throwable $t) { + // ignore + } + } + + return [$optMinChapterLength, $optKeepIndexes]; } } diff --git a/src/library/Command/ChaptersCommand.php b/src/library/Command/ChaptersCommand.php index 0986180..f2dbf51 100644 --- a/src/library/Command/ChaptersCommand.php +++ b/src/library/Command/ChaptersCommand.php @@ -10,6 +10,7 @@ use M4bTool\Audio\Silence; use M4bTool\Audio\Tag; use M4bTool\Audio\Tag\AdjustTooLongChapters; +use M4bTool\Audio\Tag\AdjustTooShortChapters; use M4bTool\Audio\Tag\ChaptersFromEpub; use M4bTool\Audio\Tag\TagImproverComposite; use M4bTool\Chapter\ChapterHandler; @@ -325,14 +326,24 @@ function () { $this->input->getOption(static::OPTION_MAX_CHAPTER_LENGTH), new TimeUnit((int)$this->input->getOption(static::OPTION_SILENCE_MIN_LENGTH)) ); + + $optMinChapterLength = trim($this->input->getOption(static::OPTION_MIN_CHAPTER_LENGTH)); + [$optMinChapterLength, $optKeepIndexes] = static::parseMinChapterLength($optMinChapterLength); + $tooShort = new AdjustTooShortChapters(new TimeUnit($optMinChapterLength, TimeUnit::SECOND), $optKeepIndexes); + try { $tooLongAdjustment->setLogger($this); $tooLongAdjustment->improve($tag); + + $tooShort->setLogger($this); + $tooShort->improve($tag); } catch (InvalidArgumentException | Exception $e) { // ignore } + + $this->metaHandler->writeTag($this->filesToProcess, $tag); $this->notice(sprintf("wrote chapters to %s", $this->filesToProcess)); diff --git a/src/library/Command/MergeCommand.php b/src/library/Command/MergeCommand.php index 9916096..43346cc 100644 --- a/src/library/Command/MergeCommand.php +++ b/src/library/Command/MergeCommand.php @@ -905,22 +905,9 @@ private function tagMergedFile(SplFileInfo $outputTmpFile) $tagImprover->add(new Tag\AdjustTooLongChapters($this->metaHandler, $this->chapterHandler, $outputTmpFile, $maxChapterLength, $silenceLength)); break; } - $optMinChapterLength = trim($this->input->getOption(static::OPTION_MIN_CHAPTER_LENGTH)); - - $parts = explode("[", $optMinChapterLength); - $optMinChapterLength = isset($parts[0]) ? (float)$parts[0] : 2; - $optKeepIndexes = null; - if(isset($parts[1])) { - try { - $keepIndexes = json_decode("[".$parts[1]); - if (is_array($keepIndexes)) { - $optKeepIndexes = $keepIndexes; - } - } catch(Throwable $t) { - // ignore - } - } + $optMinChapterLength = trim($this->input->getOption(static::OPTION_MIN_CHAPTER_LENGTH)); + [$optMinChapterLength, $optKeepIndexes] = static::parseMinChapterLength($optMinChapterLength); $tagImprover->add(new Tag\AdjustTooShortChapters(new TimeUnit($optMinChapterLength, TimeUnit::SECOND), $optKeepIndexes));