【问题标题】:How to open video/mp4 with php如何用 php 打开视频/mp4
【发布时间】:2017-07-12 04:08:18
【问题描述】:

我正在尝试使用 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不起作用,它在控制台中返回以下错误 ''加载资源失败:服务器响应状态为 416 (Requested Range Not Satisfiable)''

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

有人有什么想法吗?

【问题讨论】:

    标签: php amazon-s3 amazon


    【解决方案1】:

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

    (1)DownloadSDK最新稳定版

    (2) 解压 .zip 文件并放在 wamp/www 文件夹中

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

    (4) 将访问密钥和秘密密钥(从 Amazon S3 帐户检索)添加到 以上文件,保存退出

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

    【讨论】:

    • 我按照你说的做了,但是没有用。示例文件中的代码如何在 amazon s3 中显示该文件?对不起我的英语不好
    • 您是否在您的 s3 客户端注册了“streamWrapper”?
    • prnt.sc/fuob6y 是的,但是如何返回在浏览器中阅读视频?我已经尝试在 $contents = file_get_contents("s3://{$bucket}/{$key}") 中回显,但它不起作用
    • 将文件路径传递给类的构造函数,使用“s3://{bucket}/{key}”格式作为文件字符串。这一点编码 "open_stream": private function open() { // 创建一个流上下文以允许查找 $context = stream_context_create(array( 's3' => array( 'seekable' => true ) )); if (!($this->stream = fopen($this->path, 'rb', false, $context))) { die('Could not open stream for reading'); } }
    • thank you Yusnur Hidayah 但是我还是有问题,通过SDK/StreamWrapper打开文件,视频开始播放大约需要18秒,见192.241.159.176/file.php这样是无法提供的对用户通常在 1 或 2 秒内打开,见s3-sa-east-1.amazonaws.com/onlytestes/video.mp4
    【解决方案2】:

    谢谢你Yusnur Hidayah

    但我还是有问题,

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

    http://192.241.159.176/file.php

    这样就不可能提供给用户

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

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

    【讨论】:

      猜你喜欢
      • 2016-08-06
      • 2012-12-22
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多