流视频网址虽然PHP

时间:2016-04-03 02:47:22

标签: php video video-streaming

我创建了一个简单的PHP代码,以流式 视频文件到PHP服务器,然后再到浏览器。

视频播放正常,但我无法滚动视频。

如何正确地将范围从php传递到卷曲。

    <?php
    header("Content-Type: video/mp4");
    header('Accept-Ranges: bytes');

$downloadLink = "https://vjs.zencdn.net/v/oceans.mp4";

$size   = '23014356'; // File size
$length = $size;           // Content length
$start  = 0;               // Start byte
$end    = $size - 1;       // End byte

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);

$opts = array(
  'http'=>array(
    'method'=>"GET",
         "cache-control: no-cache",
         "Content-Range: bytes $start-$end/$size"

  )
);



$context = stream_context_create($opts);


$fp = fopen("$downloadLink", 'r', false, $context);
fpassthru($fp);
fclose($fp);

0 个答案:

没有答案