7

I am using the video tag to play videos.

I using php files for the playback like this:

    <video id="playvideo" preload="auto" width="845" height="395" 
    poster="http://video-js.zencoder.com/oceans-clip.png">

    <source src="../getvideo_webm.php" type='video/webm' />
    <source src="../getvideo_mp4.php" type='video/mp4'/>
    <source src="../getvideo_ogv.php" type='video/ogg' />

    </video>

All .php files are playing fine when i check them directly in the brower. But the above setup with all .php as source files will not play. If i give a straight .mp4 source it will play fine.

The getvideo_mp4.php looks like this:

 $path = 'oceans-clip.mp4';
 if (file_exists($path))
 {
 $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();
 }

So what am i doing wrong ?

Niels
  • 635
  • 3
  • 9
  • 23

3 Answers3

3

The above code is working. After i changed the src url for .php files, it did actually work. Now it plays in moz, ie, chrome with only php files as sources in the video tag.

Niels
  • 635
  • 3
  • 9
  • 23
  • Could you please explain what you changed? I have the same problem but the code above did not work – ChronosMOT Sep 08 '15 at 14:10
  • 1
    Does your file have ANY other code or is the code above the ONLY code in your php file ? – Niels Sep 09 '15 at 19:15
  • There is a lot of other code, I'm decrypting the file and I'm getting all encrypted resources via this php script (but I'm using the code above only for mp4s). However since yesterday I got it to run on IE11 and Firefox 40.0.3. Chrome still won't play the file though. Also by now I'm using a modified version of this code: http://www.pixelstech.net/article/1357732373-Output-a-file-with-HTTP-range-header-in-PHP which is very close :) to your code but uses slightly different headers. – ChronosMOT Sep 10 '15 at 07:54
  • Ok - So your code is at least not "broken" by other scripts. Get the direct link to the mp4 .. Look at the headers in http://web-sniffer.net/ - compare with headers you get from the php file.. then try to adapt the headers - to see if that works – Niels Sep 14 '15 at 11:52
0

You will have to echo the path after retrieving it and pass it to the 'src' attribute of the Video Tag of HTML5. Your Current Strategy won't work well, i hope...

For example,

<source src="<?php echo getMp4VideoUrl(); ?>" type='video/mp4'/>
Vijay Sarin
  • 1,326
  • 1
  • 11
  • 31
  • Hi Vijay.. It seems like you "just" want to serve the direct src by php. I want to hide the actual media file location, and load them dynamic from the getvideo_xx php files. – Niels May 24 '13 at 10:23
  • If you does that also, users can easily download your videos just by right clicking on the videos and selecting the 'Copy Video Location' option from the Video Tag Context Menu. I already have tried this work around but failed. Try this link, http://stackoverflow.com/questions/9756837/prevent-html5-video-from-being-downloaded-right-click-saved – Vijay Sarin May 24 '13 at 10:26
  • 1
    Hi Vijay. It though now seems like the above code is working.. I seems like a pretty unlucky steps of trying different ways.. The rightclick is disabled and it is served in a modalwindow.. I do know it is not safe for this file. I just don't want the user to know the excact location of the files.. – Niels May 24 '13 at 10:32
0

The browser identifies the video content from the header sent to it with the request. Just manipulate the header and keep the PHP extension. It will work perfect

Arpit Gaur
  • 263
  • 2
  • 9