1

I want to open a video/mp4 file in a php file to use it in the player. I want to limit the file upload speed to 100 kb/s.

<?php
$file = 'video.mp4';
$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();
?>

And it works fine, how to limit the speed of loading a file? And what are the optimal settings (so that the video loads smoothly, at a speed without jamming)

wojttaa
  • 99
  • 1
  • 1
  • 9
  • https://stackoverflow.com/questions/2306112/how-to-limit-file-upload-speed-in-php-or-apache – Matt Jun 07 '17 at 17:07
  • You'd have to keep track of how many bytes you have already sent, and in what time ... and them a little math to figure out how long you need to send your script to `sleep` for ... But if clients make multiple parallel requests (and by supporting byte range requests, you are practically encouraging that), you will have to find a method of detecting that as well (and that means across multiple script instances), if you don't want them to be able to get the whole video in what would essentially be a multitude of your 100kb/s limit. [...] – CBroe Jun 07 '17 at 17:08
  • 1
    [...] Long story short: You rather want to use a proper streaming server solution, than a PHP-based crutch for this ... – CBroe Jun 07 '17 at 17:08
  • Yes, So it's up to the user to wait a few minutes for the video to load or to encrypt every moment – wojttaa Jun 07 '17 at 17:13
  • For instance, someone plays a movie on the player and has to wait for it to load. And every now and then you have to wait for it to load – wojttaa Jun 07 '17 at 17:15
  • Personally, I'd run haproxy in front of it and do the throttling there. – Matt Jun 07 '17 at 17:29
  • And why not in the php script? – wojttaa Jun 07 '17 at 17:33

1 Answers1

0

I have a page as a file server, it contains video files that I want to give you the opportunity to play. To maintain the server I want to force users to buy premium accounts to speed up video encoding

wojttaa
  • 99
  • 1
  • 1
  • 9