PHP Streaming very large video (> 4GB) problem

Viewed 119

I am trying to play a long video (4gb+) using an 8 MB chunk but it is not working. everything looks good with video until 2 GB size but getting problem for very large video size. Is anything I will need to optimize at PHP, Apache side, or anything needed to update in following script to support very large video?

<?php
require 'awss3/vendor/autoload.php';
use Aws\S3\S3Client;  
use Aws\Exception\AwsException;

$objectkey = '<VIDEO>.mp4'; 

$buffer = '';
$bucketname = '<BUCKETNAME>';
$key = '<KEY>';
$secret = '<SECRET_KEY>';
$region = '<REGION>';

$s3 = new S3Client([
    'region' => $region,
    'version' => 'latest',
    'credentials' => [
        'key' => $key,
        'secret' => $secret,
    ]
]);

$s3->registerStreamWrapper();


$context = stream_context_create(array(
    's3' => array(
        'seekable' => true
    )
));

// get the file's mime type to send the correct content type header
$path = "s3://$bucketname/$objectkey";

header('HTTP/1.0 200 OK');
header("Content-Type: video/mp4");
header('Accept-Ranges: bytes');

$options = array(
    "ssl" => array(
        "verify_peer" => false,
        "verify_peer_name" => false,
    ),
);

$context = stream_context_create($options);
$handle = fopen($path, 'rb', false, $context);

$chunkSize = 1024 * 1024 * 8;
$buffer = '';

ob_start();
while (!feof($handle)) {
    $buffer = fread($handle, $chunkSize);
    echo $buffer;
    ob_flush();
    flush();
}
fclose($handle);
exit;
1 Answers

There is no need to proxy this data through your PHP application.

Simply sign an S3 URL and let your clients load that data directly from S3. Then, there is no load at all on your server, and no waste of bandwidth.

Related