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