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