Skip to content

Commit

Permalink
added controls and better mp4 stream
Browse files Browse the repository at this point in the history
  • Loading branch information
geek-at committed Mar 12, 2017
1 parent 5dadef9 commit 40a6ac4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
65 changes: 62 additions & 3 deletions inc/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
{
Expand Down Expand Up @@ -274,7 +274,7 @@ function renderImage($data)

if($data['raw'])
{
serveFile($cachepath, $hash,'video/mp4');
serveMP4($cachepath, $hash,'video/mp4');
}
else if($data['preview'])
{
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion template_mp4.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<body id="body">

<div id="container">
<video id="video" poster="<?php echo DOMAINPATH.PATH.'preview/'.$hash; ?>" preload="auto" autoplay="autoplay" muted="muted" loop="loop" webkit-playsinline>
<video id="video" poster="<?php echo DOMAINPATH.PATH.'preview/'.$hash; ?>" preload="auto" autoplay="autoplay" muted="muted" loop="loop" controls="controls" webkit-playsinline>
<source src="<?php echo DOMAINPATH.PATH.'raw/mp4/'.$hash; ?>" type="video/mp4">
<!--<source src="<?php echo DOMAINPATH.PATH.'raw/webm/'.$hash; ?>" type="video/webm">
<source src="<?php echo DOMAINPATH.PATH.'raw/ogg/'.$hash; ?>" type="video/ogg">-->
Expand Down

0 comments on commit 40a6ac4

Please sign in to comment.