流媒体视频:(大多数)浏览器失败

时间:2014-03-06 19:09:25

标签: php video video-streaming

我将视频存储在网络根目录之外,并根据请求流式传输。它在firefox和chrome中完美运行 - 但在IE,safari(mac)和iPad中都失败了。 IE报告Error: Unsupported video type or file path

请求是这样的:

<video width="600" height="300">
<source src="http://myserver.com/getVideo?key=<somehash>&file=video.mp4 type="video/mp4">
</video>`

然而如果我直接调用视频(没有流媒体,位置在网络根目录中),它可以在每个浏览器中工作,如预期的那样。 我认为这与我流媒体的方式有关。有什么想法吗?

public function actionGetvideo(){
    $filename = $_GET['file'];
    $filepath = $_GET['path'];

    if ($_GET['k'] != $this->gen_access_key($filename)) {
        throw new CHttpException(403, "unauthorized 1");
        exit('access key fail');
    }
    $filepath = '/home/columbin/' . $filepath . $filename;
    if (!file_exists($filepath)) {
        throw new CHttpException(404, "This video does not exist");
        exit('file does not exist');
    }

    $content_type = 'video/mp4';
    $filesize = filesize($filepath);
    $fp = fopen($filepath, 'r');
    if (!$fp) exit('no file');

    $content_length = $filesize;
    header('Content-Type: '.$content_type);
    header('Content-length: '.filesize($full_request_path));

    ob_clean();
    flush();
    $this->readfile_chunked($filepath);
}

protected function readfile_chunked($filename,$retbytes=true) {
    $chunksize = 1*(1024*1024); // how many bytes per chunk
    $buffer = '';
    $cnt =0;
    $handle = fopen($filename, 'rb');
    if ($handle === false) {
        return false;
    }
    while (!feof($handle)) {
        $buffer = fread($handle, $chunksize);
        echo $buffer;
        ob_flush();
        flush();
        if ($retbytes) {
            $cnt += strlen($buffer);
        }
    }
    $status = fclose($handle);
    if ($retbytes && $status) {
        return $cnt; // return num. bytes delivered like readfile() does.
    }
    return $status;

}

1 个答案:

答案 0 :(得分:1)

我想我找到了一个解决方案:here
我相信你的问题与标头设置不正确有关。看第二个答案。希望它有效。