開発環境が整っている前提で進めていきます。
仕様は次のとおりです。
仕様
- S3上にある非公開の動画を使用
- 指定のコントローラーにアクセスしたらS3から動画を表示する
ヘルパーを作成する
ヘルパーは、こちらのライブラリをベースに、少し手を加えます。
主にconstruct部分。
GitHub - rajurayhan/larastreamer: A laravel package to stream video content.
A laravel package to stream video content. Contribute to rajurayhan/larastreamer development by creating an account on GitHub.
最終的にできたコードはこちらです。
<?php
namespace App\Helpers;
class VideoStreamer
{
private $path = "";
private $stream = "";
private $buffer = 102400;
private $start = -1;
private $end = -1;
private $size = 0;
private $mime = "";
public function __construct($disk, $path)
{
$this->stream = $disk->readStream($path);
$this->mime = $disk->mimeType($path);
$this->filemtime = $disk->lastModified($path);
$this->size = $disk->size($path);
}
/**
*
*
* * Set proper header to serve the video content
*
*/
private function setHeader()
{
ob_get_clean();
$cType = "Content-Type: ".$this->mime;
header($cType);
header("Cache-Control: max-age=2592000, public");
header("Expires: ".gmdate('D, d M Y H:i:s', time()+2592000) . ' GMT');
header("Last-Modified: ".gmdate('D, d M Y H:i:s', $this->filemtime) . ' GMT');
$this->start = 0;
$this->end = $this->size - 1;
header("Accept-Ranges: 0-".$this->end);
if (isset($_SERVER['HTTP_RANGE'])) {
$c_start = $this->start;
$c_end = $this->end;
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
if (strpos($range, ',') !== false) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $this->start-$this->end/$this->size");
exit;
}
if ($range == '-') {
$c_start = $this->size - substr($range, 1);
} else {
$range = explode('-', $range);
$c_start = $range[0];
$c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $c_end;
}
$c_end = ($c_end > $this->end) ? $this->end : $c_end;
if ($c_start > $c_end || $c_start > $this->size - 1 || $c_end >= $this->size) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $this->start-$this->end/$this->size");
exit;
}
$this->start = $c_start;
$this->end = $c_end;
$length = $this->end - $this->start + 1;
fseek($this->stream, $this->start);
header('HTTP/1.1 206 Partial Content');
header("Content-Length: ".$length);
header("Content-Range: bytes $this->start-$this->end/".$this->size);
} else {
header("Content-Length: ".$this->size);
}
}
/**
* * close curretly opened stream
* */
private function end()
{
fclose($this->stream);
exit;
}
/**
* * perform the streaming of calculated range
* */
private function stream()
{
$i = $this->start;
set_time_limit(0);
while (!feof($this->stream) && $i <= $this->end) {
$bytesToRead = $this->buffer;
if (($i+$bytesToRead) > $this->end) {
$bytesToRead = $this->end - $i + 1;
}
$data = fread($this->stream, $bytesToRead);
echo $data;
flush();
$i += $bytesToRead;
}
}
/**
* * Start streaming video content
* */
public function start()
{
$this->setHeader();
$this->stream();
$this->end();
}
}
コントローラーでヘルパーを読み込む
use App\Helpers\VideoStreamer;
ヘルパーにS3の接続とファイルパスを渡す
コントローラーで次のメソッドを作成します。
public function streamVideoFile()
{
$disk = Storage::disk('s3');
$path = 's3上の動画ファイルのパス';
$stream = new VideoStreamer($disk, $path);
return response()->stream(function () use ($stream) {
$stream->start();
});
}
作成したメソッドにルーティングを設定し、ブラウザからアクセスすると動画が再生できます!

