videocutting/src/VideoInfo.php
Daniel Siepmann 2d411d7160
Generate file name matching movie database
This allows to auto scrap info for tv show episodes from movie database
on kodi.
2023-02-08 08:14:36 +01:00

174 lines
4.5 KiB
PHP

<?php
namespace DanielSiepmann\Videcutting;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
class VideoInfo
{
private $filename = '';
private $start = '';
private $end = '';
private $ads = [];
public function __construct(
string $filename,
string $start,
string $end,
array $ads
) {
$this->filename = $filename;
$this->start = $start;
$this->end = $end;
$this->ads = $this->getParsedAds($ads);
}
public function getStart(): string
{
return $this->start;
}
public function getEnd(): string
{
return $this->end;
}
public function getAds(): array
{
return $this->ads;
}
public function getTitle(): string
{
$parts = $this->getFilenameParts();
// Remove also the quality indicator
return $parts[count($parts) - 2];
}
public function getTitleForHumans(): string
{
return ''
. ($this->hasSeason() ? 'S' . str_pad($this->getSeasonNumber(), 2, '0', STR_PAD_LEFT) : '')
. ($this->hasEpisode() ? 'E' . str_pad($this->getEpisodeNumber(), 2, '0', STR_PAD_LEFT) : '')
. ($this->hasSeason() || $this->hasEpisode() ? ' - ' : '')
. ucwords(str_replace('-', ' ', $this->getTitle()));
;
}
public function getTargetFilePath(): string
{
$folderParts = [];
$folderParts[] = $this->getSeries();
if ($this->hasSeason()) {
$folderParts[] = 'Staffel-' . str_pad($this->getSeasonNumber(), 2, '0', STR_PAD_LEFT);
}
return implode(DIRECTORY_SEPARATOR, $folderParts)
. DIRECTORY_SEPARATOR
. $this->getTitleForHumans()
. '.' . $this->getExtension();
}
public function getOriginalFilename(): string
{
return $this->filename;
}
public function getOriginalFilenameWithSuffix(string $suffix): string
{
$file = new \SplFileInfo($this->filename);
return sprintf(
'%s-%s.%s',
$file->getBasename('.' . $this->getExtension()),
$suffix,
$this->getExtension()
);
}
private function getParsedAds(array $ads): array
{
return array_map(function (string $ad) {
return explode('/', $ad, 2);
}, $ads);
}
private function getSeries(): string
{
return $this->getFilenameParts()[0];
}
private function hasSeason(): bool
{
foreach ($this->getFilenameParts() as $part) {
if (stripos($part, 'season-') === 0) {
return true;
}
}
return false;
}
private function getSeasonNumber(): int
{
foreach ($this->getFilenameParts() as $part) {
if (stripos($part, 'season-') === 0) {
return (int) str_replace('season-', '', $part);
}
}
throw new \Exception('No season number detected.', 1601458223);
}
private function hasEpisode(): bool
{
foreach ($this->getFilenameParts() as $part) {
if (stripos($part, 'episode-') === 0) {
return true;
}
}
return false;
}
private function getEpisodeNumber(): int
{
foreach ($this->getFilenameParts() as $part) {
if (stripos($part, 'episode-') === 0) {
return (int) str_replace('episode-', '', $part);
}
}
throw new \Exception('No episode number detected.', 1601458223);
}
private function getExtension(): string
{
$file = new \SplFileInfo($this->filename);
return $file->getExtension();
}
private function getFilenameParts(): array
{
return explode('_', $this->filename);
}
}