mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
added support for determining to transcode or not via the URL as well as specifying the bitrate, also added support for starting transcode stream at specific point in time of the file
This commit is contained in:
parent
d25e899593
commit
b2ffb28fd3
3 changed files with 37 additions and 6 deletions
|
@ -18,10 +18,21 @@ class SongController extends Controller
|
|||
*
|
||||
* @param Song $song
|
||||
*/
|
||||
public function play(Song $song)
|
||||
public function play(Song $song, $transcode = null, $bitrate = null)
|
||||
{
|
||||
if (ends_with(mime_content_type($song->path), 'flac')) {
|
||||
return (new TranscodingStreamer($song))->stream();
|
||||
if (is_null($bitrate)) {
|
||||
$bitrate = env('OUTPUT_BIT_RATE', 128);
|
||||
}
|
||||
|
||||
// If transcode parameter isn't passed, the default is to only transcode flac
|
||||
if (is_null($transcode) && ends_with(mime_content_type($song->path), 'flac')) {
|
||||
$transcode = true;
|
||||
} else {
|
||||
$transcode = (bool) $transcode;
|
||||
}
|
||||
|
||||
if ($transcode) {
|
||||
return (new TranscodingStreamer($song, $bitrate, request()->input('time', 0)))->stream();
|
||||
}
|
||||
|
||||
switch (env('STREAMING_METHOD')) {
|
||||
|
|
|
@ -6,9 +6,25 @@ use App\Models\Song;
|
|||
|
||||
class TranscodingStreamer extends Streamer implements StreamerInterface
|
||||
{
|
||||
public function __construct(Song $song)
|
||||
/**
|
||||
* Bitrate which the stream should be transcoded as.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $bitrate;
|
||||
|
||||
/**
|
||||
* Time point to start transcoding from.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $startTime;
|
||||
|
||||
public function __construct(Song $song, $bitrate, $startTime = 0)
|
||||
{
|
||||
parent::__construct($song);
|
||||
$this->bitrate = $bitrate;
|
||||
$this->startTime = $startTime;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -20,7 +36,7 @@ class TranscodingStreamer extends Streamer implements StreamerInterface
|
|||
abort(500, 'Transcoding requires valid ffmpeg settings.');
|
||||
}
|
||||
|
||||
$bitRate = filter_var(env('OUTPUT_BIT_RATE', 128), FILTER_SANITIZE_NUMBER_INT);
|
||||
$bitRate = filter_var($this->bitrate, FILTER_SANITIZE_NUMBER_INT);
|
||||
|
||||
// Since we can't really know the content length of a file while it's still being transcoded,
|
||||
// "calculating" it (like below) will be much likely to result in net::ERR_CONTENT_LENGTH_MISMATCH errors.
|
||||
|
@ -42,6 +58,10 @@ class TranscodingStreamer extends Streamer implements StreamerInterface
|
|||
'-',
|
||||
];
|
||||
|
||||
if ($this->startTime) {
|
||||
array_unshift($args, "-ss {$this->startTime}");
|
||||
}
|
||||
|
||||
passthru("$ffmpeg ".implode($args, ' '));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ Route::group(['prefix' => 'api', 'namespace' => 'API'], function () {
|
|||
|
||||
Route::post('settings', 'SettingController@store');
|
||||
|
||||
Route::get('{song}/play', 'SongController@play');
|
||||
Route::get('{song}/play/{transcode?}/{bitrate?}', 'SongController@play');
|
||||
Route::post('{song}/scrobble/{timestamp}', 'ScrobbleController@store')->where([
|
||||
'timestamp' => '\d+',
|
||||
]);
|
||||
|
|
Loading…
Reference in a new issue