LegacyFilterMapping   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A apply() 0 18 1
A singleMediaCollection() 0 5 2
A normalizeIn() 0 3 1
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\FFMpeg;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Collection;
7
use ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg;
8
use ProtoneMedia\LaravelFFMpeg\Exporters\HLSVideoFilters;
9
use ProtoneMedia\LaravelFFMpeg\Filesystem\MediaCollection;
10
11
class LegacyFilterMapping
12
{
13
    private $in;
14
    private $out;
15
16
    /**
17
     * @var array
18
     */
19
    private $arguments;
20
21
    public function __construct($in, $out, ...$arguments)
22
    {
23
        $this->in        = $in;
24
        $this->out       = $out;
25
        $this->arguments = $arguments;
26
    }
27
28
    /**
29
     * Removes all non-numeric characters from the 'in' attribute.
30
     */
31
    public function normalizeIn(): int
32
    {
33
        return preg_replace("/[^0-9]/", "", HLSVideoFilters::beforeGlue($this->in));
34
    }
35
36
    /**
37
     * Filters the given MediaCollection down to one item by
38
     * guessing the key by the 'in' attribute.
39
     */
40
    private function singleMediaCollection(MediaCollection $mediaCollection): MediaCollection
41
    {
42
        $media = $mediaCollection->get($this->normalizeIn()) ?: $mediaCollection->first();
0 ignored issues
show
Bug introduced by
The method get() does not exist on ProtoneMedia\LaravelFFMp...esystem\MediaCollection. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        $media = $mediaCollection->/** @scrutinizer ignore-call */ get($this->normalizeIn()) ?: $mediaCollection->first();
Loading history...
Bug introduced by
The method first() does not exist on ProtoneMedia\LaravelFFMp...esystem\MediaCollection. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        $media = $mediaCollection->get($this->normalizeIn()) ?: $mediaCollection->/** @scrutinizer ignore-call */ first();
Loading history...
43
44
        return MediaCollection::make([$media]);
45
    }
46
47
    /**
48
     * Applies the filter to the FFMpeg driver.
49
     */
50
    public function apply(PHPFFMpeg $driver, Collection $maps): void
51
    {
52
        $freshDriver = $driver->fresh()
53
            ->open($this->singleMediaCollection($driver->getMediaCollection()))
54
            ->addFilter(...$this->arguments);
55
56
        $format = $maps->filter->hasOut($this->out)->first()->getFormat();
57
58
        Collection::make($freshDriver->getFilters())->map(function ($filter) use ($freshDriver, $format) {
59
            $parameters = $filter->apply($freshDriver->get(), $format);
60
61
            $parameters = Arr::where($parameters, function ($parameter) {
62
                return !in_array($parameter, ['-vf', '-filter:v', '-filter_complex']);
63
            });
64
65
            return implode(' ', $parameters);
66
        })->each(function ($customCompiledFilter) use ($driver) {
67
            $driver->addFilter($this->in, $customCompiledFilter, $this->out);
68
        });
69
    }
70
}
71