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