Passed
Branch v7 (1219d8)
by Pascal
02:59
created

HLSPlaylistGenerator::getPathOfFirstSegment()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 1
nop 1
1
<?php
2
3
namespace Pbmedia\LaravelFFMpeg\Exporters;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Pbmedia\LaravelFFMpeg\Drivers\PHPFFMpeg;
8
use Pbmedia\LaravelFFMpeg\Filesystem\Media;
9
use Pbmedia\LaravelFFMpeg\MediaOpener;
10
11
class HLSPlaylistGenerator implements PlaylistGenerator
12
{
13
    const PLAYLIST_START = '#EXTM3U';
14
    const PLAYLIST_END   = '#EXT-X-ENDLIST';
15
16
    private function getPathOfFirstSegment(Media $playlistMedia): string
17
    {
18
        $playlistContent = file_get_contents($playlistMedia->getLocalPath());
19
20
        return Collection::make(explode(PHP_EOL, $playlistContent))->first(function ($line) {
21
            return !Str::startsWith($line, '#') && Str::endsWith($line, '.ts');
22
        });
23
    }
24
25
    private function getBandwidth(MediaOpener $media)
26
    {
27
        return $media->getFormat()->get('bit_rate');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $media->getFormat() targeting Pbmedia\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 Pbmedia\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

27
        return $media->/** @scrutinizer ignore-call */ getFormat()->get('bit_rate');
Loading history...
28
    }
29
30
    private function getResolution(MediaOpener $media)
31
    {
32
        $mediaStream = $media->getStreams()[0];
0 ignored issues
show
Bug introduced by
The method getStreams() does not exist on Pbmedia\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

32
        $mediaStream = $media->/** @scrutinizer ignore-call */ getStreams()[0];
Loading history...
Bug introduced by
Are you sure the usage of $media->getStreams() targeting Pbmedia\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...
33
34
        return "{$mediaStream->get('width')}x{$mediaStream->get('height')}";
35
    }
36
37
    private function getFrameRate(MediaOpener $media)
38
    {
39
        $mediaStream = $media->getStreams()[0];
0 ignored issues
show
Bug introduced by
Are you sure the usage of $media->getStreams() targeting Pbmedia\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...
40
        $frameRate   = trim(Str::before($mediaStream->get('avg_frame_rate'), "/1"));
41
42
        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

42
        return $frameRate ? number_format(/** @scrutinizer ignore-type */ $frameRate, 3, '.', '') : null;
Loading history...
43
    }
44
45
    public function get(array $playlistMedia, PHPFFMpeg $driver): string
46
    {
47
        return Collection::make($playlistMedia)->map(function (Media $playlistMedia) use ($driver) {
48
            $media = (new MediaOpener($playlistMedia->getDisk(), $driver->fresh()))->open(
49
                $this->getPathOfFirstSegment($playlistMedia)
50
            );
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->getPath()];
62
        })->collapse()
63
            ->prepend(static::PLAYLIST_START)
64
            ->push(static::PLAYLIST_END)
65
            ->implode(PHP_EOL);
66
    }
67
}
68