Passed
Pull Request — master (#232)
by Pascal
03:07
created

FFProbe::setMedia()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
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);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type array; however, parameter $data of FFMpeg\FFProbe\MapperInterface::map() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
        return $this->getMapper()->map(static::TYPE_STREAMS, /** @scrutinizer ignore-type */ $data);
Loading history...
88
    }
89
}
90