| Conditions | 6 |
| Paths | 10 |
| Total Lines | 42 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 42 |
| Changes | 0 | ||
| 1 | <?php |
||
| 24 | public function stream(): void |
||
| 25 | { |
||
| 26 | $ffmpeg = config('koel.streaming.ffmpeg_path'); |
||
| 27 | $codec = config('koel.streaming.ffmpeg_codec'); |
||
| 28 | abort_unless(is_executable($ffmpeg), 500, 'Transcoding requires valid ffmpeg settings.'); |
||
| 29 | |||
| 30 | //used the chart on https://en.wikipedia.org/wiki/HTML5_audio#Supported_audio_coding_formats to match up formats to container types. |
||
| 31 | $container = null; |
||
| 32 | switch($codec) |
||
| 33 | { |
||
| 34 | case 'libopus': |
||
| 35 | case 'libvorbis': |
||
| 36 | $container = 'ogg'; |
||
| 37 | break; |
||
| 38 | case 'libmp3lame': |
||
| 39 | $container = 'mp3'; |
||
| 40 | break; |
||
| 41 | case 'aac': |
||
| 42 | $container = 'adts'; |
||
| 43 | break; |
||
| 44 | } |
||
| 45 | |||
| 46 | $bitRate = filter_var($this->bitRate, FILTER_SANITIZE_NUMBER_INT); |
||
| 47 | |||
| 48 | header('Content-Type: audio/mpeg'); |
||
| 49 | header('Content-Disposition: attachment; filename="'.basename($this->song->path).'"'); |
||
| 50 | |||
| 51 | $args = [ |
||
| 52 | '-i '.escapeshellarg($this->song->path), |
||
| 53 | '-map 0:0', |
||
| 54 | '-v 0', |
||
| 55 | "-b:a {$bitRate}k", |
||
| 56 | "-c:a {$codec}", |
||
| 57 | "-f {$container}", |
||
| 58 | '-', |
||
| 59 | ]; |
||
| 60 | |||
| 61 | if ($this->startTime) { |
||
| 62 | array_unshift($args, "-ss {$this->startTime}"); |
||
| 63 | } |
||
| 64 | |||
| 65 | passthru("$ffmpeg ".implode($args, ' ')); |
||
|
|
|||
| 66 | } |
||
| 78 |