2016-01-28 15:19:06 +00:00
|
|
|
<?php
|
|
|
|
|
2017-04-20 11:20:32 +00:00
|
|
|
namespace App\Services\Streamers;
|
2016-01-28 15:19:06 +00:00
|
|
|
|
2018-08-22 17:59:14 +00:00
|
|
|
class TranscodingStreamer extends Streamer implements TranscodingStreamerInterface
|
2016-01-28 15:19:06 +00:00
|
|
|
{
|
2016-05-21 15:38:16 +00:00
|
|
|
/**
|
2016-08-03 10:42:11 +00:00
|
|
|
* Bit rate the stream should be transcoded at.
|
2016-05-21 15:38:16 +00:00
|
|
|
*/
|
2021-06-05 10:47:56 +00:00
|
|
|
private ?int $bitRate = null;
|
2016-05-21 15:38:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Time point to start transcoding from.
|
|
|
|
*/
|
2021-06-05 10:47:56 +00:00
|
|
|
private ?float $startTime = null;
|
2016-05-21 15:38:16 +00:00
|
|
|
|
2016-01-28 15:19:06 +00:00
|
|
|
/**
|
|
|
|
* On-the-fly stream the current song while transcoding.
|
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function stream(): void
|
2016-01-28 15:19:06 +00:00
|
|
|
{
|
2022-09-11 08:33:55 +00:00
|
|
|
setlocale(LC_CTYPE, 'en_US.UTF-8'); // #1481 special chars might be stripped otherwise
|
|
|
|
|
2017-03-26 09:02:03 +00:00
|
|
|
$ffmpeg = config('koel.streaming.ffmpeg_path');
|
2016-06-10 07:33:27 +00:00
|
|
|
abort_unless(is_executable($ffmpeg), 500, 'Transcoding requires valid ffmpeg settings.');
|
2016-01-28 15:19:06 +00:00
|
|
|
|
2016-08-03 10:42:11 +00:00
|
|
|
$bitRate = filter_var($this->bitRate, FILTER_SANITIZE_NUMBER_INT);
|
2016-01-28 15:19:06 +00:00
|
|
|
|
|
|
|
header('Content-Type: audio/mpeg');
|
2020-12-22 20:11:22 +00:00
|
|
|
header('Content-Disposition: attachment; filename="' . basename($this->song->path) . '"');
|
2016-01-28 15:19:06 +00:00
|
|
|
|
|
|
|
$args = [
|
2020-12-22 20:11:22 +00:00
|
|
|
'-i ' . escapeshellarg($this->song->path),
|
2016-01-28 15:19:06 +00:00
|
|
|
'-map 0:0',
|
|
|
|
'-v 0',
|
|
|
|
"-ab {$bitRate}k",
|
|
|
|
'-f mp3',
|
|
|
|
'-',
|
|
|
|
];
|
|
|
|
|
2016-05-21 15:38:16 +00:00
|
|
|
if ($this->startTime) {
|
|
|
|
array_unshift($args, "-ss {$this->startTime}");
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
passthru("$ffmpeg " . implode(' ', $args));
|
2016-01-28 15:19:06 +00:00
|
|
|
}
|
2018-08-22 17:59:14 +00:00
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function setBitRate(int $bitRate): void
|
2018-08-22 17:59:14 +00:00
|
|
|
{
|
|
|
|
$this->bitRate = $bitRate;
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function setStartTime(float $startTime): void
|
2018-08-22 17:59:14 +00:00
|
|
|
{
|
|
|
|
$this->startTime = $startTime;
|
|
|
|
}
|
2016-01-28 15:19:06 +00:00
|
|
|
}
|