3

我正在使用最新版本的 VideoJS 并通过以下方式通过 PHP 放置我的视频:

像这样调用:$this->streaming->handleDownload($videoPath, "video/mp4");

函数定义如下:

public function handleDownload($file, $mime = "") {
     if (is_file($file)) {
        header("Content-Type: $mime");

        if (isset($_SERVER['HTTP_RANGE']))  { // do it for any device that supports byte-ranges not only iPhone
            header("X-Info: Starting Ranged Download of $file");
            $this->rangeDownload($file);
        } else {
            header("Content-Length: ".filesize($file));
            header("X-Info: Starting Straight Download of $file");
            readfile($file);
        }
    } else {
        header("HTTP/1.1 500 Internal Server Error");
        $msg = "The requested path was no file!";
    }
}

rangeDownload() 定义:

private function rangeDownload($file) {
    ob_end_clean();
    $fp = @fopen($file, 'rb');

    $size   = filesize($file); // File size
    $length = $size;           // Content length
    $start  = 0;               // Start byte
    $end    = $size - 1;       // End byte

    header("Accept-Ranges: 0-$length");
    if (isset($_SERVER['HTTP_RANGE'])) {

        $c_start = $start;
        $c_end   = $end;
        // Extract the range string
        list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
        // Make sure the client hasn't sent us a multibyte range
        if (strpos($range, ',') !== false) {
            header('HTTP/1.1 416 Requested Range Not Satisfiable');
            header("Content-Range: bytes $start-$end/$size");
            exit;
        }
        header("X-Got-Range: $range, $range0");
        // If the range starts with an '-' we start from the beginning
        // If not, we forward the file pointer
        // And make sure to get the end byte if spesified
        if ($range0 == '-') {
            $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;
        }

        // End bytes can not be larger than $end.
        $c_end = ($c_end > $end) ? $end : $c_end;
        // Validate the requested range and return an error if it's not correct.
        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");
            // (?) Echo some info to the client?
            exit;
        }
        $start  = $c_start;
        $end    = $c_end;
        $length = $end - $start + 1; // Calculate new content length
        fseek($fp, $start);
        header('HTTP/1.1 206 Partial Content');
    }

    // Notify the client the byte range we'll be outputting
    header("Content-Range: bytes $start-$end/$size");
    header("Content-Length: $length");

    // Start buffered download
    $buffer = 1024 * 8;
    while(!feof($fp) && ($p = ftell($fp)) <= $end) {

        if ($p + $buffer > $end) {

            // In case we're only outputtin a chunk, make sure we don't
            // read past the length
            $buffer = $end - $p + 1;
        }
        set_time_limit(0); // Reset time limit for big files
        echo fread($fp, $buffer);
        flush(); // Free up memory. Otherwise large files will trigger PHP's memory limit.
    }

    fclose($fp);
}

我通过 PHP 传递视频,因为文件应该只在登录时可见。我使用部分内容/范围做这些事情,因为我希望能够在位置栏的中间单击,并且视频应该从那里开始播放。

这一切都适用于 Chrome、较新的 Safari、iOS,但(因为 Flash 后备)在 IE 或 Firefox 中不起作用(视频是 mp4)(也许加载时间太长)

Range 可以在 Chrome 中运行,但是 VideoJS 的 Flash 版本甚至不要求 Range-Download,因此它获得了另一个版本。

如果我在 Firefox 中直接播放视频(使用 PHP 流,但没有 VideoJS)(只需在 URL 栏中调用 Video-URL),它会直接启动并在播放时加载;VideoJS 没有

我需要更改哪些视频开始直接在 VideoJS Flash-Version 中播放?

4

1 回答 1

3

我找到了解决方案(偶然;-))

问题是,Flash 播放器要求将视频作为完整文件。只要服务器不回答,有可用的流版本(接受范围:0-$ 大小),播放器就不会要求它们。

所以我设置了标题并且它起作用了。

所以所有有 VideoJS Flash Fallback 问题的所有小教程:

  • 视频需要通过 MP4Box 编码(参见Video.js 中的 MP4 直到完全加载后才播放
  • PHP Passthrough 脚本需要发送以下标头:

    header("内容类型:$videoMimeType"); header("内容长度:$videoByteCount"); header("接受范围:0-$videoByteCount");

  • 如果您想处理范围(由 标识isset($_SERVER["HTTP_RANGE"])),您还需要指定这些标头:

    header("内容范围:字节 $startByte-$endByte/$videoByteCount");

这对我有用(请参阅我的问题中的代码)。它适用于带有 Flash 的 Chrome、FF、iOS、Safari(IE 尚未测试)和带有 mp4 文件的 HTML5

于 2013-07-19T06:10:48.853 回答