Created
February 12, 2016 18:31
-
-
Save luisfredgs/d09fd08fea9eedda732e to your computer and use it in GitHub Desktop.
Pseudo Stream of MP4 files on Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::get('mp4-stream/{filename}', function($filename){ | |
$contentType = "video/mp4"; | |
$path = $filename; | |
$fullsize = filesize($path); | |
$size = $fullsize; | |
$stream = fopen($path, "r"); | |
$response_code = 200; | |
$headers = array("Content-type" => $contentType); | |
// Check for request for part of the stream | |
$range = Request::header('Range'); | |
if($range != null) { | |
$eqPos = strpos($range, "="); | |
$toPos = strpos($range, "-"); | |
$unit = substr($range, 0, $eqPos); | |
$start = intval(substr($range, $eqPos+1, $toPos)); | |
$success = fseek($stream, $start); | |
if($success == 0) { | |
$size = $fullsize - $start; | |
$response_code = 206; | |
$headers["Accept-Ranges"] = $unit; | |
$headers["Content-Range"] = $unit . " " . $start . "-" . ($fullsize-1) . "/" . $fullsize; | |
} | |
} | |
$headers["Content-Length"] = $size; | |
return Response::stream(function () use ($stream) { | |
fpassthru($stream); | |
}, $response_code, $headers); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment