Passed
Push — master ( 220eca...ef308c )
by Sébastien
03:02
created

ThumbService::makeThumbnail()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 9.8333
c 0
b 0
f 0
cc 3
nc 2
nop 4
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video;
6
7
use Soluble\MediaTools\Config\FFMpegConfig;
8
use Soluble\MediaTools\Exception\FileNotFoundException;
9
use Soluble\MediaTools\Util\Assert\PathAssertionsTrait;
10
use Soluble\MediaTools\Video\Filter\EmptyVideoFilter;
11
use Soluble\MediaTools\Video\Filter\VideoFilterInterface;
12
13
class ThumbService implements ThumbServiceInterface
14
{
15
    use PathAssertionsTrait;
16
17
    /** @var FFMpegConfig */
18
    protected $ffmpegConfig;
19
20 2
    public function __construct(FFMpegConfig $ffmpegConfig)
21
    {
22 2
        $this->ffmpegConfig = $ffmpegConfig;
23 2
    }
24
25
    /**
26
     * @param null|VideoFilterInterface $videoFilter
27
     *
28
     * @throws FileNotFoundException
29
     */
30 2
    public function makeThumbnail(string $videoFile, string $outputFile, float $time = 0.0, ?VideoFilterInterface $videoFilter = null): void
31
    {
32 2
        $this->ensureFileExists($videoFile);
33
34 1
        if ($videoFilter === null) {
35 1
            $videoFilter = new EmptyVideoFilter();
36
        }
37
38 1
        $process = $this->ffmpegConfig->getProcess();
39
40 1
        $ffmpegCmd = $process->buildCommand(
41
            [
42 1
                ($time > 0.0) ? sprintf('-ss %s', $time) : '', // putting time in front is much more efficient
43 1
                sprintf('-i %s', escapeshellarg($videoFile)),  // input filename
44 1
                $videoFilter->getFFMpegCLIArgument(),                // add -vf yadif,nlmeans
45 1
                '-frames:v 1',
46 1
                '-q:v 2',
47 1
                '-y', // tell to overwrite
48 1
                sprintf('%s', escapeshellarg($outputFile)),
49
            ]
50
        );
51
52 1
        $process->runCommand($ffmpegCmd);
53 1
    }
54
}
55