vid-queue/app/Data/DataPipes/GetSeasonEp.php

63 lines
1.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Data\DataPipes;
use FFMpeg\FFMpeg;
use Illuminate\Support\Collection;
use SimpleXMLElement;
use Spatie\LaravelData\DataPipes\DataPipe;
use Spatie\LaravelData\Support\DataClass;
use function file_exists;
use function pathinfo;
use function simplexml_load_file;
class GetSeasonEp implements DataPipe
{
public function handle(mixed $payload, DataClass $class, Collection $properties): Collection
{
$path = $properties->get('path');
$pi = pathinfo($path);
$nfo = "{$pi['dirname']}/{$pi['filename']}.nfo";
if (file_exists($nfo)) {
$properties['epNfo'] = $nfo;
return $this->getEpFromNfo($properties, $nfo);
}
return $this->getEpFromFfprobe($properties);
}
protected function getEpFromNfo(Collection $properties, string $nfo): Collection
{
$xml = simplexml_load_file($nfo);
if ($xml->season) {
$properties['season'] = (int) $xml->season;
}
if ($xml->episode) {
$properties['episode'] = (int) $xml->episode;
}
return $properties;
}
protected function getEpFromFfprobe(Collection $properties): Collection
{
$ffmpeg = FFMpeg::create();
$ffprobe = $ffmpeg->getFFProbe();
$tags = $ffprobe->format($properties->get('path'))->get('tags');
if ($season = $tags['season_number'] ?? $tags['SEASON_NUMBER'] ?? null) {
$properties['season'] = (int) $season;
}
if ($episode = $tags['episode_sort'] ?? $tags['EPISODE_SORT'] ?? null) {
$properties['episode'] = (int) $episode;
}
return $properties;
}
}