Skip to content

Commit

Permalink
improve #152 - added --min-chapter-length for chapters command
Browse files Browse the repository at this point in the history
  • Loading branch information
sandreas committed Nov 7, 2023
1 parent c1785d8 commit 02142f6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
37 changes: 29 additions & 8 deletions src/library/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 !== "";
});

Expand Down Expand Up @@ -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);
Expand All @@ -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];
}

}
11 changes: 11 additions & 0 deletions src/library/Command/ChaptersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
17 changes: 2 additions & 15 deletions src/library/Command/MergeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));


Expand Down

0 comments on commit 02142f6

Please sign in to comment.