TranscodingStreamer::stream()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 24
ccs 0
cts 15
cp 0
rs 9.7666
cc 2
nc 2
nop 0
crap 6
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