如何用php

时间:2017-07-12 04:08:18

标签: php amazon-s3 amazon

我试图用PHP读取mp4文件,我的初始代码是

 $file = 'https://s3-sa-east-1.amazonaws.com/onlytestes/video.mp4';
header('Content-type: video/mp4');
readfile($file);

但是这样我就无法使用视频的长度条,跳过甚至返回,直到视频100%加载。 当然,当我直接读取文件(video.mp4)时,一切都很好。 我用以下代码解决了这个问题:

$request = 'video.mp4';
$file = $request;
$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");
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);
    flush();
}
fclose($fp);
exit();

但它只适用于本地文件,我认为HTTP_RANGE不起作用,它在控制台中返回以下错误 &#39;&#39;无法加载资源:服务器响应状态为416(请求范围不满足)&#39;&#39;

我需要阅读Amazon S3视频,例如:video here

有没有人有任何想法?

2 个答案:

答案 0 :(得分:0)

您是否阅读了文档手册Amazon SDK ..?

(1)Download最新稳定版SDK

(2)解压缩.zip文件&amp;放在wamp / www文件夹中

(3)将config-sample.inc.php文件重命名为config.inc.php

(4)添加访问密钥&amp;密钥(从Amazon S3帐户检索)到      上面的文件,保存&amp;出口

(5)创建一个示例文件以显示来自Amazon S3的公共/私有对象

答案 1 :(得分:0)

谢谢Yusnur Hidayah

但我仍有问题,

通过SDK / StreamWrapper打开文件,视频启动大约需要18秒,请参阅

http://192.241.159.176/file.php

这样就无法向用户提供

通常在1或2秒内打开,请参阅

https://s3-sa-east-1.amazonaws.com/onlytestes/video.mp4

相关问题