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\Adapter\Validator; |
13
|
|
|
|
14
|
|
|
use Soluble\MediaTools\Video\Exception\ParamValidationException; |
15
|
|
|
use Soluble\MediaTools\Video\VideoConvertParamsInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* To get better error reporting, we try to fail early (instead of at the shell exec level) |
19
|
|
|
* Here's a starter class to add some common validation, use it as a base, will need |
20
|
|
|
* refactor when more rules are added. |
21
|
|
|
*/ |
22
|
|
|
final class FFMpegParamValidator |
23
|
|
|
{ |
24
|
|
|
/** @var VideoConvertParamsInterface */ |
25
|
|
|
private $params; |
26
|
|
|
|
27
|
42 |
|
public function __construct(VideoConvertParamsInterface $conversionParams) |
28
|
|
|
{ |
29
|
42 |
|
$this->params = $conversionParams; |
30
|
42 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @throws ParamValidationException |
34
|
|
|
*/ |
35
|
42 |
|
public function validate(): void |
36
|
|
|
{ |
37
|
42 |
|
$this->ensureValidCrf(); |
38
|
41 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Ensure that is CRF have been set, values for VP9 and H264 are in valid ranges. |
42
|
|
|
* |
43
|
|
|
* @throws ParamValidationException |
44
|
|
|
*/ |
45
|
42 |
|
private function ensureValidCrf(): void |
46
|
|
|
{ |
47
|
42 |
|
if (!$this->params->hasParam(VideoConvertParamsInterface::PARAM_CRF)) { |
48
|
35 |
|
return; |
49
|
|
|
} |
50
|
7 |
|
$crf = $this->params->getParam(VideoConvertParamsInterface::PARAM_CRF); |
51
|
|
|
|
52
|
|
|
// Check allowed values for CRF |
53
|
7 |
|
$codec = $this->params->getParam(VideoConvertParamsInterface::PARAM_VIDEO_CODEC, ''); |
54
|
|
|
|
55
|
7 |
|
if (mb_stripos($codec, 'vp9') !== false && ($crf < 0 || $crf > 63)) { |
56
|
1 |
|
throw new ParamValidationException( |
57
|
1 |
|
sprintf( |
58
|
1 |
|
'Invalid value for CRF, \'%s\' requires a number between 0 and 63: %s given.', |
59
|
|
|
$codec, |
60
|
|
|
$crf |
61
|
|
|
) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
7 |
|
if (mb_stripos($codec, '264') !== false && ($crf < 0 || $crf > 51)) { |
66
|
1 |
|
throw new ParamValidationException( |
67
|
1 |
|
sprintf( |
68
|
1 |
|
'Invalid value for CRF, \'%s\' requires a number between 0 and 61: %s given.', |
69
|
|
|
$codec, |
70
|
|
|
$crf |
71
|
|
|
) |
72
|
|
|
); |
73
|
|
|
} |
74
|
6 |
|
} |
75
|
|
|
} |
76
|
|
|
|