I am Using the newest Version of VideoJS and Put my Videos Through PHP in the Following way:
Called like: $this->streaming->handleDownload($videoPath, "video/mp4");
The Function is defined like:
public function handleDownload($file, $mime = "") {
if (is_file($file)) {
header("Content-Type: $mime");
if (isset($_SERVER['HTTP_RANGE'])) { // do it for any device that supports byte-ranges not only iPhone
header("X-Info: Starting Ranged Download of $file");
$this->rangeDownload($file);
} else {
header("Content-Length: ".filesize($file));
header("X-Info: Starting Straight Download of $file");
readfile($file);
}
} else {
header("HTTP/1.1 500 Internal Server Error");
$msg = "The requested path was no file!";
}
}
The rangeDownload() is defined:
private function rangeDownload($file) {
ob_end_clean();
$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: 0-$length");
if (isset($_SERVER['HTTP_RANGE'])) {
$c_start = $start;
$c_end = $end;
// Extract the range string
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
// Make sure the client hasn't sent us a multibyte range
if (strpos($range, ',') !== false) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $start-$end/$size");
exit;
}
header("X-Got-Range: $range, $range0");
// If the range starts with an '-' we start from the beginning
// If not, we forward the file pointer
// And make sure to get the end byte if spesified
if ($range0 == '-') {
$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;
}
// End bytes can not be larger than $end.
$c_end = ($c_end > $end) ? $end : $c_end;
// Validate the requested range and return an error if it's not correct.
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");
// (?) Echo some info to the client?
exit;
}
$start = $c_start;
$end = $c_end;
$length = $end - $start + 1; // Calculate new content length
fseek($fp, $start);
header('HTTP/1.1 206 Partial Content');
}
// Notify the client the byte range we'll be outputting
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: $length");
// Start buffered download
$buffer = 1024 * 8;
while(!feof($fp) && ($p = ftell($fp)) <= $end) {
if ($p + $buffer > $end) {
// In case we're only outputtin a chunk, make sure we don't
// read past the length
$buffer = $end - $p + 1;
}
set_time_limit(0); // Reset time limit for big files
echo fread($fp, $buffer);
flush(); // Free up memory. Otherwise large files will trigger PHP's memory limit.
}
fclose($fp);
}
I am Passing the Video through PHP, because the Files should be only Visible when Logged In. I do the things with the Partial Content/Ranges because I want to be able to Click in the Middle of The Position bar and The Video should start to play from there.
That all works well with Chrome, Newer Safari, iOS, but (Because of the Flash Fallback) doesn't work in IE or Firefox (Video is mp4) (Perhaps it only takes TOO long to load)
The Range Works in Chrome, but well, the Flash Version of VideoJS doesn't even Ask for the Range-Download, so it gets the other Version.
If I play the Video directly (With PHP-streaming, but Without VideoJS) in Firefox (Just call the Video-URL in the URL Bar) it Starts directly and Loads while it Plays; VideoJS doesn't
What do I need to change that the Videos starts to Play directly in VideoJS Flash-Version?
I have found the Solution (by accident ;-) )
The Problem was, that The Flash-Player asks for The Video as a Complete File. As Long as The Server doesn't answer, that there are Streaming-Versions Available (Accept-Ranges: 0-$size) the Player doesn't Ask for them.
So I set the Header and it Worked.
So All a small Tutorial for all that have Problems with VideoJS Flash Fallback:
The PHP Passthrough Script needs to send following Headers:
header("Content-Type: $videoMimeType"); header("Content-Length: $videoByteCount"); header("Accept-Ranges: 0-$videoByteCount");
If You want to handle the Range (Identified by isset($_SERVER["HTTP_RANGE"]) ) you need also to specify those headers:
header("Content-Range: bytes $startByte-$endByte/$videoByteCount");
This works for me (See Code in my Question). It works with Chrome, FF, iOS, Safari (IE not tested yet) with Flash and HTML5 with mp4-Files
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With