【问题标题】:PHP Stream to html5 Video, Page Navigation not workingPHP Stream to html5 Video,页面导航不起作用
【发布时间】:2016-04-19 08:41:17
【问题描述】:

我在将 .mp4 视频从 PHP 流式传输到 HTML5 视频标签时遇到问题。视频流很好,也可以向前和向后工作。但是:如果我点击菜单中的任何链接,网站不会改变。加载图标出现在选项卡上,开发工具显示有请求。但是请求“等待”直到视频流结束。因此,如果视频结束,新页面将加载,但之前不会。

对此有何想法?

PS:添加 session_write_close();在流式传输文件解决问题之前。但是对我来说它看起来有点太 hacky...

<video style="width:100%;" preload="metadata" controls="">
 <source src="/uploads/getfile?image_path=5%2FOKzAAFlSub-VLsnFWvkPWXBLluwOV-Q5DIuqJkPpDubahlAosK.mp4&amp;type=20" type="video/mp4">
Your browser does not support the video tag.
</video>

PHP 代码:

$file = Yii::getAlias('@app') . '/../files/uploads/' .$image_path;
$fp = @fopen($file, 'rb');      
$size   = filesize($file); // File size 
$length = $size;           // Content length
$start  = 0;               // Start byte
$end    = $size - 1;       // End byte  
header('Content-type: video/mp4');
header("Accept-Ranges: 0-$length");
header("Accept-Ranges: bytes"); 
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);       
    ob_flush();    
}
fclose($fp);       
exit();

【问题讨论】:

    标签: php html video stream


    【解决方案1】:

    添加 session_write_close();在流式传输文件解决问题之前。但是对我来说它看起来有点太 hacky...

    这并没有太多“hacky”。​​

    保持会话打开的长时间运行脚本将阻止稍后启动的所有其他脚本访问同一会话(如单击菜单链接。)

    为避免这种情况,请在完成长时间运行的脚本后立即关闭会话,以便释放会话数据文件上的锁定。


    非常“hacky”,首先是通过脚本流式传输视频数据。 是你一开始就应该避免做的事情,如果可能的话。就内存使用和脚本运行时而言,这不是一个好主意。

    【讨论】:

    • 感谢您的信息。问题是,我们有内容,这些内容应该只对某些人可见,而不是对所有人可见。所以文件被保存为非公开的。目前我们的系统应该只为少数人服务——它不是一个供数千人使用的系统;)
    【解决方案2】:

    最好使用已经存在的PHP包来支持部分下载。这是梨中的一种:

    http://pear.php.net/manual/en/package.http.http-download.php

    它支持多种类型的下载(部分、流、..)。这是一个示例代码:

    $dl = &new HTTP_Download();
    $dl->setData($data);
    $dl->setLastModified($unix_timestamp);
    $dl->setContentType('application/x-gzip');
    $dl->setContentDisposition(HTTP_DOWNLOAD_ATTACHMENT, 'latest.tgz');
    $dl->send();
    

    【讨论】:

      猜你喜欢
      • 2014-05-30
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-02
      • 2015-10-19
      • 2020-07-31
      相关资源
      最近更新 更多