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:
alex-phillips 2016-05-21 11:38:16 -04:00
parent d25e899593
commit b2ffb28fd3
3 changed files with 37 additions and 6 deletions

View file

@ -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')) {

View file

@ -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, ' '));
}
}

View file

@ -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+',
]);