2016-01-28 15:19:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Streamers;
|
|
|
|
|
|
|
|
use App\Models\Song;
|
|
|
|
|
2016-02-02 08:01:47 +00:00
|
|
|
class TranscodingStreamer extends Streamer implements StreamerInterface
|
2016-01-28 15:19:06 +00:00
|
|
|
{
|
2016-05-21 15:38:16 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
2016-01-28 15:19:06 +00:00
|
|
|
{
|
|
|
|
parent::__construct($song);
|
2016-05-21 15:38:16 +00:00
|
|
|
$this->bitrate = $bitrate;
|
|
|
|
$this->startTime = $startTime;
|
2016-01-28 15:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On-the-fly stream the current song while transcoding.
|
|
|
|
*/
|
|
|
|
public function stream()
|
|
|
|
{
|
|
|
|
if (!is_executable($ffmpeg = env('FFMPEG_PATH', '/usr/local/bin/ffmpeg'))) {
|
|
|
|
abort(500, 'Transcoding requires valid ffmpeg settings.');
|
|
|
|
}
|
|
|
|
|
2016-05-21 15:38:16 +00:00
|
|
|
$bitRate = filter_var($this->bitrate, FILTER_SANITIZE_NUMBER_INT);
|
2016-01-28 15:19:06 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
// Better comment these for now.
|
2016-01-31 14:00:15 +00:00
|
|
|
//
|
2016-01-28 15:19:06 +00:00
|
|
|
// header('Accept-Ranges: bytes');
|
|
|
|
// $bytes = round(($this->song->length * $bitRate * 1024) / 8);
|
|
|
|
// header("Content-Length: $bytes");
|
|
|
|
|
|
|
|
header('Content-Type: audio/mpeg');
|
|
|
|
header('Content-Disposition: attachment; filename="'.basename($this->song->path).'"');
|
|
|
|
|
|
|
|
$args = [
|
|
|
|
'-i '.escapeshellarg($this->song->path),
|
|
|
|
'-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}");
|
|
|
|
}
|
|
|
|
|
2016-01-28 15:19:06 +00:00
|
|
|
passthru("$ffmpeg ".implode($args, ' '));
|
|
|
|
}
|
|
|
|
}
|