Completed
Push — master ( 3e00d5...ea5e9f )
by Sébastien
02:23
created

FFMpegAdapter::getMappedConversionParams()   B

Complexity

Conditions 11
Paths 16

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 11.6199

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 44
ccs 24
cts 29
cp 0.8276
rs 7.3166
c 0
b 0
f 0
cc 11
nc 16
nop 1
crap 11.6199

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video\Converter;
6
7
use Soluble\MediaTools\Config\FFMpegConfig;
8
use Soluble\MediaTools\Exception\InvalidArgumentException;
9
use Soluble\MediaTools\Exception\UnsupportedParamException;
10
use Soluble\MediaTools\Exception\UnsupportedParamValueException;
11
use Soluble\MediaTools\Util\PlatformNullFile;
12
use Soluble\MediaTools\Video\ConversionParamsInterface;
13
use Soluble\MediaTools\Video\Filter\Type\FFMpegVideoFilterInterface;
14
15
class FFMpegAdapter implements AdapterInterface
16
{
17
    /** @var FFMpegConfig */
18
    protected $ffmpegConfig;
19
20 14
    public function __construct(FFMpegConfig $ffmpegConfig)
21
    {
22 14
        $this->ffmpegConfig = $ffmpegConfig;
23 14
    }
24
25
    /**
26
     * @return array<string, array<string, string>>
27
     */
28 11
    public function getParamsOptions(): array
29
    {
30
        return [
31 11
            ConversionParamsInterface::PARAM_OUTPUT_FORMAT => [
32
                'pattern' => '-f %s',
33
            ],
34
35 11
            ConversionParamsInterface::PARAM_VIDEO_CODEC => [
36
                'pattern' => '-c:v %s',
37
            ],
38 11
            ConversionParamsInterface::PARAM_VIDEO_BITRATE => [
39
                'pattern' => '-b:v %s',
40
            ],
41 11
            ConversionParamsInterface::PARAM_VIDEO_MIN_BITRATE => [
42
                'pattern' => '-minrate %s',
43
            ],
44 11
            ConversionParamsInterface::PARAM_VIDEO_MAX_BITRATE => [
45
                'pattern' => '-maxrate %s',
46
            ],
47
48 11
            ConversionParamsInterface::PARAM_AUDIO_CODEC => [
49
                'pattern' => '-c:a %s',
50
            ],
51 11
            ConversionParamsInterface::PARAM_AUDIO_BITRATE => [
52
                'pattern' => '-b:a %s',
53
            ],
54 11
            ConversionParamsInterface::PARAM_PIX_FMT => [
55
                'pattern' => '-pix_fmt %s',
56
            ],
57 11
            ConversionParamsInterface::PARAM_PRESET => [
58
                'pattern' => '-preset %s',
59
            ],
60 11
            ConversionParamsInterface::PARAM_SPEED => [
61
                'pattern' => '-speed %d',
62
            ],
63 11
            ConversionParamsInterface::PARAM_THREADS => [
64
                'pattern' => '-threads %d',
65
            ],
66 11
            ConversionParamsInterface::PARAM_KEYFRAME_SPACING => [
67
                'pattern' => '-g %d',
68
            ],
69 11
            ConversionParamsInterface::PARAM_QUALITY => [
70
                'pattern' => '-quality %s',
71
            ],
72
73 11
            ConversionParamsInterface::PARAM_VIDEO_QUALITY_SCALE => [
74
                'pattern' => '-qscale:v %d',
75
            ],
76
77 11
            ConversionParamsInterface::PARAM_CRF => [
78
                'pattern' => '-crf %d',
79
            ],
80 11
            ConversionParamsInterface::PARAM_STREAMABLE => [
81
                'pattern' => '-movflags +faststart',
82
            ],
83
84 11
            ConversionParamsInterface::PARAM_FRAME_PARALLEL => [
85
                'pattern' => '-frame-parallel %s',
86
            ],
87 11
            ConversionParamsInterface::PARAM_TILE_COLUMNS => [
88
                'pattern' => '-tile-columns %s',
89
            ],
90 11
            ConversionParamsInterface::PARAM_TUNE => [
91
                'pattern' => '-tune %s',
92
            ],
93 11
            ConversionParamsInterface::PARAM_VIDEO_FILTER => [
94
                'pattern' => '-vf %s',
95
            ],
96 11
            ConversionParamsInterface::PARAM_OVERWRITE => [
97
                'pattern' => '-y',
98
            ],
99 11
            ConversionParamsInterface::PARAM_VIDEO_FRAMES => [
100
                'pattern' => '-frames:v %d',
101
            ],
102 11
            ConversionParamsInterface::PARAM_NOAUDIO => [
103
                'pattern' => '-an',
104
            ],
105
106 11
            ConversionParamsInterface::PARAM_SEEK_START => [
107
                'pattern' => '-ss %s',
108
            ],
109
110 11
            ConversionParamsInterface::PARAM_SEEK_END => [
111
                'pattern' => '-to %s',
112
            ],
113
        ];
114
    }
115
116
    /**
117
     * @return array<string, string>
118
     *
119
     * @throws UnsupportedParamException
120
     * @throws UnsupportedParamValueException
121
     */
122 11
    public function getMappedConversionParams(ConversionParamsInterface $conversionParams): array
123
    {
124 11
        $args             = [];
125 11
        $supportedOptions = $this->getParamsOptions();
126
127
        // Add default overwrite option if not set
128 11
        $overwriteParam = ConversionParamsInterface::PARAM_OVERWRITE;
129 11
        if (!$conversionParams->hasParam($overwriteParam)) {
130 8
            $conversionParams = $conversionParams->withBuiltInParam(
131 8
                $overwriteParam,
132 8
                true
133
            );
134
        }
135
136 11
        foreach ($conversionParams->toArray() as $paramName => $value) {
137 11
            if (!array_key_exists($paramName, $supportedOptions)) {
138
                throw new UnsupportedParamException(
139
                    sprintf(
140
                        'FFMpegAdapter does not support param \'%s\'',
141
                        $paramName
142
                    )
143
                );
144
            }
145 11
            $pattern = $supportedOptions[$paramName]['pattern'];
146 11
            if (is_bool($value)) {
147 10
                $args[$paramName] = $value ? $pattern : '';
148 10
            } elseif ($value instanceof FFMpegCLIValueInterface) {
149 7
                $args[$paramName] = sprintf($pattern, $value->getFFmpegCLIValue());
150 8
            } elseif ($value instanceof FFMpegVideoFilterInterface) {
151
                $args[$paramName] = sprintf($pattern, $value->getFFmpegCLIValue());
152 8
            } elseif (is_string($value) || is_int($value)) {
153 7
                $args[$paramName] = sprintf($pattern, $value);
154
            } else {
155 1
                throw new UnsupportedParamValueException(
156 1
                    sprintf(
157 1
                        'Param \'%s\' has an unsupported type: \'%s\'',
158 1
                        $paramName,
159 11
                        is_object($value) ? get_class($value) : gettype($value)
160
                    )
161
                );
162
            }
163
        }
164
165 10
        return $args;
166
    }
167
168
    /**
169
     * @param array<int|string, string>    $arguments
170
     * @param string|null                  $inputFile  if <null> will not prepend '-i inputFile' in args
171
     * @param null|string|PlatformNullFile $outputFile
172
     *
173
     * @throws InvalidArgumentException
174
     */
175 6
    public function getCliCommand(array $arguments, ?string $inputFile, $outputFile = null): string
176
    {
177 6
        $inputArg = ($inputFile !== null && $inputFile !== '')
178 6
                        ? sprintf('-i %s', escapeshellarg($inputFile))
179 6
                        : '';
180
181 6
        $outputArg = '';
182 6
        if ($outputFile instanceof PlatformNullFile) {
183 1
            $outputArg = $outputFile->getNullFile();
184 5
        } elseif (is_string($outputFile)) {
185 5
            $outputArg = sprintf('%s', escapeshellarg($outputFile));
186
        } elseif ($outputFile !== null) {
0 ignored issues
show
introduced by
The condition $outputFile !== null is always false.
Loading history...
187
            throw new InvalidArgumentException(sprintf(
188
                'Output file must be either a non empty string, null or PlatformNullFile (type %s)',
189
                gettype($outputFile)
190
            ));
191
        }
192
193 6
        $ffmpegCmd = trim(sprintf(
194 6
            '%s %s %s %s',
195 6
            $this->ffmpegConfig->getBinary(),
196 6
            $inputArg,
197 6
            implode(' ', $arguments),
198 6
            $outputArg
199
        ));
200
201 6
        return $ffmpegCmd;
202
    }
203
}
204