Failed Conditions
Push — master ( 85c284...561f65 )
by Sébastien
02:44
created

VideoThumbService::makeThumbnail()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.679

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 10
ccs 3
cts 7
cp 0.4286
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 5
crap 4.679
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\Converter\FFMpegAdapter;
11
use Soluble\MediaTools\Video\Filter\Type\VideoFilterInterface;
12
use Soluble\MediaTools\Video\SeekTime;
13
use Soluble\MediaTools\Video\ThumbServiceInterface;
14
use Symfony\Component\Process\Exception as SPException;
15
use Symfony\Component\Process\Process;
16
17
class VideoThumbService implements ThumbServiceInterface
18
{
19
    use PathAssertionsTrait;
20
21
    /** @var FFMpegConfig */
22
    protected $ffmpegConfig;
23
24
    /** @var FFMpegAdapter */
25
    protected $adapter;
26
27 2
    public function __construct(FFMpegConfig $ffmpegConfig)
28
    {
29 2
        $this->ffmpegConfig = $ffmpegConfig;
30 2
        $this->adapter      = new FFMpegAdapter($ffmpegConfig);
31 2
    }
32
33
    /**
34
     * Return ready-to-run symfony process object that you can use
35
     * to `run()` or `start()` programmatically. Useful if you want
36
     * handle the process your way...
37
     *
38
     * @see https://symfony.com/doc/current/components/process.html
39
     *
40
     * @throws FileNotFoundException when inputFile does not exists
41
     */
42 2
    public function getConversionProcess(string $videoFile, string $thumbnailFile, ?SeekTime $time = null, ?VideoFilterInterface $videoFilter = null): Process
43
    {
44 2
        $this->ensureFileExists($videoFile);
45
46 1
        $params = (new VideoConversionParams());
47 1
        if ($time !== null) {
48
            // For performance reasons time seek must be
49
            // made at the beginning of options
50 1
            $params = $params->withSeekStart($time);
51
        }
52 1
        $params = $params->withVideoFrames(1);
53
54 1
        if ($videoFilter !== null) {
55
            $params = $params->withVideoFilter($videoFilter);
56
        }
57
58
        // Quality scale for the mjpeg encoder
59 1
        $params->withVideoQualityScale(2);
60
61 1
        $arguments = $this->adapter->getMappedConversionParams($params);
62 1
        $ffmpegCmd = $this->adapter->getCliCommand($arguments, $videoFile, $thumbnailFile);
63
64 1
        $process = new Process($ffmpegCmd);
65 1
        $process->setTimeout($this->ffmpegConfig->getConversionTimeout());
66 1
        $process->setIdleTimeout($this->ffmpegConfig->getConversionIdleTimeout());
67 1
        $process->setEnv($this->ffmpegConfig->getConversionEnv());
68
69 1
        return $process;
70
    }
71
72
    /**
73
     * @throws FileNotFoundException
74
     * @throws SPException\RuntimeException
75
     */
76 2
    public function makeThumbnail(string $videoFile, string $thumbnailFile, ?SeekTime $time = null, ?VideoFilterInterface $videoFilter = null, ?callable $callback = null): void
77
    {
78 2
        $process = $this->getConversionProcess($videoFile, $thumbnailFile, $time, $videoFilter);
79
        try {
80 1
            $process->mustRun($callback);
81
        } catch (SPException\RuntimeException $symfonyProcessException) {
82
            // will include: ProcessFailedException|ProcessTimedOutException|ProcessSignaledException
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
            throw $symfonyProcessException;
84
        } catch (FileNotFoundException $e) {
85
            throw $e;
86
        }
87 1
    }
88
}
89