videocutting/src/VideoInfo.php

170 lines
4.3 KiB
PHP
Raw Normal View History

2020-09-30 11:46:41 +02:00
<?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 = [];
2020-09-30 11:46:41 +02:00
public function __construct(
string $filename,
string $start,
string $end,
array $ads
2020-09-30 11:46:41 +02:00
) {
$this->filename = $filename;
$this->start = $start;
$this->end = $end;
$this->ads = $this->getParsedAds($ads);
2020-09-30 11:46:41 +02:00
}
public function getStart(): string
{
return $this->start;
}
public function getEnd(): string
{
return $this->end;
}
public function getAds(): array
2020-09-30 11:46:41 +02:00
{
return $this->ads;
2020-09-30 11:46:41 +02:00
}
public function getTitle(): string
{
$parts = $this->getFilenameParts();
// Remove also the quality indicator
return $parts[count($parts) - 2];
}
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->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);
}
2020-09-30 11:46:41 +02:00
private function getExtension(): string
{
$file = new \SplFileInfo($this->filename);
return $file->getExtension();
}
private function getFilenameParts(): array
{
return explode('_', $this->filename);
}
}