2
votes

I'm trying to read mp4 files with PHP, my initial code was

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

But that way I couldn't use the length bar of the video, skip or even go back, until the video is 100% loaded. Of course when I read the file directly (video.mp4) everything is fine. I solved this problem with the following code:

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

But it only works with local files, I think HTTP_RANGE does not work, it returns the following error in the console ''Failed to load resource: the server responded with a status of 416 (Requested Range Not Satisfiable)''

I need to read Amazon S3 videos, for example: video here

Does anyone have any ideas?

2

2 Answers

0
votes

Have you read Document Manual Amazon SDK..?

(1) Download latest stable version of SDK

(2) Extract the .zip file & place in wamp/www folder

(3) Rename config-sample.inc.php file to config.inc.php

(4) Add the access key & secret key (retrieved from Amazon S3 account) into above file, save & exit

(5) create a sample file to display public / private objects from Amazon S3

0
votes

thank you Yusnur Hidayah

But I still have problems,

Opening the file by the SDK / StreamWrapper, it takes about 18 seconds for the video to start, see

http://192.241.159.176/file.php

This way it is impossible to make available to users

Usually opens in 1 or 2 seconds, see

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