MP4 Streaming PHP Script Help!

VideoLeopard

Newbie
Joined
Sep 25, 2012
Messages
41
Reaction score
20
Hello, I found this really nice MP4 Streaming PHP Script online and I just need some help so i can grab videos from remote url's instead of inside the server. For example, right now it only grabs from mydomain.com/video.mp4 but i would like to make it grab the video from differentdomain.com/video.mp4

How do i do this? I don't mind sending you $5 if you have a fix for this. Here's the script:




  1. <?php
  2. $file = 'video360p.mp4';
  3. $fp = @fopen($file, 'rb');
  4. $size = filesize($file); // File size
  5. $length = $size; // Content length
  6. $start = 0; // Start byte
  7. $end = $size - 1; // End byte
  8. header('Content-type: video/mp4');
  9. //header("Accept-Ranges: 0-$length");
  10. header("Accept-Ranges: bytes");
  11. if (isset($_SERVER['HTTP_RANGE'])) {
  12. $c_start = $start;
  13. $c_end = $end;
  14. list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
  15. if (strpos($range, ',') !== false) {
  16. header('HTTP/1.1 416 Requested Range Not Satisfiable');
  17. header("Content-Range: bytes $start-$end/$size");
  18. exit;
  19. }
  20. if ($range == '-') {
  21. $c_start = $size - substr($range, 1);
  22. }else{
  23. $range = explode('-', $range);
  24. $c_start = $range[0];
  25. $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
  26. }
  27. $c_end = ($c_end > $end) ? $end : $c_end;
  28. if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
  29. header('HTTP/1.1 416 Requested Range Not Satisfiable');
  30. header("Content-Range: bytes $start-$end/$size");
  31. exit;
  32. }
  33. $start = $c_start;
  34. $end = $c_end;
  35. $length = $end - $start + 1;
  36. fseek($fp, $start);
  37. header('HTTP/1.1 206 Partial Content');
  38. }
  39. header("Content-Range: bytes $start-$end/$size");
  40. header("Content-Length: ".$length);
  41. $buffer = 1024 * 8;
  42. while(!feof($fp) && ($p = ftell($fp)) <= $end) {
  43. if ($p + $buffer > $end) {
  44. $buffer = $end - $p + 1;
  45. }
  46. set_time_limit(0);
  47. echo fread($fp, $buffer);
  48. flush();
  49. }
  50. fclose($fp);
  51. exit();
  52. ?>
 
Last edited:
If from a remote server you ha
ve 2 choices.

1.) Have the browser pull it directly from the remote stream.

2.) Pull the file locally (on request) this can be done using
Code:
file_get_contents
or
Code:
curl
in PHP.

3.) Manually save them locally and refer to that rather than pulling them on demand each time.

2/3 the remote server will see you pulling these files, i'd use 1 and if they are allowing it stream it from their url.

Example of HTML5 implementation

Code:
<video controls>
 <source src="http://media.w3.org/2010/05/sintel/trailer.mp4"
        type='video/mp4; codecs="avc1, mp4a"'>
 <source src="http://media.w3.org/2010/05/sintel/trailer.ogv"
        type='video/ogg; codecs="theora, vorbis"'>
 <p>Your user agent does not support the HTML5 Video element.</p>
</video>


if you wanted to show the videos from a different domain you could also use CNAME depending on the configuration they have at their end.
 
Back
Top