|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Soluble\MediaTools\Video; |
|
6
|
|
|
|
|
7
|
|
|
use Soluble\MediaTools\Video\Adapter\FFMpegCLIValueInterface; |
|
8
|
|
|
use Soluble\MediaTools\Video\Exception\InvalidArgumentException; |
|
9
|
|
|
use Soluble\MediaTools\Video\Exception\UnsetParamException; |
|
10
|
|
|
use Soluble\MediaTools\Video\Filter\Type\VideoFilterInterface; |
|
11
|
|
|
|
|
12
|
|
|
class ThumbParams implements ThumbParamsInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var array<string, bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface> */ |
|
15
|
|
|
protected $params = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param array<string, bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface> $params |
|
19
|
|
|
* |
|
20
|
|
|
* @throws InvalidArgumentException in case of unsupported option |
|
21
|
|
|
*/ |
|
22
|
15 |
|
public function __construct($params = []) |
|
23
|
|
|
{ |
|
24
|
15 |
|
$this->ensureSupportedParams($params); |
|
25
|
15 |
|
$this->params = $params; |
|
26
|
15 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param float $time time in seconds, decimals are milli |
|
30
|
|
|
* |
|
31
|
|
|
* @return ThumbParams |
|
32
|
|
|
*/ |
|
33
|
3 |
|
public function withTime(float $time): self |
|
34
|
|
|
{ |
|
35
|
3 |
|
return new self(array_merge($this->params, [ |
|
36
|
3 |
|
self::PARAM_SEEK_TIME => new SeekTime($time), |
|
37
|
|
|
])); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
5 |
|
public function withSeekTime(SeekTime $seekTime): self |
|
41
|
|
|
{ |
|
42
|
5 |
|
return new self(array_merge($this->params, [ |
|
43
|
5 |
|
self::PARAM_SEEK_TIME => $seekTime, |
|
44
|
|
|
])); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
public function withVideoFilter(VideoFilterInterface $videoFilter): self |
|
48
|
|
|
{ |
|
49
|
2 |
|
return new self(array_merge($this->params, [ |
|
50
|
2 |
|
self::PARAM_VIDEO_FILTER => $videoFilter, |
|
51
|
|
|
])); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set the underlying encoder quality scale. (-qscale:v <int>, alias to -q:v <int>). |
|
56
|
|
|
* |
|
57
|
|
|
* @param int $qualityScale a number interpreted by the encoder, generally 1-5 |
|
58
|
|
|
*/ |
|
59
|
4 |
|
public function withQualityScale(int $qualityScale): self |
|
60
|
|
|
{ |
|
61
|
4 |
|
return new self(array_merge($this->params, [ |
|
62
|
4 |
|
self::PARAM_QUALITY_SCALE => $qualityScale, |
|
63
|
|
|
])); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Add with overwrite option (default). |
|
68
|
|
|
* |
|
69
|
|
|
* @see self::withNoOverwrite() |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function withOverwrite(): self |
|
72
|
|
|
{ |
|
73
|
1 |
|
return new self(array_merge($this->params, [ |
|
74
|
1 |
|
self::PARAM_OVERWRITE => true |
|
75
|
|
|
])); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Add protection against output file overwriting. |
|
80
|
|
|
* |
|
81
|
|
|
* @see self::witoOverwrite() |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function withNoOverwrite(): self |
|
84
|
|
|
{ |
|
85
|
1 |
|
return new self(array_merge($this->params, [ |
|
86
|
1 |
|
self::PARAM_OVERWRITE => false |
|
87
|
|
|
])); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
public function withOutputFormat(string $outputFormat): self |
|
91
|
|
|
{ |
|
92
|
1 |
|
return new self(array_merge($this->params, [ |
|
93
|
1 |
|
self::PARAM_OUTPUT_FORMAT => $outputFormat, |
|
94
|
|
|
])); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set a built-in param... |
|
99
|
|
|
* |
|
100
|
|
|
* @param bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface $paramValue |
|
101
|
|
|
* |
|
102
|
|
|
* @throws InvalidArgumentException in case of unsupported builtin param |
|
103
|
|
|
* |
|
104
|
|
|
* @return self (static analysis the trick is to return 'self' instead of interface) |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function withBuiltInParam(string $paramName, $paramValue): ThumbParamsInterface |
|
107
|
|
|
{ |
|
108
|
1 |
|
return new self(array_merge($this->params, [ |
|
109
|
1 |
|
$paramName => $paramValue, |
|
110
|
|
|
])); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return self (For static analysis the trick is to return 'self' instead of interface) |
|
115
|
|
|
*/ |
|
116
|
1 |
|
public function withoutParam(string $paramName): ThumbParamsInterface |
|
117
|
|
|
{ |
|
118
|
1 |
|
$ao = (new \ArrayObject($this->params)); |
|
119
|
1 |
|
if ($ao->offsetExists($paramName)) { |
|
120
|
1 |
|
$ao->offsetUnset($paramName); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
return new self($ao->getArrayCopy()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Return the internal array holding params. |
|
128
|
|
|
* |
|
129
|
|
|
* @return array<string,bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface> |
|
130
|
|
|
*/ |
|
131
|
3 |
|
public function toArray(): array |
|
132
|
|
|
{ |
|
133
|
3 |
|
return $this->params; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
13 |
|
public function isParamValid(string $paramName): bool |
|
137
|
|
|
{ |
|
138
|
13 |
|
return in_array($paramName, self::BUILTIN_PARAMS, true); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface|null |
|
143
|
|
|
* |
|
144
|
|
|
* @throws UnsetParamException |
|
145
|
|
|
*/ |
|
146
|
8 |
|
public function getParam(string $paramName) |
|
147
|
|
|
{ |
|
148
|
8 |
|
if (!$this->hasParam($paramName)) { |
|
149
|
1 |
|
throw new UnsetParamException(sprintf( |
|
150
|
1 |
|
'Cannot get param \'%s\', it has not been set', |
|
151
|
1 |
|
$paramName |
|
152
|
|
|
)); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
7 |
|
return $this->params[$paramName]; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
9 |
|
public function hasParam(string $paramName): bool |
|
159
|
|
|
{ |
|
160
|
9 |
|
return array_key_exists($paramName, $this->params); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Ensure that all params are supported. |
|
165
|
|
|
* |
|
166
|
|
|
* @param array<string, bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface> $params |
|
167
|
|
|
* |
|
168
|
|
|
* @throws InvalidArgumentException in case of unsupported option |
|
169
|
|
|
*/ |
|
170
|
15 |
|
protected function ensureSupportedParams(array $params): void |
|
171
|
|
|
{ |
|
172
|
15 |
|
foreach ($params as $paramName => $paramValue) { |
|
173
|
13 |
|
if (!$this->isParamValid($paramName)) { |
|
174
|
|
|
throw new InvalidArgumentException( |
|
175
|
13 |
|
sprintf('Unsupported param "%s" given.', $paramName) |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
15 |
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|