TranscodingStreamer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 23.81%

Importance

Changes 0
Metric Value
wmc 4
eloc 20
c 0
b 0
f 0
dl 0
loc 53
ccs 5
cts 21
cp 0.2381
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setBitRate() 0 3 1
A stream() 0 24 2
A setStartTime() 0 3 1
1
<?php
2
3
namespace App\Services\Streamers;
4
5
class TranscodingStreamer extends Streamer implements TranscodingStreamerInterface
6
{
7
    /**
8
     * Bit rate the stream should be transcoded at.
9
     *
10
     * @var int
11
     */
12
    private $bitRate;
13
14
    /**
15
     * Time point to start transcoding from.
16
     *
17
     * @var float
18
     */
19
    private $startTime;
20
21
    /**
22
     * On-the-fly stream the current song while transcoding.
23
     */
24
    public function stream(): void
25
    {
26
        $ffmpeg = config('koel.streaming.ffmpeg_path');
27
        abort_unless(is_executable($ffmpeg), 500, 'Transcoding requires valid ffmpeg settings.');
28
29
        $bitRate = filter_var($this->bitRate, FILTER_SANITIZE_NUMBER_INT);
30
31
        header('Content-Type: audio/mpeg');
32
        header('Content-Disposition: attachment; filename="'.basename($this->song->path).'"');
33
34
        $args = [
35
            '-i '.escapeshellarg($this->song->path),
36
            '-map 0:0',
37
            '-v 0',
38
            "-ab {$bitRate}k",
39
            '-f mp3',
40
            '-',
41
        ];
42
43
        if ($this->startTime) {
44
            array_unshift($args, "-ss {$this->startTime}");
45
        }
46
47
        passthru("$ffmpeg ".implode(' ', $args));
48
    }
49
50 2
    public function setBitRate(int $bitRate): void
51
    {
52 2
        $this->bitRate = $bitRate;
53 2
    }
54
55 2
    public function setStartTime(float $startTime): void
56
    {
57 2
        $this->startTime = $startTime;
58 2
    }
59
}
60