Reputation: 21
I am currently trying to stream video files using video.js and php. I am able to load the files directly using html , but when i try to pass the name of the video through source tag via php , i get an error saying The video could not be loaded, either because the server or network failed or because the format is not supported.
My code is as follows
<head>
<link href="css/videojs/video-js.css" rel="stylesheet">
<script src="scripts/videojs/video.js"></script>
<script>
videojs.options.flash.swf = "swf/videojs/video-js.swf";
</script>
</head>
$videofilename = $name; // name of the file Eg video.mp4
echo '<video id="player" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" width="640" height="360" poster="images/video_img.png">
<source type="video/mp4" src="uploads/video.php?'.$videofilename.'" >
<p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a></p>
</video>';
echo'<script>
videojs("player", {}, function(){
});
</script>';
video.php contains the following code
<script>
var query = location.href.split('?');
document.cookie = 'anchor=' + query[1];
</script>
<?php if (!$_COOKIE['anchor']){
echo"<script> window.location.reload() </script>";
}
else
{
$streamname = $_COOKIE['anchor'];
$stream_size = filesize($streamname);
header('Content-Description: File Transfer');
header('Content-type: video/mp4');
header("Content-length: " . filesize($streamname));
header("Expires: 0");
header("Content-Transfer-Encoding: binary");
header("Accept-Ranges: 0-$stream_size");
$file = fopen($streamname, 'r');
echo stream_get_contents($file);
fclose($file);
}
?>
Upvotes: 1
Views: 7000
Reputation: 28366
It looks like the problem is a media type mismatch. You are telling the browser that video.php
will return mp4 data:
<source type="video/mp4" src="uploads/video.php?'.$videofilename.'" >
But but the video.php
script sends back HTML starting with a script tag in the first line. If the script ever got to the headers there, you would also be getting errors in your PHP logs that you can't send headers after you have started sending data.
It appears that you are trying to get the query part of the URL using Javascript, which would not be evaluated until after it is in the client browser, and therefore its result is not available the the PHP code running on the server.
In the video.php file:
This first bit of code is HTML, not mp4, and it is sent back regardless of any query string
<script>
var query = location.href.split('?');
document.cookie = 'anchor=' + query[1];
</script>
The javascript is not evaluated by PHP, it is just sent to the browser, but since video.php is being loaded as the source of a video element, it won't be executed. The PHP block that comes next would be evaluated by the PHP interpreter server-side, but since the javascript hasn't run yet, the cookie will never be set.
I would suggest naming the parameter in the URL like so:
<source type="video/mp4" src="uploads/video.php?filename='.$videofilename.'" >
and in video.php, eliminate use $_GET
like this:
<?php
if (isset($_GET['filename']) && $_GET['filename'] != '') {
$streamname = $_GET['filename'];
$stream_size = filesize($streamname);
header('Content-Description: File Transfer');
header('Content-type: video/mp4');
header("Content-length: " . filesize($streamname));
header("Expires: 0");
header("Content-Transfer-Encoding: binary");
header("Accept-Ranges: 0-$stream_size");
$file = fopen($streamname, 'r');
echo stream_get_contents($file);
fclose($file);
}
?>
Upvotes: 1