From 40a6ac45ded8ff2f0f1fbae2f5697120efed3273 Mon Sep 17 00:00:00 2001 From: Christian Haschek Date: Sun, 12 Mar 2017 21:48:08 +0100 Subject: [PATCH] added controls and better mp4 stream --- inc/core.php | 65 +++++++++++++++++++++++++++++++++++++++++++++--- template_mp4.php | 2 +- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/inc/core.php b/inc/core.php index e4cef87..0388192 100644 --- a/inc/core.php +++ b/inc/core.php @@ -225,7 +225,7 @@ function renderImage($data) if($data['ogg']) serveFile($oggpath, $hash.'.ogg','video/ogg'); else - serveFile($mp4path, $hash.'.mp4','video/mp4'); + serveMp4($mp4path, $hash.'.mp4','video/mp4'); } else if($data['preview']) { @@ -274,7 +274,7 @@ function renderImage($data) if($data['raw']) { - serveFile($cachepath, $hash,'video/mp4'); + serveMP4($cachepath, $hash,'video/mp4'); } else if($data['preview']) { @@ -354,11 +354,13 @@ function serveFile($filename, $filename_output = false, $mime = 'application/oct header('Accept-Ranges: bytes', true); header('Content-Type: ' . $mime, true); +/* if($filename_output) { header('Content-Disposition: attachment; filename="' . $filename_output . '"'); } - +*/ +header("Content-Disposition: inline;"); // Content-Range header for byte offsets if (isset($_SERVER['HTTP_RANGE']) && preg_match('%bytes=(\d+)-(\d+)?%i', $_SERVER['HTTP_RANGE'], $match)) { @@ -404,6 +406,63 @@ function serveFile($filename, $filename_output = false, $mime = 'application/oct } } + +//via gist: https://gist.github.com/codler/3906826 +function serveMP4($path,$hash,$null) +{ + if ($fp = fopen($path, "rb")) { + $size = filesize($path); + $length = $size; + $start = 0; + $end = $size - 1; + header('Content-type: video/mp4'); + header("Accept-Ranges: 0-$length"); + if (isset($_SERVER['HTTP_RANGE'])) { + $c_start = $start; + $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 $start-$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] : $size; + } + $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 $start-$end/$size"); + exit; + } + $start = $c_start; + $end = $c_end; + $length = $end - $start + 1; + fseek($fp, $start); + header('HTTP/1.1 206 Partial Content'); + } + header("Content-Range: bytes $start-$end/$size"); + header("Content-Length: ".$length); + $buffer = 1024 * 8; + while(!feof($fp) && ($p = ftell($fp)) <= $end) { + if ($p + $buffer > $end) { + $buffer = $end - $p + 1; + } + set_time_limit(0); + echo fread($fp, $buffer); + flush(); + } + fclose($fp); + exit(); + } else { + die('file not found'); + } +} + function cidr_match($ip, $range) { list ($subnet, $bits) = explode('/', $range); diff --git a/template_mp4.php b/template_mp4.php index 5735bb0..01b10f2 100644 --- a/template_mp4.php +++ b/template_mp4.php @@ -38,7 +38,7 @@
-