Failed Conditions
Push — master ( c912ee...feeb83 )
by Sébastien
03:39
created

FFMpegAdapter::getParamsOptions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 83
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 83
ccs 28
cts 28
cp 1
rs 8.9818
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1

How to fix   Long Method   

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