Passed
Push — master ( 4422d3...1f3291 )
by Pascal
19:42 queued 15:59
created

HLSPlaylistGenerator::getResolution()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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 getPathOfFirstSegment(Media $playlistMedia): string
18
    {
19
        $playlistContent = file_get_contents($playlistMedia->getLocalPath());
20
21
        return Collection::make(explode(PHP_EOL, $playlistContent))->first(function ($line) {
22
            return !Str::startsWith($line, '#') && Str::endsWith($line, '.ts');
23
        });
24
    }
25
26
    private function getBandwidth(MediaOpener $media)
27
    {
28
        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

28
        return $media->/** @scrutinizer ignore-call */ getFormat()->get('bit_rate');
Loading history...
29
    }
30
31
    private function getResolution(MediaOpener $media)
32
    {
33
        try {
34
            $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

34
            $dimensions = optional($media->/** @scrutinizer ignore-call */ getVideoStream())->getDimensions();
Loading history...
35
        } catch (Exception $exception) {
36
            return null;
37
        }
38
39
        return "{$dimensions->getWidth()}x{$dimensions->getHeight()}";
40
    }
41
42
    private function getFrameRate(MediaOpener $media)
43
    {
44
        $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...
45
46
        $frameRate = trim(Str::before(optional($mediaStream)->get('avg_frame_rate'), "/1"));
47
48
        if (!$frameRate || Str::endsWith($frameRate, '/0')) {
49
            return null;
50
        }
51
52
        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

52
        return $frameRate ? number_format(/** @scrutinizer ignore-type */ $frameRate, 3, '.', '') : null;
Loading history...
53
    }
54
55
    public function get(array $playlistMedia, PHPFFMpeg $driver): string
56
    {
57
        return Collection::make($playlistMedia)->map(function (Media $playlistMedia) use ($driver) {
58
            $media = (new MediaOpener($playlistMedia->getDisk(), $driver->fresh()))->open(
59
                $playlistMedia->getDirectory() . $this->getPathOfFirstSegment($playlistMedia)
60
            );
61
62
            $streamInfo = [
63
                "#EXT-X-STREAM-INF:BANDWIDTH={$this->getBandwidth($media)}",
64
                "RESOLUTION={$this->getResolution($media)}",
65
            ];
66
67
            if ($frameRate = $this->getFrameRate($media)) {
68
                $streamInfo[] = "FRAME-RATE={$frameRate}";
69
            }
70
71
            return [implode(',', $streamInfo), $playlistMedia->getFilename()];
72
        })->collapse()
73
            ->prepend(static::PLAYLIST_START)
74
            ->push(static::PLAYLIST_END)
75
            ->implode(PHP_EOL);
76
    }
77
}
78