|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelFFMpeg\FFMpeg; |
|
4
|
|
|
|
|
5
|
|
|
use Alchemy\BinaryDriver\Exception\ExecutionFailureException; |
|
6
|
|
|
use FFMpeg\Exception\RuntimeException; |
|
7
|
|
|
use FFMpeg\FFProbe as FFMpegFFProbe; |
|
8
|
|
|
use ProtoneMedia\LaravelFFMpeg\Filesystem\MediaOnNetwork; |
|
9
|
|
|
|
|
10
|
|
|
class FFProbe extends FFMpegFFProbe |
|
11
|
|
|
{ |
|
12
|
|
|
protected $media; |
|
13
|
|
|
|
|
14
|
|
|
public static function make(FFMpegFFProbe $probe): self |
|
15
|
|
|
{ |
|
16
|
|
|
if ($probe instanceof FFProbe) { |
|
17
|
|
|
return $probe; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
return new static($probe->getFFProbeDriver(), $probe->getCache()); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function setMedia($media): self |
|
24
|
|
|
{ |
|
25
|
|
|
$this->media = $media; |
|
26
|
|
|
|
|
27
|
|
|
return $this; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function streams($pathfile) |
|
31
|
|
|
{ |
|
32
|
|
|
if (!$this->media instanceof MediaOnNetwork || $this->media->getLocalPath() !== $pathfile) { |
|
33
|
|
|
return parent::streams($pathfile); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if (!$this->getOptionsTester()->has('-show_streams')) { |
|
37
|
|
|
throw new RuntimeException('This version of ffprobe is too old and does not support `-show_streams` option, please upgrade'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $this->probeStreams($pathfile); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* This is just copy-paste from FFMpeg\FFProbe... |
|
45
|
|
|
*/ |
|
46
|
|
|
private function probeStreams($pathfile, $allowJson = true) |
|
47
|
|
|
{ |
|
48
|
|
|
$commands = array_merge( |
|
49
|
|
|
$this->media->getCompiledHeaders(), |
|
50
|
|
|
[$pathfile, '-show_streams'] |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
$parseIsToDo = false; |
|
54
|
|
|
|
|
55
|
|
|
if ($allowJson && $this->getOptionsTester()->has('-print_format')) { |
|
56
|
|
|
// allowed in latest PHP-FFmpeg version |
|
57
|
|
|
$commands[] = '-print_format'; |
|
58
|
|
|
$commands[] = 'json'; |
|
59
|
|
|
} elseif ($allowJson && $this->getOptionsTester()->has('-of')) { |
|
60
|
|
|
// option has changed in avconv 9 |
|
61
|
|
|
$commands[] = '-of'; |
|
62
|
|
|
$commands[] = 'json'; |
|
63
|
|
|
} else { |
|
64
|
|
|
$parseIsToDo = true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
try { |
|
68
|
|
|
$output = $this->getFFProbeDriver()->command($commands); |
|
69
|
|
|
} catch (ExecutionFailureException $e) { |
|
70
|
|
|
throw new RuntimeException(sprintf('Unable to probe %s', $pathfile), $e->getCode(), $e); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if ($parseIsToDo) { |
|
74
|
|
|
$data = $this->getParser()->parse(static::TYPE_STREAMS, $output); |
|
75
|
|
|
} else { |
|
76
|
|
|
try { |
|
77
|
|
|
$data = @json_decode($output, true); |
|
78
|
|
|
|
|
79
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
80
|
|
|
throw new RuntimeException(sprintf('Unable to parse json %s', $output)); |
|
81
|
|
|
} |
|
82
|
|
|
} catch (RuntimeException $e) { |
|
83
|
|
|
return $this->probeStreams($pathfile, false); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->getMapper()->map(static::TYPE_STREAMS, $data); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|