예제 #1
0
 /**
  * @param $file_name
  * @return array
  * @throws ApplicationException
  */
 public static function generate($file_name)
 {
     set_time_limit(0);
     $temp_file = TempFileProvider::generate("peaks", ".raw");
     $command = sprintf("%s -v quiet -i %s -ac 1 -f u8 -ar 11025 -acodec pcm_u8 %s", self::$ffmpeg_cmd, escapeshellarg($file_name), escapeshellarg($temp_file));
     shell_exec($command);
     if (!file_exists($temp_file)) {
         throw new ApplicationException("Waveform could not be generated!");
     }
     $chunk_size = ceil(filesize($temp_file) / self::PEAKS_RESOLUTION);
     $peaks = withOpenedFile($temp_file, "r", function ($fh) use(&$chunk_size) {
         while ($data = fread($fh, $chunk_size)) {
             $peak = 0;
             $array = str_split($data);
             foreach ($array as $item) {
                 $code = ord($item);
                 if ($code > $peak) {
                     $peak = $code;
                 }
                 if ($code == 255) {
                     break;
                 }
             }
             (yield $peak - 127);
         }
     });
     TempFileProvider::delete($temp_file);
     return $peaks;
 }
예제 #2
0
 /**
  * @param $file
  * @throws ApplicationException
  */
 private static function write($file)
 {
     header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $file[TFiles::MTIME]) . ' GMT');
     header('Cache-Control: max-age=0');
     header('Content-Type: ' . $file[TFiles::CONTENT_TYPE]);
     if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $file[TFiles::MTIME]) {
         http_response_code(304);
         return;
     }
     $filename = FSTools::hashToFullPath($file[TFiles::SHA1]);
     if (!file_exists($filename)) {
         throw new ApplicationException("Internal server error: file not exists!");
     }
     $filesize = filesize($filename);
     if (isset($_SERVER["HTTP_RANGE"])) {
         $range = str_replace("bytes=", "", $_SERVER["HTTP_RANGE"]);
         $start = Arrays::first(explode("-", $range));
     } else {
         $start = 0;
     }
     if (isset($range)) {
         http_response_code(HttpStatusCodes::HTTP_PARTIAL_CONTENT);
         header("Content-Range: bytes " . $start . "-" . ($filesize - 1) . "/" . $filesize);
         header("Content-Length: " . ($filesize - $start));
     } else {
         http_response_code(HttpStatusCodes::HTTP_OK);
         header("Content-Length: " . $filesize);
     }
     header("Accept-Ranges: bytes");
     set_time_limit(0);
     withOpenedFile($filename, "rb", function ($fh) use($start) {
         if ($start > 0) {
             fseek($fh, $start, SEEK_SET);
         }
         while ($data = fread($fh, self::READ_BUFFER_SIZE)) {
             echo $data;
             flush();
         }
     });
 }