Passed
Push — master ( dcf1a9...e98bf8 )
by Sébastien
02:49
created

FFMpegAdapter::getMappedConversionParams()   C

Complexity

Conditions 12
Paths 28

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 12.3091

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 51
ccs 27
cts 31
cp 0.871
rs 6.9666
c 0
b 0
f 0
cc 12
nc 28
nop 2
crap 12.3091

How to fix   Long Method    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\Adapter;
6
7
use Soluble\MediaTools\Common\Exception\InvalidArgumentException;
8
use Soluble\MediaTools\Common\Exception\UnsupportedParamException;
9
use Soluble\MediaTools\Common\Exception\UnsupportedParamValueException;
10
use Soluble\MediaTools\Common\IO\UnescapedFileInterface;
11
use Soluble\MediaTools\Video\Adapter\Validator\FFMpegParamValidator;
12
use Soluble\MediaTools\Video\Config\FFMpegConfigInterface;
13
use Soluble\MediaTools\Video\Exception\ParamValidationException;
14
use Soluble\MediaTools\Video\Exception\UnexpectedValueException;
15
use Soluble\MediaTools\Video\VideoConvertParamsInterface;
16
17
class FFMpegAdapter implements ConverterAdapterInterface
18
{
19
    /** @var FFMpegConfigInterface */
20
    protected $ffmpegConfig;
21
22 39
    public function __construct(FFMpegConfigInterface $ffmpegConfig)
23
    {
24 39
        $this->ffmpegConfig = $ffmpegConfig;
25 39
    }
26
27
    /**
28
     * @return array<string, array<string, string>>
29
     */
30 38
    public function getParamsOptions(): array
31
    {
32
        return [
33 38
            VideoConvertParamsInterface::PARAM_OUTPUT_FORMAT => [
34
                'pattern' => '-f %s',
35
            ],
36 38
            VideoConvertParamsInterface::PARAM_VIDEO_CODEC => [
37
                'pattern' => '-c:v %s',
38
            ],
39 38
            VideoConvertParamsInterface::PARAM_VIDEO_BITRATE => [
40
                'pattern' => '-b:v %s',
41
            ],
42 38
            VideoConvertParamsInterface::PARAM_VIDEO_MIN_BITRATE => [
43
                'pattern' => '-minrate %s',
44
            ],
45 38
            VideoConvertParamsInterface::PARAM_VIDEO_MAX_BITRATE => [
46
                'pattern' => '-maxrate %s',
47
            ],
48 38
            VideoConvertParamsInterface::PARAM_AUDIO_CODEC => [
49
                'pattern' => '-c:a %s',
50
            ],
51 38
            VideoConvertParamsInterface::PARAM_AUDIO_BITRATE => [
52
                'pattern' => '-b:a %s',
53
            ],
54 38
            VideoConvertParamsInterface::PARAM_PIX_FMT => [
55
                'pattern' => '-pix_fmt %s',
56
            ],
57 38
            VideoConvertParamsInterface::PARAM_PRESET => [
58
                'pattern' => '-preset %s',
59
            ],
60 38
            VideoConvertParamsInterface::PARAM_SPEED => [
61
                'pattern' => '-speed %d',
62
            ],
63 38
            VideoConvertParamsInterface::PARAM_THREADS => [
64
                'pattern' => '-threads %d',
65
            ],
66 38
            VideoConvertParamsInterface::PARAM_KEYFRAME_SPACING => [
67
                'pattern' => '-g %d',
68
            ],
69 38
            VideoConvertParamsInterface::PARAM_QUALITY => [
70
                'pattern' => '-quality %s',
71
            ],
72 38
            VideoConvertParamsInterface::PARAM_VIDEO_QUALITY_SCALE => [
73
                'pattern' => '-qscale:v %d',
74
            ],
75 38
            VideoConvertParamsInterface::PARAM_CRF => [
76
                'pattern' => '-crf %d',
77
            ],
78 38
            VideoConvertParamsInterface::PARAM_STREAMABLE => [
79
                'pattern' => '-movflags +faststart',
80
            ],
81 38
            VideoConvertParamsInterface::PARAM_FRAME_PARALLEL => [
82
                'pattern' => '-frame-parallel %s',
83
            ],
84 38
            VideoConvertParamsInterface::PARAM_TILE_COLUMNS => [
85
                'pattern' => '-tile-columns %s',
86
            ],
87 38
            VideoConvertParamsInterface::PARAM_TUNE => [
88
                'pattern' => '-tune %s',
89
            ],
90 38
            VideoConvertParamsInterface::PARAM_VIDEO_FILTER => [
91
                'pattern' => '-filter:v %s',
92
            ],
93 38
            VideoConvertParamsInterface::PARAM_OVERWRITE => [
94
                'pattern' => '-y',
95
            ],
96 38
            VideoConvertParamsInterface::PARAM_VIDEO_FRAMES => [
97
                'pattern' => '-frames:v %d',
98
            ],
99 38
            VideoConvertParamsInterface::PARAM_NOAUDIO => [
100
                'pattern' => '-an',
101
            ],
102 38
            VideoConvertParamsInterface::PARAM_SEEK_START => [
103
                'pattern' => '-ss %s',
104
            ],
105 38
            VideoConvertParamsInterface::PARAM_SEEK_END => [
106
                'pattern' => '-to %s',
107
            ],
108 38
            VideoConvertParamsInterface::PARAM_PASSLOGFILE => [
109
                'pattern' => '-passlogfile %s',
110
            ],
111 38
            VideoConvertParamsInterface::PARAM_PASS => [
112
                'pattern' => '-pass %s',
113
            ],
114 38
            VideoConvertParamsInterface::PARAM_AUTO_ALT_REF => [
115
                'pattern' => '-auto-alt-ref %s',
116
            ],
117 38
            VideoConvertParamsInterface::PARAM_LAG_IN_FRAMES => [
118
                'pattern' => '-lag-in-frames %s',
119
            ],
120
        ];
121
    }
122
123
    /**
124
     * Return an array version of params suitable for ffmpeg cli.
125
     *
126
     * @param bool $validateParams whether to run ffmpeg validation process validation
127
     *
128
     * @return array<string, string>
129
     *
130
     * @throws UnsupportedParamException
131
     * @throws UnsupportedParamValueException
132
     * @throws ParamValidationException
133
     */
134 38
    public function getMappedConversionParams(VideoConvertParamsInterface $conversionParams, bool $validateParams = true): array
135
    {
136 38
        $args             = [];
137 38
        $supportedOptions = $this->getParamsOptions();
138
139
        // Add default overwrite option if not set
140 38
        $overwriteParam = VideoConvertParamsInterface::PARAM_OVERWRITE;
141 38
        if (!$conversionParams->hasParam($overwriteParam)) {
142 34
            $conversionParams = $conversionParams->withBuiltInParam(
143 34
                $overwriteParam,
144 34
                true
145
            );
146
        }
147
148 38
        foreach ($conversionParams->toArray() as $paramName => $value) {
149 38
            if (!array_key_exists($paramName, $supportedOptions)) {
150
                throw new UnsupportedParamException(
151
                    sprintf(
152
                        'FFMpegAdapter does not support param \'%s\'',
153
                        $paramName
154
                    )
155
                );
156
            }
157 38
            $pattern = $supportedOptions[$paramName]['pattern'];
158 38
            if (is_bool($value)) {
159 37
                $args[$paramName] = $value ? $pattern : '';
160 35
            } elseif ($value instanceof FFMpegCLIValueInterface) {
161
                // Will test also FFMpegVideoFilterInterface
162 23
                $cliValue = $value->getFFmpegCLIValue();
163 23
                if ($cliValue !== null) {
164 23
                    $args[$paramName] = sprintf($pattern, $cliValue);
165
                }
166 32
            } elseif (is_string($value) || is_int($value)) {
167 31
                $args[$paramName] = sprintf($pattern, $value);
168
            } else {
169 1
                throw new UnsupportedParamValueException(
170 1
                    sprintf(
171 1
                        'Param \'%s\' has an unsupported type: \'%s\'',
172 1
                        $paramName,
173 38
                        is_object($value) ? get_class($value) : gettype($value)
174
                    )
175
                );
176
            }
177
        }
178
179
        // Validation
180 37
        if ($validateParams) {
181 37
            (new FFMpegParamValidator($conversionParams))->validate();
182
        }
183
184 36
        return $args;
185
    }
186
187
    /**
188
     * @param array<string,string>               $arguments        args that will be added
189
     * @param null|string                        $inputFile
190
     * @param null|string|UnescapedFileInterface $outputFile
191
     * @param array<string,string>               $prependArguments args that must be added at the beginning of the command
192
     *
193
     * @throws InvalidArgumentException
194
     */
195 31
    public function getCliCommand(array $arguments, ?string $inputFile, $outputFile = null, array $prependArguments = []): string
196
    {
197 31
        $inputArg = ($inputFile !== null && $inputFile !== '')
198 31
                        ? sprintf('-i %s', escapeshellarg($inputFile))
199 31
                        : '';
200
201 31
        $outputArg = '';
202 31
        if ($outputFile instanceof UnescapedFileInterface) {
203 5
            $outputArg = $outputFile->getFile();
204 28
        } elseif (is_string($outputFile)) {
205 26
            $outputArg = sprintf('%s', escapeshellarg($outputFile));
206 2
        } elseif ($outputFile !== null) {
0 ignored issues
show
introduced by
The condition $outputFile !== null is always false.
Loading history...
207 2
            throw new InvalidArgumentException(sprintf(
208 2
                'Output file must be either a non empty string, null or PlatformNullFile (type %s)',
209 2
                gettype($outputFile)
210
            ));
211
        }
212
213 29
        $ffmpegCmd = preg_replace(
214 29
            '/(\ ){2,}/',
215 29
            ' ',
216 29
            trim(sprintf(
217 29
                '%s %s %s %s %s',
218 29
                $this->ffmpegConfig->getBinary(),
219 29
                implode(' ', $prependArguments),
220 29
                $inputArg,
221 29
                implode(' ', $arguments),
222 29
                $outputArg
223
            ))
224
        );
225
226 29
        if ($ffmpegCmd === null) {
227
            throw new UnexpectedValueException(
228
                'Cannot generate ffmpeg cli command'
229
            );
230
        }
231
232 29
        return $ffmpegCmd;
233
    }
234
235 24
    public function getDefaultThreads(): ?int
236
    {
237 24
        return $this->ffmpegConfig->getThreads();
238
    }
239
}
240