Passed
Push — master ( d0bcc1...2a69c8 )
by Sébastien
02:28
created

FFMpegAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 38
                '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
                        $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|UnescapedFileInterface $outputFile
190
     * @param array<string,string>               $prependArguments args that must be added at the beginning of the command
191
     *
192
     * @throws InvalidArgumentException
193
     */
194 31
    public function getCliCommand(array $arguments, string $inputFile, $outputFile = null, array $prependArguments = []): array
195
    {
196 31
        $outputArg = null;
197 31
        if ($outputFile instanceof UnescapedFileInterface) {
198 5
            $outputArg = $outputFile->getFile();
199 28
        } elseif (is_string($outputFile)) {
200 26
            $outputArg = $outputFile;
201
        }
202
203 31
        if ($outputArg === null) {
204 2
            throw new InvalidArgumentException(sprintf(
205 2
                'Output file must be either a non empty string, null or PlatformNullFile (type %s)',
206 2
                gettype($outputFile)
207
            ));
208
        }
209
210 29
        $ffmpegCmd = array_merge(
211
            [
212 29
                $this->ffmpegConfig->getBinary(),
213
            ],
214 29
            $this->getArgsWithExplodedValues($prependArguments),
215 29
            ['-i', $inputFile],
216 29
            $this->getArgsWithExplodedValues($arguments),
217 29
            [$outputArg]
218
        );
219
220 29
        if (count($ffmpegCmd) < 2) {
221
            throw new UnexpectedValueException(
222
                'Cannot generate ffmpeg cli command'
223
            );
224
        }
225
226 29
        return $ffmpegCmd;
227
    }
228
229
    /**
230
     * As we rely on symfony process unescaping, we
231
     * need to explode options name and values... i.e
232
     * ['-tune animation'] will become ['-tune', 'animation'].
233
     *
234
     * @param array<string, string> $args
235
     *
236
     * @return string[]
237
     */
238 29
    private function getArgsWithExplodedValues(array $args): array
239
    {
240 29
        $exploded = [];
241 29
        foreach ($args as $key => $value) {
242 29
            $elems      = explode(' ', $value);
243 29
            $exploded[] = (string) array_shift($elems);
244 29
            if (count($elems) <= 0) {
245 29
                continue;
246
            }
247 28
            $exploded[] = implode(' ', $elems);
248
        }
249
250 29
        return $exploded;
251
    }
252
253 24
    public function getDefaultThreads(): ?int
254
    {
255 24
        return $this->ffmpegConfig->getThreads();
256
    }
257
}
258