Completed
Push — master ( aea97d...ec8061 )
by Sébastien
04:37
created

FFMpegAdapter::getParamsOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 84
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 1

Importance

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