HLSPlaylistGenerator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 75
rs 10
c 3
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getStreamInfoLine() 0 9 1
A getFrameRate() 0 17 5
A get() 0 17 2
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
     * Extracts the framerate from the given media and formats it in a
19
     * suitable format.
20
     *
21
     * @param \ProtoneMedia\LaravelFFMpeg\MediaOpener $media
22
     * @return mixed
23
     */
24
    private function getFrameRate(MediaOpener $media)
25
    {
26
        $mediaStream = $media->getVideoStream();
0 ignored issues
show
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

26
        /** @scrutinizer ignore-call */ 
27
        $mediaStream = $media->getVideoStream();
Loading history...
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...
27
28
        $frameRate = trim(Str::before(optional($mediaStream)->get('avg_frame_rate'), "/1"));
29
30
        if (!$frameRate || Str::endsWith($frameRate, '/0')) {
31
            return null;
32
        }
33
34
        if (Str::contains($frameRate, '/')) {
35
            [$numerator, $denominator] = explode('/', $frameRate);
36
37
            $frameRate = $numerator / $denominator;
38
        }
39
40
        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

40
        return $frameRate ? number_format(/** @scrutinizer ignore-type */ $frameRate, 3, '.', '') : null;
Loading history...
41
    }
42
43
    /**
44
     * Return the line from the master playlist that references the given segment playlist.
45
     *
46
     * @param \ProtoneMedia\LaravelFFMpeg\Filesystem\Media $playlistMedia
47
     * @param string $key
48
     * @return string
49
     */
50
    private function getStreamInfoLine(Media $segmentPlaylistMedia, string $key): string
51
    {
52
        $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

52
        $segmentPlaylist = $segmentPlaylistMedia->getDisk()->/** @scrutinizer ignore-call */ get(
Loading history...
53
            $segmentPlaylistMedia->getDirectory() . HLSExporter::generateTemporarySegmentPlaylistFilename($key)
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

53
            $segmentPlaylistMedia->getDirectory() . HLSExporter::generateTemporarySegmentPlaylistFilename(/** @scrutinizer ignore-type */ $key)
Loading history...
54
        );
55
56
        $lines = DynamicHLSPlaylist::parseLines($segmentPlaylist)->filter();
57
58
        return $lines->get($lines->search($segmentPlaylistMedia->getFilename()) - 1);
59
    }
60
61
    /**
62
     * Loops through all segment playlists and generates a main playlist. It finds
63
     * the relative paths to the segment playlists and adds the framerate when
64
     * to each playlist.
65
     *
66
     * @param array $segmentPlaylists
67
     * @param \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg $driver
68
     * @return string
69
     */
70
    public function get(array $segmentPlaylists, PHPFFMpeg $driver): string
71
    {
72
        return Collection::make($segmentPlaylists)->map(function (Media $segmentPlaylist, $key) use ($driver) {
73
            $streamInfoLine = $this->getStreamInfoLine($segmentPlaylist, $key);
74
75
            $media = (new MediaOpener($segmentPlaylist->getDisk(), $driver))
76
                ->openWithInputOptions($segmentPlaylist->getPath(), ['-allowed_extensions', 'ALL']);
77
78
            if ($frameRate = $this->getFrameRate($media)) {
79
                $streamInfoLine .= ",FRAME-RATE={$frameRate}";
80
            }
81
82
            return [$streamInfoLine, $segmentPlaylist->getFilename()];
83
        })->collapse()
84
            ->prepend(static::PLAYLIST_START)
85
            ->push(static::PLAYLIST_END)
86
            ->implode(PHP_EOL);
87
    }
88
}
89