Passed
Pull Request — master (#262)
by Pascal
02:44
created

HLSPlaylistGenerator::getPathOfFirstSegment()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\Exporters;
4
5
use Exception;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Str;
8
use ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg;
9
use ProtoneMedia\LaravelFFMpeg\Filesystem\Media;
10
use ProtoneMedia\LaravelFFMpeg\MediaOpener;
11
12
class HLSPlaylistGenerator implements PlaylistGenerator
13
{
14
    const PLAYLIST_START = '#EXTM3U';
15
    const PLAYLIST_END   = '#EXT-X-ENDLIST';
16
17
    private function getBandwidth(MediaOpener $media)
18
    {
19
        return $media->getFormat()->get('bit_rate');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $media->getFormat() targeting ProtoneMedia\LaravelFFMpeg\MediaOpener::__call() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
The method getFormat() does not exist on ProtoneMedia\LaravelFFMpeg\MediaOpener. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

19
        return $media->/** @scrutinizer ignore-call */ getFormat()->get('bit_rate');
Loading history...
20
    }
21
22
    private function getResolution(MediaOpener $media)
23
    {
24
        try {
25
            $dimensions = optional($media->getVideoStream())->getDimensions();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $media->getVideoStream() targeting ProtoneMedia\LaravelFFMpeg\MediaOpener::__call() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
The method getVideoStream() does not exist on ProtoneMedia\LaravelFFMpeg\MediaOpener. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

25
            $dimensions = optional($media->/** @scrutinizer ignore-call */ getVideoStream())->getDimensions();
Loading history...
26
        } catch (Exception $exception) {
27
            return null;
28
        }
29
30
        return "{$dimensions->getWidth()}x{$dimensions->getHeight()}";
31
    }
32
33
    private function getFrameRate(MediaOpener $media)
34
    {
35
        $mediaStream = $media->getVideoStream();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $mediaStream is correct as $media->getVideoStream() targeting ProtoneMedia\LaravelFFMpeg\MediaOpener::__call() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
36
37
        $frameRate = trim(Str::before(optional($mediaStream)->get('avg_frame_rate'), "/1"));
38
39
        if (!$frameRate || Str::endsWith($frameRate, '/0')) {
40
            return null;
41
        }
42
43
        return $frameRate ? number_format($frameRate, 3, '.', '') : null;
0 ignored issues
show
Bug introduced by
$frameRate of type string is incompatible with the type double expected by parameter $number of number_format(). ( Ignorable by Annotation )

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

43
        return $frameRate ? number_format(/** @scrutinizer ignore-type */ $frameRate, 3, '.', '') : null;
Loading history...
44
    }
45
46
    public function get(array $playlistMedia, PHPFFMpeg $driver): string
47
    {
48
        return Collection::make($playlistMedia)->map(function (Media $playlistMedia) use ($driver) {
49
            $media = (new MediaOpener($playlistMedia->getDisk(), $driver))
50
                ->openWithInputOptions($playlistMedia->getPath(), ['-allowed_extensions', 'ALL']);
51
52
            $streamInfo = [
53
                "#EXT-X-STREAM-INF:BANDWIDTH={$this->getBandwidth($media)}",
54
                "RESOLUTION={$this->getResolution($media)}",
55
            ];
56
57
            if ($frameRate = $this->getFrameRate($media)) {
58
                $streamInfo[] = "FRAME-RATE={$frameRate}";
59
            }
60
61
            return [implode(',', $streamInfo), $playlistMedia->getFilename()];
62
        })->collapse()
63
            ->prepend(static::PLAYLIST_START)
64
            ->push(static::PLAYLIST_END)
65
            ->implode(PHP_EOL);
66
    }
67
}
68