video3.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. session_start();
  3. /**
  4. * Description of VideoStream
  5. *
  6. * @author Rana
  7. * @link http://codesamplez.com/programming/php-html5-video-streaming-tutorial
  8. */
  9. if (isset($_POST["video"])) {
  10. $filePath = $_POST["video"];
  11. } else {
  12. $filePath = "./1.mp4";
  13. }
  14. //$filePath = "./test.mp4";
  15. $stream = new VideoStream($filePath);
  16. $stream->start();
  17. class VideoStream
  18. {
  19. private $path = "";
  20. private $stream = "";
  21. private $buffer = 102400;
  22. private $start = -1;
  23. private $end = -1;
  24. private $size = 0;
  25. function __construct($filePath)
  26. {
  27. $this->path = $filePath;
  28. }
  29. /**
  30. * Open stream
  31. */
  32. private function open()
  33. {
  34. if (!($this->stream = fopen($this->path, 'rb'))) {
  35. die('Could not open stream for reading');
  36. }
  37. }
  38. /**
  39. * Set proper header to serve the video content
  40. */
  41. private function setHeader()
  42. {
  43. ob_get_clean();
  44. header("Content-Type: video/mp4");
  45. header("Cache-Control: max-age=2592000, public");
  46. header("Expires: " . gmdate('D, d M Y H:i:s', time() + 2592000) . ' GMT');
  47. header("Last-Modified: " . gmdate('D, d M Y H:i:s', @filemtime($this->path)) . ' GMT');
  48. $this->start = 0;
  49. $this->size = filesize($this->path);
  50. $this->end = $this->size - 1;
  51. header("Accept-Ranges: 0-" . $this->end);
  52. if (isset($_SERVER['HTTP_RANGE'])) {
  53. $c_start = $this->start;
  54. $c_end = $this->end;
  55. list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
  56. if (strpos($range, ',') !== false) {
  57. header('HTTP/1.1 416 Requested Range Not Satisfiable');
  58. header("Content-Range: bytes $this->start-$this->end/$this->size");
  59. exit;
  60. }
  61. if ($range == '-') {
  62. $c_start = $this->size - substr($range, 1);
  63. } else {
  64. $range = explode('-', $range);
  65. $c_start = $range[0];
  66. $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $c_end;
  67. }
  68. $c_end = ($c_end > $this->end) ? $this->end : $c_end;
  69. if ($c_start > $c_end || $c_start > $this->size - 1 || $c_end >= $this->size) {
  70. header('HTTP/1.1 416 Requested Range Not Satisfiable');
  71. header("Content-Range: bytes $this->start-$this->end/$this->size");
  72. exit;
  73. }
  74. $this->start = $c_start;
  75. $this->end = $c_end;
  76. $length = $this->end - $this->start + 1;
  77. fseek($this->stream, $this->start);
  78. header('HTTP/1.1 206 Partial Content');
  79. header("Content-Length: " . $length);
  80. header("Content-Range: bytes $this->start-$this->end/" . $this->size);
  81. } else {
  82. header("Content-Length: " . $this->size);
  83. }
  84. }
  85. /**
  86. * close curretly opened stream
  87. */
  88. private function end()
  89. {
  90. fclose($this->stream);
  91. exit;
  92. }
  93. /**
  94. * perform the streaming of calculated range
  95. */
  96. private function stream()
  97. {
  98. $i = $this->start;
  99. set_time_limit(0);
  100. while (!feof($this->stream) && $i <= $this->end) {
  101. $bytesToRead = $this->buffer;
  102. if (($i + $bytesToRead) > $this->end) {
  103. $bytesToRead = $this->end - $i + 1;
  104. }
  105. $data = fread($this->stream, $bytesToRead);
  106. echo $data;
  107. flush();
  108. $i += $bytesToRead;
  109. }
  110. }
  111. /**
  112. * Start streaming video content
  113. */
  114. function start()
  115. {
  116. $this->open();
  117. $this->setHeader();
  118. $this->stream();
  119. $this->end();
  120. }
  121. }