|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Soluble\MediaTools; |
|
6
|
|
|
|
|
7
|
|
|
use Soluble\MediaTools\Config\FFProbeConfig; |
|
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 FFProbeConfig */ |
|
19
|
|
|
protected $ffprobeConfig; |
|
20
|
|
|
|
|
21
|
2 |
|
public function __construct(FFProbeConfig $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
|
|
|
* @param string $file |
|
61
|
|
|
* |
|
62
|
|
|
* @return Info |
|
63
|
|
|
* |
|
64
|
|
|
* @throws FileNotFoundException |
|
65
|
|
|
* @throws \Throwable |
|
66
|
|
|
*/ |
|
67
|
2 |
|
public function getInfo(string $file): Info |
|
68
|
|
|
{ |
|
69
|
2 |
|
$process = $this->getFFProbeProcess($file); |
|
70
|
|
|
|
|
71
|
|
|
try { |
|
72
|
1 |
|
$process->mustRun(); |
|
73
|
1 |
|
$output = $process->getOutput(); |
|
74
|
|
|
} catch (\Throwable $e) { |
|
75
|
|
|
throw $e; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
return Info::createFromFFProbeJson($file, $output); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|