Passed
Push — master ( 6309f1...8af892 )
by Pascal
02:12
created

FFProbe::probeStreams()   B

Complexity

Conditions 9
Paths 18

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 26
c 1
b 0
f 0
nc 18
nop 2
dl 0
loc 42
rs 8.0555
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
    /**
13
     * @var \ProtoneMedia\LaravelFFMpeg\Filesystem\Media|\ProtoneMedia\LaravelFFMpeg\Filesystem\MediaOnNetwork
14
     */
15
    protected $media;
16
17
    public function setMedia($media): self
18
    {
19
        $this->media = $media;
20
21
        return $this;
22
    }
23
24
    /**
25
     * Create a new instance of this class with the instance of the underlying library.
26
     *
27
     * @param \FFMpeg\FFProbe $probe
28
     * @return self
29
     */
30
    public static function make(FFMpegFFProbe $probe): self
31
    {
32
        if ($probe instanceof FFProbe) {
33
            return $probe;
34
        }
35
36
        return new static($probe->getFFProbeDriver(), $probe->getCache());
37
    }
38
39
    /**
40
     * Probes the streams contained in a given file.
41
     *
42
     * @param string $pathfile
43
     * @return \FFMpeg\FFProbe\DataMapping\StreamCollection
44
     * @throws \FFMpeg\Exception\InvalidArgumentException
45
     * @throws \FFMpeg\Exception\RuntimeException
46
     */
47
48
    public function streams($pathfile)
49
    {
50
        if (!$this->media instanceof MediaOnNetwork || $this->media->getLocalPath() !== $pathfile) {
51
            return parent::streams($pathfile);
52
        }
53
54
        if (!$this->getOptionsTester()->has('-show_streams')) {
55
            throw new RuntimeException('This version of ffprobe is too old and does not support `-show_streams` option, please upgrade');
56
        }
57
58
        return $this->probeStreams($pathfile);
59
    }
60
61
    /**
62
     * This is just copy-paste from FFMpeg\FFProbe...
63
     * It prepends the command with the headers.
64
     */
65
    private function probeStreams($pathfile, $allowJson = true)
66
    {
67
        $commands = array_merge(
68
            $this->media->getCompiledHeaders(),
0 ignored issues
show
Bug introduced by
The method getCompiledHeaders() does not exist on ProtoneMedia\LaravelFFMpeg\Filesystem\Media. ( Ignorable by Annotation )

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

68
            $this->media->/** @scrutinizer ignore-call */ 
69
                          getCompiledHeaders(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
            [$pathfile, '-show_streams']
70
        );
71
72
        $parseIsToDo = false;
73
74
        if ($allowJson && $this->getOptionsTester()->has('-print_format')) {
75
            // allowed in latest PHP-FFmpeg version
76
            $commands[] = '-print_format';
77
            $commands[] = 'json';
78
        } elseif ($allowJson && $this->getOptionsTester()->has('-of')) {
79
            // option has changed in avconv 9
80
            $commands[] = '-of';
81
            $commands[] = 'json';
82
        } else {
83
            $parseIsToDo = true;
84
        }
85
86
        try {
87
            $output = $this->getFFProbeDriver()->command($commands);
88
        } catch (ExecutionFailureException $e) {
89
            throw new RuntimeException(sprintf('Unable to probe %s', $pathfile), $e->getCode(), $e);
90
        }
91
92
        if ($parseIsToDo) {
93
            $data = $this->getParser()->parse(static::TYPE_STREAMS, $output);
94
        } else {
95
            try {
96
                $data = @json_decode($output, true);
97
98
                if (JSON_ERROR_NONE !== json_last_error()) {
99
                    throw new RuntimeException(sprintf('Unable to parse json %s', $output));
100
                }
101
            } catch (RuntimeException $e) {
102
                return $this->probeStreams($pathfile, false);
103
            }
104
        }
105
106
        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

106
        return $this->getMapper()->map(static::TYPE_STREAMS, /** @scrutinizer ignore-type */ $data);
Loading history...
107
    }
108
}
109