Passed
Push — master ( f74a67...fcec7a )
by Sébastien
05:07 queued 03:02
created

FFMpegAdapter::getMappedConversionParams()   B

Complexity

Conditions 10
Paths 14

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 10.3248

Importance

Changes 0
Metric Value
cc 10
eloc 27
nc 14
nop 1
dl 0
loc 43
ccs 23
cts 27
cp 0.8519
crap 10.3248
rs 7.6666
c 0
b 0
f 0

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