Completed
Push — master ( aea97d...ec8061 )
by Sébastien
04:37
created

VideoInfoService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
wmc 4
eloc 26
dl 0
loc 61
ccs 21
cts 23
cp 0.913
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getFFProbeProcess() 0 22 1
A getInfo() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools;
6
7
use Soluble\MediaTools\Config\FFProbeConfigInterface;
8
use Soluble\MediaTools\Exception\FileNotFoundException;
9
use Soluble\MediaTools\Util\Assert\PathAssertionsTrait;
10
use Soluble\MediaTools\Video\Info;
11
use Soluble\MediaTools\Video\InfoServiceInterface;
12
use Symfony\Component\Process\Process;
13
14
class VideoInfoService implements InfoServiceInterface
15
{
16
    use PathAssertionsTrait;
17
18
    /** @var FFProbeConfigInterface */
19
    protected $ffprobeConfig;
20
21 2
    public function __construct(FFProbeConfigInterface $ffProbeConfig)
22
    {
23 2
        $this->ffprobeConfig = $ffProbeConfig;
24 2
    }
25
26
    /**
27
     * Return ready-to-run symfony process object that you can use
28
     * to `run()` or `start()` programmatically. Useful if you want to make
29
     * things your way...
30
     *
31
     * @see https://symfony.com/doc/current/components/process.html
32
     *
33
     * @throws FileNotFoundException when inputFile does not exists
34
     */
35 2
    public function getFFProbeProcess(string $inputFile): Process
36
    {
37 2
        $this->ensureFileExists($inputFile);
38
39 1
        $ffprobeCmd = trim(sprintf(
40 1
            '%s %s %s',
41 1
            $this->ffprobeConfig->getBinary(),
42 1
            implode(' ', [
43 1
                '-v quiet',
44
                '-print_format json',
45
                '-show_format',
46
                '-show_streams',
47
            ]),
48 1
            sprintf('-i %s', escapeshellarg($inputFile))
49
        ));
50
51 1
        $process = new Process($ffprobeCmd);
52 1
        $process->setTimeout($this->ffprobeConfig->getTimeout());
53 1
        $process->setIdleTimeout($this->ffprobeConfig->getIdleTimeout());
54 1
        $process->setEnv($this->ffprobeConfig->getEnv());
55
56 1
        return $process;
57
    }
58
59
    /**
60
     * @throws FileNotFoundException
61
     * @throws \Throwable
62
     */
63 2
    public function getInfo(string $file): Info
64
    {
65 2
        $process = $this->getFFProbeProcess($file);
66
67
        try {
68 1
            $process->mustRun();
69 1
            $output = $process->getOutput();
70
        } catch (\Throwable $e) {
71
            throw $e;
72
        }
73
74 1
        return Info::createFromFFProbeJson($file, $output);
75
    }
76
}
77