Advertisement
Laszlooo

Videostreamer class

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