Passed
Push — master ( 6bec5a...604d79 )
by Pascal
03:42
created

HLSPlaylistGenerator::getStreamInfoLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 9
rs 10
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
        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 $num 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

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

46
        $segmentPlaylist = $segmentPlaylistMedia->getDisk()->/** @scrutinizer ignore-call */ get(
Loading history...
47
            $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

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