SaschArt
SaschArt

Reputation: 11

PHP pseudo streaming

I have two problems with PHP pseudo streaming

  1. Sometimes it stuttering a bit
  2. Don't work with mp4 files

  $php_buffer=2;
  
    $bitr_min=60;
    $intv_max=0.6;

    $bitr_max=300;
    $intv_min=0.3;

    $bitr=$bitr+$php_buffer;

    if ($bitr<=$bitr_min) {
      $packet_interval=$intv_max;
    }
    elseif ($bitr>=$bitr_max) {
      $packet_interval=$intv_min;
    }
    else {
      $packet_interval=$intv_max-(($intv_max-$intv_min)*($bitr-$bitr_min)/($bitr_max-$bitr_min));
      $packet_interval=round($packet_interval*100)/100;
    }
    $packet_size=round($bitr*$packet_interval*1024);
  
  $limit_bandwidth=$bitr;

  $fsize = filesize($flv) - (($fseek > 0) ? $fseek  + 1 : 0);

  set_time_limit(30+$fsize/($limit_bandwidth*1024));


  if($no_cache) {
    # prohibit caching (different methods for different clients)
    session_cache_limiter("nocache");
    header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
    header("Pragma: no-cache");
  }

  // Flash Video Header
  if (strpos($fname,'.flv'))
    header("Content-Type: video/x-flv");
  else
    header("Content-Type: video/mp4");   ////  video/x-mp4
  header("Content-Disposition: attachment; filename=\"" . $fname . "\"");
  header("Content-Length: " . $fsize);

  $fpr = fopen($flv, 'rb');
  // Flash Video File Format Header
  if($fseek != 0) {
    echo 'FLV'.pack('C', 1).pack('C', 1).pack('N', 9).pack('N', 9);
  }

  // Seek to the file requested start
  fseek($fpr, $fseek);

  // Start the file output
  while(!feof($fpr)) {
    // Bandwidth limiting
    if($packet_interval > 0 && $limit_bandwidth) {
      // Start time
      list($us, $s) = explode(' ', microtime());
      $ts = ((float)$us + (float)$s);
      // Echo packet
      echo fread($fpr, $packet_size);
      // End time
      list($us, $s) = explode(' ', microtime());
      $tst = ((float)$us + (float)$s);
      $tdi = $tst - $ts;
      // Wait, when output is slower than packet interval
      if($tdi < (float)$packet_interval){
        usleep((float)$packet_interval * 1000000 - (float)$tdi * 1000000);
      }
    }
    else {
      // output file without bandwidth limiting
      echo fread($fpr, filesize($flv));
    }
  }

How much resources get this kind of solution? In my localhost have same results and is not apache/php overloading on the contrary take very low resources.

Please help.

Upvotes: 1

Views: 372

Answers (1)

Yassine Qoraiche
Yassine Qoraiche

Reputation: 1158

to pseudo stream any MP4 file on any browser (including iPads/tablets) with all functionality working properly (seek forward, seek backwards and displays correct time).

try MP4Streaming by tuxxin github

also works fine with Longtail JWPlayer 6 and HTML5 players.

Upvotes: 1

Related Questions