Использование php для вывода видео в формате mp4 - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.

Name: Использование php для вывода видео в формате mp4 fullscreencopydownloadembedprint


Your result can be seen below.

Result of php executing





Full code of Использование php для вывода видео в формате mp4.php

  1. <?php
  2. $file = 'path/to/videofile.mp4';
  3. $fp = @fopen($file, 'rb');
  4.  
  5. $size   = filesize($file); // File size
  6. $length = $size;           // Content length
  7. $start  = 0;               // Start byte
  8. $end    = $size - 1;       // End byte
  9.  
  10. header('Content-type: video/mp4');
  11. header("Accept-Ranges: 0-$length");
  12. if (isset($_SERVER['HTTP_RANGE'])) {
  13.  
  14.     $c_start = $start;
  15.     $c_end   = $end;
  16.  
  17.     list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
  18.     if (strpos($range, ',') !== false) {
  19.         header('HTTP/1.1 416 Requested Range Not Satisfiable');
  20.         header("Content-Range: bytes $start-$end/$size");
  21.         exit;
  22.     }
  23.     if ($range == '-') {
  24.         $c_start = $size - substr($range, 1);
  25.     }else{
  26.         $range  = explode('-', $range);
  27.         $c_start = $range[0];
  28.         $c_end   = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
  29.     }
  30.     $c_end = ($c_end > $end) ? $end : $c_end;
  31.     if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
  32.         header('HTTP/1.1 416 Requested Range Not Satisfiable');
  33.         header("Content-Range: bytes $start-$end/$size");
  34.         exit;
  35.     }
  36.     $start  = $c_start;
  37.     $end    = $c_end;
  38.     $length = $end - $start + 1;
  39.     fseek($fp, $start);
  40.     header('HTTP/1.1 206 Partial Content');
  41. }
  42. header("Content-Range: bytes $start-$end/$size");
  43. header("Content-Length: ".$length);
  44.  
  45.  
  46. $buffer = 1024 * 8;
  47. while(!feof($fp) && ($p = ftell($fp)) <= $end) {
  48.  
  49.     if ($p + $buffer > $end) {
  50.         $buffer = $end - $p + 1;
  51.     }
  52.     set_time_limit(0);
  53.     echo fread($fp, $buffer);
  54.     flush();
  55. }
  56.  
  57. fclose($fp);
  58. exit();
  59. ?>
File Description
  • Использование php для вывода видео в формате mp4
  • PHP Code
  • 17 Jan-2021
  • 1.57 Kb
You can Share it: