【问题标题】:Streaming Video: Failure on (most) browsers流媒体视频:(大多数)浏览器失败
【发布时间】:2014-03-06 19:09:25
【问题描述】:

我将我的视频存储在 Web 根目录之外,并根据要求进行流式传输。它在 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>`

但是如果我直接调用视频(没有流式传输,并且位置位于 Web 根目录中),它可以在每个浏览器中正常运行,如预期的那样。 我相信这与我流媒体的方式有关。有什么想法吗?

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;

}

【问题讨论】:

  • 您可能只是有一个并非所有浏览器都支持的编解码器。当您插入 URL 时,您并不总是使用浏览器的内置播放功能。您经常使用 HTML5 不提供的插件。
  • 我不这么认为 - 只有在尝试流式传输视频时才会出现此问题。如果我将视频 src 更改为 webroot 中的视频,它可以正常播放。
  • 那还在流式传输......你为什么不使用readfile(),或者把它交给你的网络服务器?
  • 你能说得更具体点吗?
  • readfile() 是一个从磁盘读取文件并输出到缓冲区的函数。不过,最好的办法是让服务器使用 sendfile 或同等功能为您处理此问题。 wiki.nginx.org/HttpCoreModule#sendfile

标签: php video video-streaming


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多