繁体   English   中英

PHP流到html5视频,页面导航不起作用

[英]PHP Stream to html5 Video, Page Navigation not working

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

有什么想法吗?

PS:添加session_write_close(); 在流式传输文件之前解决问题。 但是对我来说似乎有点太过分了...

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

添加session_write_close(); 在流式传输文件之前解决问题。 但是对我来说似乎有点太过分了...

对此没有太多的“ hacky”。

一个长时间运行的脚本,它使一个会话保持打开状态,这将阻止以后开始使用的所有其他脚本(与单击菜单链接一样)访问同一会话。

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


什么相当“哈克”,虽然,通过在首位的脚本流式视频数据。 如果可能的话, 是您应该首先避免做的事情。 就内存使用和脚本运行时而言,这不是一个好主意。

最好使用已经存在的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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM