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