Created
April 22, 2015 15:26
-
-
Save bnnadi/1c56275c3b048a725ce5 to your computer and use it in GitHub Desktop.
PHP Streaming video player
This file contains 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
function watch() | |
{ | |
$file = $this->getAbsolutePath(); | |
$name = $this->getUploadedName(); | |
$mime_type = $this->getMimeType(); | |
/* | |
This function takes a path to a file to output ($file), | |
the filename that the browser will see ($name) and | |
the MIME type of the file ($mime_type, optional). | |
If you want to do something on download abort/finish, | |
register_shutdown_function('function_name'); | |
*/ | |
if(!is_readable($file)) die('File not found or inaccessible!'); | |
$size = filesize($file); | |
$name = rawurldecode($name); | |
/* Figure out the MIME type (if not specified) */ | |
$known_mime_types=array( | |
"gif" => "video/mp4", | |
"png" => "video/ogg", | |
"jpeg"=> "video/webm" | |
); | |
$file_extension = strtolower(substr(strrchr($file,"."),1)); | |
if($mime_type=='') | |
{ | |
if(array_key_exists($file_extension, $known_mime_types)) | |
{ | |
$mime_type=$known_mime_types[$file_extension]; | |
} | |
} | |
if (!($file = fopen($file, 'rb'))) { | |
die('Could not open stream for reading'); | |
} | |
$begin = 0; | |
$buffer = 102400; | |
$end = $size - 1; | |
ob_get_clean(); | |
header('Content-Type: ' . $mime_type); | |
header("Cache-Control: max-age=2592000, public"); | |
header('Accept-Ranges: 0-'.$end ); | |
if(isset($_SERVER['HTTP_RANGE'])) { | |
$c_start = $begin; | |
$c_end = $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 $begin-$end/".$size); | |
exit; | |
} | |
if($range == '-'){ | |
$c_start = $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 > $end) ? $end : $c_end; | |
if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) { | |
header('HTTP/1.1 416 Requested Range Not Satisfiable'); | |
header("Content-Range: bytes $begin-$end/".$size); | |
exit; | |
} | |
$begin = $c_start; | |
$end = $c_end; | |
$length = $end - $begin + 1; | |
fseek($file, $begin); | |
header('HTTP/1.1 206 Partial Content'); | |
header("Content-Length: ".$length); | |
header("Content-Range: bytes $begin-$end/".$size); | |
} else { | |
header("Content-Length: ".$size); | |
} | |
$i = $begin; | |
set_time_limit(0); | |
while(!feof($file)&&$i<=$end) | |
{ | |
$bytesToRead = $buffer; | |
if(($i+$bytesToRead) > $end) { | |
$bytesToRead = $end - $i + 1; | |
} | |
$data = fread($file, $bytesToRead); | |
echo $data; | |
flush(); | |
$i += $bytesToRead; | |
} | |
fclose($file); | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment