/** * Execute the job. * * @return void */ public function handle() { // Sanity-check: was this file just generated, or is it already being processed? if ($this->trackFile->status === TrackFile::STATUS_PROCESSING) { Log::warning('Track file #' . $this->trackFile->id . ' (track #' . $this->trackFile->track_id . ') is already being processed!'); return; } elseif (!$this->trackFile->is_expired) { Log::warning('Track file #' . $this->trackFile->id . ' (track #' . $this->trackFile->track_id . ') is still valid! No need to re-encode it.'); return; } // Start the job $this->trackFile->status = TrackFile::STATUS_PROCESSING; $this->trackFile->save(); // Use the track's master file as the source if ($this->isForUpload) { $source = $this->trackFile->track->getTemporarySourceFile(); } else { $source = TrackFile::where('track_id', $this->trackFile->track_id)->where('is_master', true)->first()->getFile(); } // Assign the target $this->trackFile->track->ensureDirectoryExists(); $target = $this->trackFile->getFile(); // Prepare the command $format = Track::$Formats[$this->trackFile->format]; $command = $format['command']; $command = str_replace('{$source}', '"' . $source . '"', $command); $command = str_replace('{$target}', '"' . $target . '"', $command); Log::info('Encoding track file ' . $this->trackFile->id . ' into ' . $target); // Start a synchronous process to encode the file $process = new Process($command); try { $process->mustRun(); } catch (ProcessFailedException $e) { Log::error('An exception occured in the encoding process for track file ' . $this->trackFile->id . ' - ' . $e->getMessage()); Log::info($process->getOutput()); // Ensure queue fails throw $e; } // Update the tags of the track $this->trackFile->track->updateTags($this->trackFile->format); // Insert the expiration time for cached tracks if ($this->isExpirable && $this->trackFile->is_cacheable) { $this->trackFile->expires_at = Carbon::now()->addMinutes(Config::get('ponyfm.track_file_cache_duration')); $this->trackFile->save(); } // Update file size $this->trackFile->updateFilesize(); // Complete the job $this->trackFile->status = TrackFile::STATUS_NOT_BEING_PROCESSED; $this->trackFile->save(); if ($this->isForUpload) { if (!$this->trackFile->is_master && $this->trackFile->is_cacheable) { File::delete($this->trackFile->getFile()); } // This was the final TrackFile for this track! if ($this->trackFile->track->status === Track::STATUS_COMPLETE) { if ($this->autoPublishWhenComplete) { $this->trackFile->track->published_at = Carbon::now(); DB::table('tracks')->whereUserId($this->trackFile->track->user_id)->update(['is_latest' => false]); $this->trackFile->track->is_latest = true; $this->trackFile->track->save(); } File::delete($this->trackFile->track->getTemporarySourceFile()); } } }
private function updateTagsForTrackFile(TrackFile $trackFile) { $trackFile->touch(); if (\File::exists($trackFile->getFile())) { $format = $trackFile->format; $data = self::$Formats[$format]; $this->{$data['tag_method']}($format); } }
/** * @throws \Exception * @return CommandResponse */ public function execute() { $user = \Auth::user(); $trackFile = \Input::file('track', null); if (null === $trackFile) { return CommandResponse::fail(['track' => ['You must upload an audio file!']]); } $audio = \AudioCache::get($trackFile->getPathname()); list($parsedTags, $rawTags) = $this->parseOriginalTags($trackFile, $user, $audio->getAudioCodec()); $track = new Track(); $track->user_id = $user->id; $track->title = Input::get('title', $parsedTags['title']); $track->duration = $audio->getDuration(); $track->save(); $track->ensureDirectoryExists(); if (!is_dir(Config::get('ponyfm.files_directory') . '/queued-tracks')) { mkdir(Config::get('ponyfm.files_directory') . '/queued-tracks', 0755, true); } $trackFile = $trackFile->move(Config::get('ponyfm.files_directory') . '/queued-tracks', $track->id); $input = Input::all(); $input['track'] = $trackFile; $validator = \Validator::make($input, ['track' => 'required|' . ($this->_allowLossy ? '' : 'audio_format:' . implode(',', $this->_losslessFormats) . '|') . ($this->_allowShortTrack ? '' : 'min_duration:30|') . 'audio_channels:1,2', 'auto_publish' => 'boolean', 'title' => 'string', 'track_type_id' => 'exists:track_types,id', 'genre' => 'string', 'album' => 'string', 'track_number' => 'integer', 'released_at' => 'date_format:' . Carbon::ISO8601, 'description' => 'string', 'lyrics' => 'string', 'is_vocal' => 'boolean', 'is_explicit' => 'boolean', 'is_downloadable' => 'boolean', 'is_listed' => 'boolean', 'cover' => 'image|mimes:png,jpeg|min_width:350|min_height:350', 'metadata' => 'json']); if ($validator->fails()) { $track->delete(); return CommandResponse::fail($validator); } // Process optional track fields $autoPublish = (bool) ($input['auto_publish'] ?? $this->_autoPublishByDefault); if (Input::hasFile('cover')) { $track->cover_id = Image::upload(Input::file('cover'), $track->user_id)->id; } else { $track->cover_id = $parsedTags['cover_id']; } $track->title = $input['title'] ?? $parsedTags['title'] ?? $track->title; $track->track_type_id = $input['track_type_id'] ?? TrackType::UNCLASSIFIED_TRACK; $track->genre_id = isset($input['genre']) ? $this->getGenreId($input['genre']) : $parsedTags['genre_id']; $track->album_id = isset($input['album']) ? $this->getAlbumId($user->id, $input['album']) : $parsedTags['album_id']; if ($track->album_id === null) { $track->track_number = null; } else { $track->track_number = $input['track_number'] ?? $parsedTags['track_number']; } $track->released_at = isset($input['released_at']) ? Carbon::createFromFormat(Carbon::ISO8601, $input['released_at']) : $parsedTags['release_date']; $track->description = $input['description'] ?? $parsedTags['comments']; $track->lyrics = $input['lyrics'] ?? $parsedTags['lyrics']; $track->is_vocal = $input['is_vocal'] ?? $parsedTags['is_vocal']; $track->is_explicit = $input['is_explicit'] ?? false; $track->is_downloadable = $input['is_downloadable'] ?? true; $track->is_listed = $input['is_listed'] ?? true; $track->source = $this->_customTrackSource ?? 'direct_upload'; // If json_decode() isn't called here, Laravel will surround the JSON // string with quotes when storing it in the database, which breaks things. $track->metadata = json_decode(Input::get('metadata', null)); $track->original_tags = ['parsed_tags' => $parsedTags, 'raw_tags' => $rawTags]; $track->save(); try { $source = $trackFile->getPathname(); // Lossy uploads need to be identified and set as the master file // without being re-encoded. $audioObject = AudioCache::get($source); $isLossyUpload = !in_array($audioObject->getAudioCodec(), $this->_losslessFormats); if ($isLossyUpload) { if ($audioObject->getAudioCodec() === 'mp3') { $masterFormat = 'MP3'; } else { if (Str::startsWith($audioObject->getAudioCodec(), 'aac')) { $masterFormat = 'AAC'; } else { if ($audioObject->getAudioCodec() === 'vorbis') { $masterFormat = 'OGG Vorbis'; } else { $validator->messages()->add('track', 'The track does not contain audio in a known lossy format.'); $track->delete(); return CommandResponse::fail($validator); } } } $trackFile = new TrackFile(); $trackFile->is_master = true; $trackFile->format = $masterFormat; $trackFile->track_id = $track->id; $trackFile->save(); // Lossy masters are copied into the datastore - no re-encoding involved. File::copy($source, $trackFile->getFile()); } $trackFiles = []; foreach (Track::$Formats as $name => $format) { // Don't bother with lossless transcodes of lossy uploads, and // don't re-encode the lossy master. if ($isLossyUpload && ($format['is_lossless'] || $name === $masterFormat)) { continue; } $trackFile = new TrackFile(); $trackFile->is_master = $name === 'FLAC' ? true : false; $trackFile->format = $name; $trackFile->status = TrackFile::STATUS_PROCESSING_PENDING; if (in_array($name, Track::$CacheableFormats) && $trackFile->is_master == false) { $trackFile->is_cacheable = true; } else { $trackFile->is_cacheable = false; } $track->trackFiles()->save($trackFile); // All TrackFile records we need are synchronously created // before kicking off the encode jobs in order to avoid a race // condition with the "temporary" source file getting deleted. $trackFiles[] = $trackFile; } try { foreach ($trackFiles as $trackFile) { $this->dispatch(new EncodeTrackFile($trackFile, false, true, $autoPublish)); } } catch (InvalidEncodeOptionsException $e) { $track->delete(); return CommandResponse::fail(['track' => [$e->getMessage()]]); } } catch (\Exception $e) { $track->delete(); throw $e; } return CommandResponse::succeed(['id' => $track->id, 'name' => $track->name, 'title' => $track->title, 'slug' => $track->slug, 'autoPublish' => $autoPublish]); }