Last active
August 29, 2015 13:56
-
-
Save ameliaikeda/8797631 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use GetId3\GetId3Core as ID3; | |
| use Symfony\Component\HttpFoundation\File\UploadedFile; | |
| trait Analysis { | |
| // min, max | |
| protected $bitrate = 128.0; // kbps | |
| // max upload size | |
| protected $maxSize = 17000000; // 17MiB | |
| // max song length, in seconds | |
| protected $length = 480.0; // 8 minutes | |
| // allowed formats | |
| protected $formats = ["mp3"]; | |
| protected function analyze(UploadedFile $file) { | |
| $size = $file->getSize(); | |
| $status = ["error" => [], "success" = []]; | |
| $analyzer = new ID3(); | |
| $analyzer->setEncoding("UTF-8"); | |
| $result = $analyzer->analyze($file->getRealPath()); | |
| if ($result["error"]) { | |
| $status["error"][] = $result["error"]; | |
| } else { | |
| // song length in seconds (float) | |
| $length = @$result["playtime_seconds"]; | |
| // exact bitrate, in float format (kbps) | |
| $bitrate = @$result["audio"]["bitrate"]; | |
| // cbr, vbr, etc | |
| $mode = @$result["audio"]["bitrate_mode"]; | |
| // sample rate, in Hz (int) | |
| $sampleRate = @$result["audio"]["sample_rate"]; | |
| // "mp3" or "ogg" etc. | |
| $format = @$result["fileformat"]; | |
| // UTF-8, etc | |
| $encoding = @$result["encoding"]; | |
| // cover image | |
| // raw JPEG/PNG/whatever data | |
| // $coverImage = @$result["comments"]["picture"][0]["data"]; | |
| // these are generic, non-tag comments set by ID3 as an implementation-agnostic metadata dump | |
| $title = @$result["comments"]["title"][1]; // 0 is a truncated version | |
| $album = @$result["comments"]["album"]; | |
| $artist = @$result["comments"]["artist"]; | |
| if (! in_array($format, $this->formats)) | |
| $status["error"][] = "$format is not allowed."; | |
| if ($bitrate < $this->bitrate[0]) | |
| $status["error"][] = "Bitrate is too low ({$bitrate}kbps)"; | |
| if ($length > $this->length) | |
| $status["error"][] = "The song is too long (>{$this->length} seconds)"; | |
| if ($size > $this->maxSize) | |
| $status["error"][] = "Max filesize exceeded (" . $this->maxSize / 1000000 . "MiB)"; | |
| if (! count($status["error"])) | |
| $status["success"] = [ | |
| "length" => $length, | |
| "bitrate" => $bitrate, | |
| "mode" => $mode, | |
| "format" => $format, | |
| "mime" => $mime, | |
| "title" => $title, | |
| "artist" => $artist, | |
| "album" => $album, | |
| "encoding" => $encoding, | |
| ]; | |
| } | |
| return $status; | |
| } | |
| protected function addPending(UploadedFile $file) { | |
| $status = $this->analyze($file); | |
| if (count($status["error"])) { | |
| return $status; | |
| } | |
| $new = $status["success"]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment