【问题标题】:php serve mp4 instantly (before loading everything)php 立即提供 mp4(在加载所有内容之前)
【发布时间】:2016-02-10 09:44:36
【问题描述】:

我正在实现一个提供 mp4 文件的 HTML5 视频播放器。

当我使用常规 mp4 文件作为源时,视频很快就会开始播放,我还可以查找尚未下载的部分。

现在,我想保护我的视频文件,即它们存储在 web 根目录下,并且视频 src 是一个 php 文件,用于检查是否允许用户观看视频。

所以我的 HTML 看起来像这样:

...
<video src="http://videoserver.com/stream.php?file=videoxy.mp4">
...

还有stream.php:

...
if ($access_granted) {
  if ($fd = fopen($path, "rb")) {
    $fsize = filesize($path);
    header("Content-Type: video/mp4");
    header("Content-Disposition: inline; filename=\"".$filename."\"");
    header('Content-Transfer-Encoding: binary');
    header("Content-Length: $fsize");
    fpassthru($fd);
  } else {
    die('file not found');
  }
} else {
  die('forbidden');
}
...

现在,视频仍会播放,但只有在视频完全下载之后。

如何在下载所有内容之前开始播放视频?此外,我想寻求尚未下载的部分以像以前一样工作。

【问题讨论】:

    标签: php http-headers html5-video


    【解决方案1】:

    我发现这个gist 的解决方案正在工作:

    ...
    if ($access_granted) {
      if ($fp = fopen($path, "rb")) {
        $size = filesize($path); 
        $length = $size;
        $start = 0;  
        $end = $size - 1; 
        header('Content-type: video/mp4');
        header("Accept-Ranges: 0-$length");
        if (isset($_SERVER['HTTP_RANGE'])) {
          $c_start = $start;
          $c_end = $end;
          list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
          if (strpos($range, ',') !== false) {
            header('HTTP/1.1 416 Requested Range Not Satisfiable');
            header("Content-Range: bytes $start-$end/$size");
            exit;
          }
          if ($range == '-') {
            $c_start = $size - substr($range, 1);
          } else {
            $range = explode('-', $range);
            $c_start = $range[0];
            $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
          }
          $c_end = ($c_end > $end) ? $end : $c_end;
          if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
            header('HTTP/1.1 416 Requested Range Not Satisfiable');
            header("Content-Range: bytes $start-$end/$size");
            exit;
          }
          $start = $c_start;
          $end = $c_end;
          $length = $end - $start + 1;
          fseek($fp, $start);
          header('HTTP/1.1 206 Partial Content');
        }
        header("Content-Range: bytes $start-$end/$size");
        header("Content-Length: ".$length);
        $buffer = 1024 * 8;
        while(!feof($fp) && ($p = ftell($fp)) <= $end) {
          if ($p + $buffer > $end) {
            $buffer = $end - $p + 1;
          }
          set_time_limit(0);
          echo fread($fp, $buffer);
          flush();
        }
        fclose($fp);
        exit();
      } else {
        die('file not found');
      }
    } else {
      die('forbidden');
    }
    ...
    

    【讨论】:

      猜你喜欢
      • 2020-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      相关资源
      最近更新 更多