Passed
Push — master ( 1e8bfe...85c284 )
by Sébastien
03:02
created

VideoThumbService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 17
dl 0
loc 40
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A makeThumbnail() 0 23 3
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools;
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
use Soluble\MediaTools\Video\ThumbServiceInterface;
13
14
class VideoThumbService implements ThumbServiceInterface
15
{
16
    use PathAssertionsTrait;
17
18
    /** @var FFMpegConfig */
19
    protected $ffmpegConfig;
20
21 2
    public function __construct(FFMpegConfig $ffmpegConfig)
22
    {
23 2
        $this->ffmpegConfig = $ffmpegConfig;
24 2
    }
25
26
    /**
27
     * @param null|VideoFilterInterface $videoFilter
28
     *
29
     * @throws FileNotFoundException
30
     */
31 2
    public function makeThumbnail(string $videoFile, string $outputFile, float $time = 0.0, ?VideoFilterInterface $videoFilter = null): void
32
    {
33 2
        $this->ensureFileExists($videoFile);
34
35 1
        if ($videoFilter === null) {
36 1
            $videoFilter = new EmptyVideoFilter();
37
        }
38
39 1
        $process = $this->ffmpegConfig->getProcess();
40
41 1
        $ffmpegCmd = $process->buildCommand(
42
            [
43 1
                ($time > 0.0) ? sprintf('-ss %s', $time) : '', // putting time in front is much more efficient
44 1
                sprintf('-i %s', escapeshellarg($videoFile)),  // input filename
45 1
                $videoFilter->getFFMpegCLIArgument(),                // add -vf yadif,nlmeans
46 1
                '-frames:v 1',
47 1
                '-q:v 2',
48 1
                '-y', // tell to overwrite
49 1
                sprintf('%s', escapeshellarg($outputFile)),
50
            ]
51
        );
52
53 1
        $process->runCommand($ffmpegCmd);
54 1
    }
55
}
56