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 VideoThumbParams implements VideoThumbParamsInterface |
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
|
16 |
|
public function __construct($params = []) |
23
|
|
|
{ |
24
|
16 |
|
$this->ensureSupportedParams($params); |
25
|
16 |
|
$this->params = $params; |
26
|
16 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param float $time time in seconds, decimals are milli |
30
|
|
|
* |
31
|
|
|
* @return VideoThumbParams |
32
|
|
|
*/ |
33
|
4 |
|
public function withTime(float $time): self |
34
|
|
|
{ |
35
|
4 |
|
return new self(array_merge($this->params, [ |
36
|
4 |
|
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): VideoThumbParamsInterface |
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): VideoThumbParamsInterface |
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
|
14 |
|
public function isParamValid(string $paramName): bool |
137
|
|
|
{ |
138
|
14 |
|
return in_array($paramName, self::BUILTIN_PARAMS, true); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Return a param, throw an exception if the param has not been defined yet or |
143
|
|
|
* use $default if it was set. |
144
|
|
|
* |
145
|
|
|
* @param mixed $default Will return default value instead of throwing exception |
146
|
|
|
* |
147
|
|
|
* @return bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface|null |
148
|
|
|
* |
149
|
|
|
* @throws UnsetParamException |
150
|
|
|
*/ |
151
|
9 |
|
public function getParam(string $paramName, $default = null) |
152
|
|
|
{ |
153
|
9 |
|
if (!$this->hasParam($paramName)) { |
154
|
2 |
|
if ($default !== null) { |
155
|
1 |
|
return $default; |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
throw new UnsetParamException(sprintf( |
159
|
1 |
|
'Cannot get param \'%s\', it has not been set', |
160
|
1 |
|
$paramName |
161
|
|
|
)); |
162
|
|
|
} |
163
|
|
|
|
164
|
7 |
|
return $this->params[$paramName]; |
165
|
|
|
} |
166
|
|
|
|
167
|
10 |
|
public function hasParam(string $paramName): bool |
168
|
|
|
{ |
169
|
10 |
|
return array_key_exists($paramName, $this->params); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Ensure that all params are supported. |
174
|
|
|
* |
175
|
|
|
* @param array<string, bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface> $params |
176
|
|
|
* |
177
|
|
|
* @throws InvalidArgumentException in case of unsupported option |
178
|
|
|
*/ |
179
|
16 |
|
protected function ensureSupportedParams(array $params): void |
180
|
|
|
{ |
181
|
16 |
|
foreach ($params as $paramName => $paramValue) { |
182
|
14 |
|
if (!$this->isParamValid($paramName)) { |
183
|
|
|
throw new InvalidArgumentException( |
184
|
14 |
|
sprintf('Unsupported param "%s" given.', $paramName) |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
} |
188
|
16 |
|
} |
189
|
|
|
} |
190
|
|
|
|