Example #1
0
 public function doPost(JsonResponse $response, LoggedIn $me, HttpFiles $files, $track_id)
 {
     $artwork_file = $files->getOrError("artwork_file");
     $temp_image = TempFileProvider::generate("", $artwork_file["name"]);
     move_uploaded_file($artwork_file["tmp_name"], $temp_image);
     $response->write(Songs::changeCover($track_id, $temp_image));
 }
Example #2
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;
 }
Example #3
0
 public function doPost(JsonResponse $response, $track_id, HttpFiles $file)
 {
     $track = $file->getOrError("file");
     $decoded_name = urldecode($track["name"]);
     $extension = pathinfo($decoded_name, PATHINFO_EXTENSION);
     $tm = new Song($track_id);
     $temp_file = TempFileProvider::generate("upload", ".{$extension}");
     error_log(print_r($track, true));
     error_log("Old Exists: " . (file_exists($track["tmp_name"]) ? 1 : 0));
     move_uploaded_file($track["tmp_name"], $temp_file);
     error_log("New Exists: " . (file_exists($temp_file) ? 1 : 0));
     $response->write($tm->upload($temp_file, $decoded_name));
 }
Example #4
0
 /**
  * Reads album cover from audio file if possible.
  *
  * @param $filename
  * @return Option
  */
 public static function readTempCovers($filename)
 {
     $escaped_filename = escapeshellarg($filename);
     $temp_cover_full = TempFileProvider::generate("cover", ".jpeg");
     $temp_cover_middle = TempFileProvider::generate("cover", ".jpeg");
     $temp_cover_small = TempFileProvider::generate("cover", ".jpeg");
     $command = sprintf(self::$settings->get("command_templates", "make_covers"), self::$settings->get("tools", "ffmpeg_cmd"), $escaped_filename, escapeshellarg($temp_cover_full), escapeshellarg($temp_cover_middle), escapeshellarg($temp_cover_small));
     exec($command, $result, $status);
     if (file_exists($temp_cover_full) && file_exists($temp_cover_middle) && file_exists($temp_cover_small)) {
         return Option::Some([$temp_cover_full, $temp_cover_middle, $temp_cover_small]);
     } else {
         return Option::None();
     }
 }
Example #5
0
 /**
  * Reads audio file record from database and generates audio preview.
  */
 public function preview()
 {
     Logger::printf("Requested preview for track %s", $this->track_id);
     if ($this->hasPreview()) {
         Logger::printf("Track preview is available (file_id is %s)", $this->track_data[TSongs::PREVIEW_ID]);
         FileServer::sendToClient($this->track_data[TSongs::PREVIEW_ID]);
         return;
     }
     Logger::printf("Track preview is unavailable");
     Logger::printf("Generating new track preview in real time");
     header("Content-Type: " . PREVIEW_MIME);
     $temp_file = TempFileProvider::generate("preview", ".mp3");
     $filename = FileServer::getFileUsingId($this->track_data[TSongs::FILE_ID]);
     $command_template = "%s -i %s -bufsize 256k -vn -ab 128k -ac 2 -acodec libmp3lame -f mp3 - | tee %s";
     $command = sprintf($command_template, $this->settings->get("tools", "ffmpeg_cmd"), escapeshellarg($filename), escapeshellarg($temp_file));
     passthru($command);
     $temp_file_id = FileServer::register($temp_file, PREVIEW_MIME);
     Logger::printf("New preview registered => %s", $temp_file_id);
     SongDao::updateSongUsingId($this->track_id, [TSongs::PREVIEW_ID => $temp_file_id]);
 }
Example #6
0
<?php

/**
 * Created by PhpStorm.
 * User: Roman
 * Date: 28.07.2015
 * Time: 9:30
 */
use app\core\cache\TempFileProvider;
use app\core\etc\Settings;
use app\core\injector\Injector;
use app\libs\WaveformGenerator;
Injector::run(function (Settings $settings) {
    WaveformGenerator::setCommand($settings->get("tools", "ffmpeg_cmd"));
    TempFileProvider::setTempPath($settings->get("fs", "temp"));
});
Example #7
0
 public static function registerByContent($file_content, $content_type)
 {
     $temp_file = TempFileProvider::generate();
     file_put_contents($temp_file, $file_content);
     return self::register($temp_file, $content_type);
 }