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