| Total Complexity | 4 |
| Total Lines | 53 |
| Duplicated Lines | 0 % |
| Coverage | 23.81% |
| Changes | 0 | ||
| 1 | <?php |
||
| 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 |
|
| 53 | 2 | } |
|
| 54 | |||
| 55 | 2 | public function setStartTime(float $startTime): void |
|
| 60 |