Passed
Pull Request — master (#341)
by Pascal
12:13
created

HLSPlaylistGenerator::setUuid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\Exporters;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg;
8
use ProtoneMedia\LaravelFFMpeg\Filesystem\Media;
9
use ProtoneMedia\LaravelFFMpeg\Http\DynamicHLSPlaylist;
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
    /**
18
     * @var string
19
     */
20
    private $uuid;
21
22
    /**
23
     * Setter for the UUID.
24
     *
25
     * @param string $uuid
26
     * @return self
27
     */
28
    public function setUuid(string $uuid): self
29
    {
30
        $this->uuid = $uuid;
31
32
        return $this;
33
    }
34
35
    /**
36
     * Extracts the framerate from the given media and formats it in a
37
     * suitable format.
38
     *
39
     * @param \ProtoneMedia\LaravelFFMpeg\MediaOpener $media
40
     * @return mixed
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...
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

44
        /** @scrutinizer ignore-call */ 
45
        $mediaStream = $media->getVideoStream();
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
        if (Str::contains($frameRate, '/')) {
53
            [$numerator, $denominator] = explode('/', $frameRate);
54
55
            $frameRate = $numerator / $denominator;
56
        }
57
58
        return $frameRate ? number_format($frameRate, 3, '.', '') : null;
0 ignored issues
show
Bug introduced by
It seems like $frameRate can also be of type string; however, parameter $num of number_format() does only seem to accept double, 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

58
        return $frameRate ? number_format(/** @scrutinizer ignore-type */ $frameRate, 3, '.', '') : null;
Loading history...
59
    }
60
61
    /**
62
     * Return the line from the master playlist that references the given segment playlist.
63
     *
64
     * @param \ProtoneMedia\LaravelFFMpeg\Filesystem\Media $playlistMedia
65
     * @param string $key
66
     * @return string
67
     */
68
    private function getStreamInfoLine(Media $segmentPlaylistMedia, string $key): string
69
    {
70
        $segmentPlaylist = $segmentPlaylistMedia->getDisk()->get(
0 ignored issues
show
Bug introduced by
The method get() does not exist on ProtoneMedia\LaravelFFMpeg\Filesystem\Disk. 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

70
        $segmentPlaylist = $segmentPlaylistMedia->getDisk()->/** @scrutinizer ignore-call */ get(
Loading history...
71
            $segmentPlaylistMedia->getDirectory() . HLSExporter::generateTemporarySegmentPlaylistFilename($key, $this->uuid)
0 ignored issues
show
Bug introduced by
$key of type string is incompatible with the type integer expected by parameter $key of ProtoneMedia\LaravelFFMp...gmentPlaylistFilename(). ( Ignorable by Annotation )

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

71
            $segmentPlaylistMedia->getDirectory() . HLSExporter::generateTemporarySegmentPlaylistFilename(/** @scrutinizer ignore-type */ $key, $this->uuid)
Loading history...
72
        );
73
74
        $lines = DynamicHLSPlaylist::parseLines($segmentPlaylist)->filter();
75
76
        return $lines->get($lines->search($segmentPlaylistMedia->getFilename()) - 1);
77
    }
78
79
    /**
80
     * Loops through all segment playlists and generates a main playlist. It finds
81
     * the relative paths to the segment playlists and adds the framerate when
82
     * to each playlist.
83
     *
84
     * @param array $segmentPlaylists
85
     * @param \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg $driver
86
     * @return string
87
     */
88
    public function get(array $segmentPlaylists, PHPFFMpeg $driver): string
89
    {
90
        return Collection::make($segmentPlaylists)->map(function (Media $segmentPlaylist, $key) use ($driver) {
91
            $streamInfoLine = $this->getStreamInfoLine($segmentPlaylist, $key);
92
93
            $media = (new MediaOpener($segmentPlaylist->getDisk(), $driver))
94
                ->openWithInputOptions($segmentPlaylist->getPath(), ['-allowed_extensions', 'ALL']);
95
96
            if ($frameRate = $this->getFrameRate($media)) {
97
                $streamInfoLine .= ",FRAME-RATE={$frameRate}";
98
            }
99
100
            return [$streamInfoLine, $segmentPlaylist->getFilename()];
101
        })->collapse()
102
            ->prepend(static::PLAYLIST_START)
103
            ->push(static::PLAYLIST_END)
104
            ->implode(PHP_EOL);
105
    }
106
}
107