PHP获取视频部分的大小


PHP get size of portion of video

如何获得视频部分的大小?我想要一分钟视频的大小。这可能吗?

我正在制作一个php视频流脚本,我意识到你不能跳过。这就是我计划做的:如果用户跳过一分钟,我计算一分钟视频的大小。然后我回显视频,但跳过第一分钟。

这是我当前的代码:

function readfile_chunked($filename, $retbytes = TRUE) {
    $buffer = "";
    $cnt = 0;
    $handle = fopen($filename, 'rb');
    if ($handle === false) {
        return false;
    }
    while (!feof($handle)) {
        $buffer = fread($handle, CHUNK_SIZE);
        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;
}

我还有一个内容类型的头,我没有包含在上面的代码中。

谢谢你的帮助!

这不是"跳过"的方式。

你应该做的是寻找将由播放器发送的HTTP_RANGE头。

在你的PHP中,你需要添加并处理header('Accept-Ranges: bytes');标头。

所以当用户点击跳过视频时,播放器将发送一个$_SERVER['HTTP_RANGE']到服务器,然后你用它来寻找文件的输出部分。

下面是一个示例(未测试):

<?php 
...
...
//check if http_range is sent by browser (or download manager)
if(isset($_SERVER['HTTP_RANGE'])){
    list($size_unit, $range_orig) = explode('=', $_SERVER['HTTP_RANGE'], 2);
    if ($size_unit == 'bytes'){
        //multiple ranges could be specified at the same time, but for simplicity only serve the first range
        //http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt
        list($range, $extra_ranges) = explode(',', $range_orig, 2);
    }else{
        $range = '';
        header('HTTP/1.1 416 Requested Range Not Satisfiable');
        exit;
    }
}else{
    $range = '';
}
//figure out download piece from range (if set)
list($seek_start, $seek_end) = explode('-', $range, 2);
//set start and end based on range (if set), else set defaults
//also check for invalid ranges.
$seek_end   = (empty($seek_end)) ? ($file_size - 1) : min(abs(intval($seek_end)),($file_size - 1));
$seek_start = (empty($seek_start) || $seek_end < abs(intval($seek_start))) ? 0 : max(abs(intval($seek_start)),0);
//Only send partial content header if downloading a piece of the file (IE workaround)
if ($seek_start > 0 || $seek_end < ($file_size - 1)){
    header('HTTP/1.1 206 Partial Content');
    header('Content-Range: bytes '.$seek_start.'-'.$seek_end.'/'.$file_size);
    header('Content-Length: '.($seek_end - $seek_start + 1));
}else{
    header("Content-Length: $file_size");
}
header('Accept-Ranges: bytes');
...
...
?>

这里还有一个你可能会觉得有用的问题,它将更容易实现。