php serve mp4 instantly (before loading everything) - php

I'm implementing a HTML5 video player that serves mp4-files.
When I use a regular mp4 file as a source, the video starts pretty soon and I can also seek to parts that aren't downloaded yet.
Now, I want to secure my video files, i.e. they are stored below the web root and the video src is a php-file, that checks, if the user is allowed to view the video.
So my HTML looks like this:
...
<video src="http://videoserver.com/stream.php?file=videoxy.mp4">
...
And the stream.php:
...
if ($access_granted) {
if ($fd = fopen($path, "rb")) {
$fsize = filesize($path);
header("Content-Type: video/mp4");
header("Content-Disposition: inline; filename=\"".$filename."\"");
header('Content-Transfer-Encoding: binary');
header("Content-Length: $fsize");
fpassthru($fd);
} else {
die('file not found');
}
} else {
die('forbidden');
}
...
Now, the video will still be played, but only after the video is completely downloaded.
How can I make the video start before everything is downloaded? Also I'd like seeking to not yet downloaded parts to work as before.

I found the solution from this gist to be working:
...
if ($access_granted) {
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');
}
} else {
die('forbidden');
}
...

Related

PHP Video only plays if on local network. (mp4, mobile, desktop)

Having problem with mp4 only playing on local network. If I try by tuning the wifi access off on my cell phone, it will not work. If I try outside on local network it will not work. The browser seems to lockup, I then get could not connect if I refresh (without trying to play video). It will eventually access the site again.
here is the code...
header("Content-Type: video/mp4");
$file = $_SERVER['DOCUMENT_ROOT'].$thisFile;
$fp = #fopen($file, 'rb');
$size = filesize($file); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
//
header("Accept-Ranges: bytes");
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) {
$buffer = $end - $p + 1;
}
set_time_limit(0);
echo fread($fp, $buffer);
flush();
}
// readfile($file);
fclose($fp);
exit();

Stream mp4 video in php file with speed limitation

I want to open a video/mp4 file in a php file to use it in the player. I want to limit the file upload speed to 100 kb/s.
<?php
$file = 'video.mp4';
$fp = #fopen($file, 'rb');
$size = filesize($file); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
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();
?>
And it works fine, how to limit the speed of loading a file? And what are the optimal settings (so that the video loads smoothly, at a speed without jamming)
I have a page as a file server, it contains video files that I want to give you the opportunity to play. To maintain the server I want to force users to buy premium accounts to speed up video encoding

HTML5 video stream only to player

I am trying to make my videos on website harder to download, but failed with one point. Solution works good if your goal is to kill video link after 15 mins (enough for user to see video).
But also i want let player get this streaming into HTML5 video player, but don't let user to download it by putting video source link in new tab.
I tried to made this link one-time-working, but the problem is that HTML5 player connects to streaming script more than one time.
So probably there is any solution how in my streaming script i can check if script forced to open directly or forced by HTML5 player? Or maybe there is another way how to block direct opening of this script?
Streaming script:
<?php
[...]
if (!empty($_GET['id']) && !empty($_GET['token'])){
if (strtotime($array['created']) > strtotime('-15 minutes')) {
$file = 'Z:/home/localhost/www/mvc/video/' . $_GET['id'];
$fp = #fopen($file, 'rb');
$size = filesize($file); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
header('Content-type: video/mp4');
//header("Accept-Ranges: 0-$length");
header("Accept-Ranges: bytes");
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 * 32;
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 {
echo 'Token is closed';
}
}
else {
echo 'Denied';
}
Player looks like this:
<video width="640" height="480" preload controls>
<source src="http://localhost/mvc/video/video.php?id=video.mp4&token=0c9eb340fa59db2accf61b16663c79b1" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
Well after two days of cruel fighting i finally found the decision.
To achieve this goal (let HTML5 video player stream video, but block direct download with link), i changed this:
if (!empty($_GET['id']) && !empty($_GET['token'])){
if (strtotime($array['created']) > strtotime('-15 minutes'))
{
stream video...
}
to this:
if (!empty($_GET['id']) && !empty($_GET['token'])){
if (strtotime($array['created']) > strtotime('-15 minutes') && $_SERVER['HTTP_RANGE'] == true) {
stream...
}
Sure this is not 100% guarantee to protect your video, but now it is harder to download.

Stream Video Url though php

I have created a simple php code to stream a video file over to PHP server and then to the browser.
Video plays fine but I am unable to scroll through video.
How can I pass on the range from php to curl properly.
<?php
header("Content-Type: video/mp4");
header('Accept-Ranges: bytes');
$downloadLink = "https://vjs.zencdn.net/v/oceans.mp4";
$size = '23014356'; // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
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);
$opts = array(
'http'=>array(
'method'=>"GET",
"cache-control: no-cache",
"Content-Range: bytes $start-$end/$size"
)
);
$context = stream_context_create($opts);
$fp = fopen("$downloadLink", 'r', false, $context);
fpassthru($fp);
fclose($fp);

PHP mp4 stream to browser

I am trying to stream a simple mp4 video to a video.js player using php.
I found here a simple function to use and modified a bit but the video player is issuing an error even when i go directly to the link i get an error that media is not ok or plugin failed to load (when using the direct link on safari)
Here is the code snippet:
$token = $_GET['token'];
if ($token == 1 )
{
$path = '../videos/vid1.mp4';
$size=filesize($path);
$fm=#fopen($path,'rb');
if(!$fm) {
// You can also redirect here
header ("HTTP/1.0 404 Not Found");
die();
}
$begin=0;
$end=$size;
if(isset($_SERVER['HTTP_RANGE'])) {
if(preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) {
$begin=intval($matches[0]);
if(!empty($matches[1])) {
$end=intval($matches[1]);
}
}
}
if($begin>0||$end<$size)
header('HTTP/1.0 206 Partial Content');
else
header('HTTP/1.0 200 OK');
header("Content-Type: video/mp4");
header('Accept-Ranges: bytes');
header('Content-Length:'.($end-$begin));
header("Content-Disposition: inline;");
header("Content-Range: bytes $begin-$end/$size");
header("Content-Transfer-Encoding: binary\n");
header('Connection: close');
$cur=$begin;
fseek($fm,$begin,0);
while(!feof($fm)&&$cur<$end&&(connection_status()==0))
{ print fread($fm,min(1024*16,$end-$cur));
$cur+=1024*16;
usleep(1000);
}
die();
}
Ignore token as that is for testing purposes only. Is there something wrong with my code and is there any other practical way to stream mp4 or other media types to the browser?
Regards
Solved using this also found here... Sorry for posting but to who will be redirected here can have options.
Working code snippet:
$file = '../videos/vid1.mp4';
$fp = #fopen($file, 'rb');
$size = filesize($file); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
header('Content-type: video/mp4');
//header("Accept-Ranges: 0-$length");
header("Accept-Ranges: bytes");
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();
here's a simple solution:
if (access_token_valid()) {
$file = $_GET['file'];
if (file_exists($file)) {
header('Location:
'.$file);
} else {
header('Location:
404.mp4');
}
} else {
header('Location:
403.mp4');
}

Categories