ScaleFilter   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
eloc 57
c 2
b 0
f 0
dl 0
loc 134
ccs 38
cts 38
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C getFFmpegCLIValue() 0 21 14
A __construct() 0 37 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @see       https://github.com/soluble-io/soluble-mediatools for the canonical repository
7
 *
8
 * @copyright Copyright (c) 2018-2020 Sébastien Vanvelthem. (https://github.com/belgattitude)
9
 * @license   https://github.com/soluble-io/soluble-mediatools/blob/master/LICENSE.md MIT
10
 */
11
12
namespace Soluble\MediaTools\Video\Filter;
13
14
use Soluble\MediaTools\Video\Exception\ParamValidationException;
15
use Soluble\MediaTools\Video\Filter\Type\FFMpegVideoFilterInterface;
16
17
final class ScaleFilter implements FFMpegVideoFilterInterface
18
{
19
    public const OPTION_ASPECT_RATIO_INCREASE = 'increase';
20
    public const OPTION_ASPECT_RATIO_DECREASE = 'decrease';
21
    public const OPTION_ASPECT_RATIO_DISABLE  = 'disable';
22
23
    /**
24
     * Built-in aspect ratios.
25
     */
26
    public const ASPECT_RATIO_MODES = [
27
        self::OPTION_ASPECT_RATIO_INCREASE,
28
        self::OPTION_ASPECT_RATIO_DECREASE,
29
        self::OPTION_ASPECT_RATIO_DISABLE,
30
    ];
31
32
    /** @var int|string|null */
33
    private $height;
34
35
    /** @var int|string|null */
36
    private $width;
37
38
    /** @var string|null */
39
    private $forceOriginalAspectRatio;
40
41
    /** @var string|null */
42
    private $eval;
43
44
    /** @var int|null */
45
    private $interl;
46
47
    /** @var string|null */
48
    private $flags;
49
50
    /** @var float|null */
51
    private $param0;
52
53
    /** @var float|null */
54
    private $param1;
55
56
    /** @var string|null */
57
    private $size;
58
59
    /** @var string|null */
60
    private $inColorMatrix;
61
62
    /** @var string|null */
63
    private $outColorMatrix;
64
65
    /** @var string|null */
66
    private $inRange;
67
68
    /** @var string|null */
69
    private $outRange;
70
71
    /**
72
     * Scale filter.
73
     *
74
     * @see https://trac.ffmpeg.org/wiki/Scaling
75
     * @see https://ffmpeg.org/ffmpeg-filters.html#scale-1
76
     *
77
     * @param string|int|null $width                    Set the output video width expression. Default value is the input width.
78
     * @param string|int|null $height                   Set the output video height expression. Default value is the input height.
79
     * @param string|null     $forceOriginalAspectRatio enable decreasing or increasing output video width or height if necessary to keep the original aspect ratio
80
     * @param string|null     $eval                     Specify when to evaluate width and height expression: 'init' or 'frame'
81
     * @param int|null        $interl                   Set the interlacing mode. It accepts the following values: ‘1’ Force interlaced aware scaling. ‘0’ Do not apply interlaced scaling. ‘-1’ Select interlaced aware scaling depending on whether the source frames are flagged as interlaced or not.
82
     * @param string|null     $flags                    Set libswscale scaling flags
83
     * @param float|null      $param0                   Set libswscale input parameters for scaling algorithms that need them
84
     * @param float|null      $param1                   Set libswscale input parameters for scaling algorithms that need them
85
     * @param string|null     $size                     set the video size
86
     * @param string|null     $inColorMatrix            set input YCbCr color space type
87
     * @param string|null     $outColorMatrix           set output YCbCr color space type
88
     * @param string|null     $inRange                  set intput YCbCr sample range
89
     * @param string|null     $outRange                 set output YCbCr sample range
90
     */
91 5
    public function __construct(
92
        $width = null,
93
        $height = null,
94
        ?string $forceOriginalAspectRatio = null,
95
        ?string $eval = null,
96
        ?int $interl = null,
97
        ?string $flags = null,
98
        ?float $param0 = null,
99
        ?float $param1 = null,
100
        ?string $size = null,
101
        ?string $inColorMatrix = null,
102
        ?string $outColorMatrix = null,
103
        ?string $inRange = null,
104
        ?string $outRange = null
105
    ) {
106 5
        if ($forceOriginalAspectRatio !== null &&
107 5
           !in_array($forceOriginalAspectRatio, self::ASPECT_RATIO_MODES, true)) {
108 1
            throw new ParamValidationException(sprintf(
109 1
                'Unsupported forceOriginalAspectRatio param: \'%s\'. Must be %s.',
110
                $forceOriginalAspectRatio,
111 1
                implode(' | ', self::ASPECT_RATIO_MODES)
112
            ));
113
        }
114 4
        $this->forceOriginalAspectRatio = $forceOriginalAspectRatio;
115
116 4
        $this->width          = $width;
117 4
        $this->height         = $height;
118 4
        $this->eval           = $eval;
119 4
        $this->interl         = $interl;
120 4
        $this->flags          = $flags;
121 4
        $this->param0         = $param0;
122 4
        $this->param1         = $param1;
123 4
        $this->size           = $size;
124 4
        $this->inColorMatrix  = $inColorMatrix;
125 4
        $this->outColorMatrix = $outColorMatrix;
126 4
        $this->inRange        = $inRange;
127 4
        $this->outRange       = $outRange;
128 4
    }
129
130 4
    public function getFFmpegCLIValue(): string
131
    {
132 4
        $args = array_filter([
133 4
            ($this->width !== null) ? "w={$this->width}" : false,
134 4
            ($this->height !== null) ? "h={$this->height}" : false,
135 4
            ($this->forceOriginalAspectRatio !== null) ? "force_original_aspect_ratio={$this->forceOriginalAspectRatio}" : false,
136 4
            ($this->eval !== null) ? "eval={$this->eval}" : false,
137 4
            ($this->interl !== null) ? "interl={$this->interl}" : false,
138 4
            ($this->flags !== null) ? "flags={$this->flags}" : false,
139 4
            ($this->param0 !== null) ? "param0={$this->param0}" : false,
140 4
            ($this->param1 !== null) ? "param1={$this->param1}" : false,
141 4
            ($this->size !== null) ? "size={$this->size}" : false,
142 4
            ($this->inColorMatrix !== null) ? "in_color_matrix={$this->inColorMatrix}" : false,
143 4
            ($this->outColorMatrix !== null) ? "out_color_matrix={$this->outColorMatrix}" : false,
144 4
            ($this->inRange !== null) ? "in_range={$this->inRange}" : false,
145 4
            ($this->outRange !== null) ? "out_range={$this->outRange}" : false,
146
        ]);
147
148 4
        return sprintf(
149 4
            'scale=%s',
150 4
            implode(':', $args)
151
        );
152
    }
153
}
154