From 5838745b96e47f0d6a020a50f74e0b509eda7d22 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Mon, 5 Oct 2020 11:53:47 +0200 Subject: [PATCH] Add human title to metadata Most of the time meta data (from my source) has the season title as title. Instead use episode title with episode number. --- src/Cutting.php | 2 +- src/VideoInfo.php | 18 ++++++++++++------ tests/Unit/CuttingTest.php | 5 +++-- tests/Unit/VideoInfoTest.php | 27 +++++++++++++++++++++++++-- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/Cutting.php b/src/Cutting.php index 3988c09..fba687b 100644 --- a/src/Cutting.php +++ b/src/Cutting.php @@ -96,7 +96,7 @@ class Cutting $command = sprintf( 'ffmpeg -y -f concat -safe 0 -i %s -c copy' . ' -f ffmetadata -i %s -c copy -map_metadata 1' - // . ' -metadata Title="$episodeTitle"' + . ' -metadata Title=' . escapeshellarg($this->video->getTitleForHumans()) . ' %s', escapeshellarg($this->getConcatFilename()), escapeshellarg($this->getMetadataFilename()), diff --git a/src/VideoInfo.php b/src/VideoInfo.php index dcba19c..d7e9455 100644 --- a/src/VideoInfo.php +++ b/src/VideoInfo.php @@ -62,23 +62,29 @@ class VideoInfo return $parts[count($parts) - 2]; } + public function getTitleForHumans(): string + { + $title = ucwords(str_replace('-', ' ', $this->getTitle())); + + if ($this->hasEpisode()) { + $title = str_pad($this->getEpisodeNumber(), 2, '0', STR_PAD_LEFT) . ' - ' . $title; + } + + return $title; + } + public function getTargetFilePath(): string { $folderParts = []; - $filenameParts = []; $folderParts[] = $this->getSeries(); if ($this->hasSeason()) { $folderParts[] = 'Staffel-' . str_pad($this->getSeasonNumber(), 2, '0', STR_PAD_LEFT); } - if ($this->hasEpisode()) { - $filenameParts[] = str_pad($this->getEpisodeNumber(), 2, '0', STR_PAD_LEFT); - } - $filenameParts[] = ucwords(str_replace('-', ' ', $this->getTitle())); return implode(DIRECTORY_SEPARATOR, $folderParts) . DIRECTORY_SEPARATOR - . implode('-', $filenameParts) + . $this->getTitleForHumans() . '.' . $this->getExtension(); } diff --git a/tests/Unit/CuttingTest.php b/tests/Unit/CuttingTest.php index a36b0f7..e2f5342 100644 --- a/tests/Unit/CuttingTest.php +++ b/tests/Unit/CuttingTest.php @@ -222,12 +222,13 @@ class CuttingTest extends TestCase /** * @test */ - public function generatesCommandForGeneratingVideo(): void + public function generatesCommandForGeneratingVideoWithCustomTitle(): void { $video = $this->prophesize(VideoInfo::class); $video->getTargetFilePath()->willReturn('Series-Name/Series-01/10-episode title.mp4'); $video->getOriginalFilenameWithSuffix('concat')->willReturn('Series-Name_Series-01_episode-title-01-concat.mp4'); $video->getOriginalFilenameWithSuffix('metadata')->willReturn('Series-Name_Series-01_episode-title-01-metadata.mp4'); + $video->getTitleForHumans()->willReturn('01 - Episode Title'); $subject = new Cutting( $video->reveal() @@ -238,7 +239,7 @@ class CuttingTest extends TestCase static::assertSame( 'ffmpeg -y -f concat -safe 0 -i \'/tmp/Series-Name_Series-01_episode-title-01-concat.txt\' -c copy' . ' -f ffmetadata -i \'/tmp/Series-Name_Series-01_episode-title-01-metadata.txt\' -c copy -map_metadata 1' - // . ' -metadata Title="$episodeTitle"' + . ' -metadata Title=\'01 - Episode Title\'' . ' \'Series-Name/Series-01/10-episode title.mp4\'', $command->getCommandLine() ); diff --git a/tests/Unit/VideoInfoTest.php b/tests/Unit/VideoInfoTest.php index ba46123..dd1a172 100644 --- a/tests/Unit/VideoInfoTest.php +++ b/tests/Unit/VideoInfoTest.php @@ -167,6 +167,29 @@ class VideoInfoTest extends TestCase ); } + /** + * @test + * @dataProvider possibleFilenames + */ + public function returnsHumanTitle( + string $filename, + string $expectedTargetFilePath + ): void { + $subject = new VideoInfo( + $filename, + '05:20.1', + '32:30.30', + [] + ); + + $file = new \SplFileInfo($expectedTargetFilePath); + + static::assertSame( + $file->getBasename('.' . $file->getExtension()), + $subject->getTitleForHumans() + ); + } + public function possibleFilenames(): array { return [ @@ -176,11 +199,11 @@ class VideoInfoTest extends TestCase ], 'Series no seasons' => [ 'filename' => 'Storage-Hunters_episode-2_trucker-auktion_hq.mp4', - 'expectedTargetFilePath' => 'Storage-Hunters/02-Trucker Auktion.mp4', + 'expectedTargetFilePath' => 'Storage-Hunters/02 - Trucker Auktion.mp4', ], 'Series with seasons' => [ 'filename' => 'The-Big-Bang-Theory_season-1_episode-12_das-jerusalem-projekt_hq.mp4', - 'expectedTargetFilePath' => 'The-Big-Bang-Theory/Staffel-01/12-Das Jerusalem Projekt.mp4', + 'expectedTargetFilePath' => 'The-Big-Bang-Theory/Staffel-01/12 - Das Jerusalem Projekt.mp4', ], ]; }