mirror of
https://github.com/Poniverse/Pony.fm.git
synced 2024-11-22 04:58:01 +01:00
T350: Removed some leftover files and corrected the MLPMA importer's reference to getID3.
This commit is contained in:
parent
5188e4947c
commit
583b03f132
4 changed files with 1 additions and 834 deletions
|
@ -17,8 +17,8 @@ use Illuminate\Console\Command;
|
|||
use Illuminate\Support\Str;
|
||||
use Input;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
use getID3;
|
||||
|
||||
require_once(app_path() . '/Library/getid3/getid3/getid3.php');
|
||||
|
||||
class ImportMLPMA extends Command
|
||||
{
|
||||
|
|
|
@ -1,235 +0,0 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Entities\ShowSong;
|
||||
use Entities\Track;
|
||||
use Entities\TrackType;
|
||||
|
||||
class ClassifyMLPMA extends Command {
|
||||
|
||||
/**
|
||||
* The console command name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'mlpma:classify';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Adds Pony.fm-specific metadata to imported MLPMA tracks.';
|
||||
|
||||
/**
|
||||
* A counter for the number of processed tracks.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $currentTrack = 0;
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function fire()
|
||||
{
|
||||
// Get the list of tracks that need classification
|
||||
$tracks = DB::table('mlpma_tracks')
|
||||
->orderBy('id')
|
||||
->get();
|
||||
|
||||
$this->comment('Importing tracks...');
|
||||
|
||||
$totalTracks = sizeof($tracks);
|
||||
|
||||
$fileToStartAt = (int) $this->option('startAt') - 1;
|
||||
$this->comment("Skipping $fileToStartAt files..." . PHP_EOL);
|
||||
|
||||
$tracks = array_slice($tracks, $fileToStartAt);
|
||||
$this->currentTrack = $fileToStartAt;
|
||||
|
||||
foreach ($tracks as $track) {
|
||||
$this->currentTrack++;
|
||||
$this->comment('[' . $this->currentTrack . '/' . $totalTracks . '] Classifying track [' . $track->filename . ']...');
|
||||
|
||||
$parsedTags = json_decode($track->parsed_tags, true);
|
||||
|
||||
|
||||
//==========================================================================================================
|
||||
// Original, show song remix, fan song remix, show audio remix, or ponified song?
|
||||
//==========================================================================================================
|
||||
$sanitizedTrackTitle = $parsedTags['title'];
|
||||
$sanitizedTrackTitle = str_replace(['-', '+', '~', 'ft.', '*', '(', ')', '.'], ' ', $sanitizedTrackTitle);
|
||||
|
||||
$queriedTitle = DB::connection()->getPdo()->quote($sanitizedTrackTitle);
|
||||
$officialSongs = ShowSong::select(['id', 'title'])
|
||||
->whereRaw("
|
||||
MATCH (title)
|
||||
AGAINST ($queriedTitle IN BOOLEAN MODE)
|
||||
")
|
||||
->get();
|
||||
|
||||
|
||||
// If it has "Ingram" in the name, it's definitely an official song remix.
|
||||
if (Str::contains(Str::lower($track->filename), 'ingram')) {
|
||||
$this->info('This is an official song remix!');
|
||||
|
||||
list($trackType, $linkedSongIds) = $this->classifyTrack($track->filename, $officialSongs, true, $parsedTags);
|
||||
|
||||
|
||||
// If it has "remix" in the name, it's definitely a remix.
|
||||
} else if (Str::contains(Str::lower($sanitizedTrackTitle), 'remix')) {
|
||||
$this->info('This is some kind of remix!');
|
||||
|
||||
list($trackType, $linkedSongIds) = $this->classifyTrack($track->filename, $officialSongs, false, $parsedTags);
|
||||
|
||||
// No idea what this is. Have the pony at the terminal figure it out!
|
||||
} else {
|
||||
list($trackType, $linkedSongIds) = $this->classifyTrack($track->filename, $officialSongs, false, $parsedTags);
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================================================
|
||||
// Attach the data and publish the track!
|
||||
//==========================================================================================================
|
||||
|
||||
$track = Track::find($track->track_id);
|
||||
|
||||
$track->track_type_id = $trackType;
|
||||
$track->published_at = $parsedTags['released_at'];
|
||||
$track->save();
|
||||
|
||||
if (sizeof($linkedSongIds) > 0) {
|
||||
$track->showSongs()->sync($linkedSongIds);
|
||||
}
|
||||
|
||||
echo PHP_EOL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command arguments.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getArguments()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
return array(
|
||||
array('startAt', null, InputOption::VALUE_OPTIONAL, 'Track to start importing from. Useful for resuming an interrupted import.', 1),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determines what type of track the given file is. If unable to guess, the user
|
||||
* is asked to identify it interactively.
|
||||
*
|
||||
* @param string $filename
|
||||
* @param \Entities\ShowSong[] $officialSongs
|
||||
* @param bool|false $isRemixOfOfficialTrack
|
||||
* @return array
|
||||
*/
|
||||
protected function classifyTrack($filename, $officialSongs, $isRemixOfOfficialTrack = false, $tags) {
|
||||
$trackTypeId = null;
|
||||
$linkedSongIds = [];
|
||||
|
||||
|
||||
foreach ($officialSongs as $song) {
|
||||
$this->comment('=> Matched official song: [' . $song->id . '] ' . $song->title);
|
||||
}
|
||||
|
||||
|
||||
if ($isRemixOfOfficialTrack && sizeof($officialSongs) === 1) {
|
||||
$linkedSongIds = [$officialSongs[0]->id];
|
||||
|
||||
} else {
|
||||
if ($isRemixOfOfficialTrack && sizeof($officialSongs) > 1) {
|
||||
$this->question('Multiple official songs matched! Please enter the ID of the correct one.');
|
||||
|
||||
} else if (sizeof($officialSongs) > 0) {
|
||||
$this->question('This looks like a remix of an official song!');
|
||||
$this->question('Press "r" if the match above is right!');
|
||||
|
||||
} else {
|
||||
$this->question('Exactly what kind of track is this?');
|
||||
|
||||
}
|
||||
$this->question('If this is a medley, multiple song ID\'s can be separated by commas. ');
|
||||
$this->question(' ');
|
||||
$this->question(' ' . $filename . ' ');
|
||||
$this->question(' ');
|
||||
$this->question(' Title: '.$tags['title'].' ');
|
||||
$this->question(' Album: '.$tags['album'].' ');
|
||||
$this->question(' Artist: '.$tags['artist'].' ');
|
||||
$this->question(' ');
|
||||
$this->question(' r = official song remix (accept all "guessed" matches) ');
|
||||
$this->question(' # = official song remix (enter the ID(s) of the show song(s)) ');
|
||||
$this->question(' a = show audio remix ');
|
||||
$this->question(' f = fan track remix ');
|
||||
$this->question(' p = ponified track ');
|
||||
$this->question(' o = original track ');
|
||||
$this->question(' ');
|
||||
$input = $this->ask('[r/#/a/f/p/o]: ');
|
||||
|
||||
switch ($input) {
|
||||
case 'r':
|
||||
$trackTypeId = TrackType::OFFICIAL_TRACK_REMIX;
|
||||
foreach ($officialSongs as $officialSong) {
|
||||
$linkedSongIds[] = (int) $officialSong->id;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'a':
|
||||
$trackTypeId = TrackType::OFFICIAL_AUDIO_REMIX;
|
||||
break;
|
||||
|
||||
case 'f':
|
||||
$trackTypeId = TrackType::FAN_TRACK_REMIX;
|
||||
break;
|
||||
|
||||
case 'p':
|
||||
$trackTypeId = TrackType::PONIFIED_TRACK;
|
||||
break;
|
||||
|
||||
case 'o':
|
||||
$trackTypeId = TrackType::ORIGINAL_TRACK;
|
||||
break;
|
||||
|
||||
default:
|
||||
$trackTypeId = TrackType::OFFICIAL_TRACK_REMIX;
|
||||
$linkedSongIds = explode(',', $input);
|
||||
$linkedSongIds = array_map(function ($item) {
|
||||
return (int) $item;
|
||||
}, $linkedSongIds);
|
||||
}
|
||||
}
|
||||
|
||||
return [$trackTypeId, $linkedSongIds];
|
||||
}
|
||||
|
||||
}
|
|
@ -1,522 +0,0 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Entities\Album;
|
||||
use Entities\Image;
|
||||
use Entities\Genre;
|
||||
use Entities\User;
|
||||
use Entities\Track;
|
||||
use Commands\UploadTrackCommand;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
use Carbon\Carbon;
|
||||
|
||||
require_once(app_path() . '/library/getid3/getid3/getid3.php');
|
||||
|
||||
|
||||
class ImportMLPMA extends Command {
|
||||
|
||||
/**
|
||||
* The console command name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'mlpma:import';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Imports the MLP Music Archive';
|
||||
|
||||
/**
|
||||
* File extensions to ignore when importing the archive.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $ignoredExtensions = ['db', 'jpg', 'png', 'txt', 'rtf', 'wma', 'wmv'];
|
||||
|
||||
/**
|
||||
* Used to stop the import process when a SIGINT is received.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $isInterrupted = false;
|
||||
|
||||
/**
|
||||
* A counter for the number of processed tracks.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $currentFile;
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function handleInterrupt($signo) {
|
||||
$this->error('Import aborted!');
|
||||
$this->error('Resume it from here using: --startAt='.$this->currentFile);
|
||||
$this->isInterrupted = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function fire()
|
||||
{
|
||||
pcntl_signal(SIGINT, [$this, 'handleInterrupt']);
|
||||
|
||||
$mlpmaPath = Config::get('app.files_directory').'mlpma';
|
||||
$tmpPath = Config::get('app.files_directory').'tmp';
|
||||
|
||||
if (!File::exists($tmpPath)) {
|
||||
File::makeDirectory($tmpPath);
|
||||
}
|
||||
|
||||
$UNKNOWN_GENRE = Genre::firstOrCreate([
|
||||
'name' => 'Unknown',
|
||||
'slug' => 'unknown'
|
||||
]);
|
||||
|
||||
$this->comment('Enumerating MLP Music Archive source files...');
|
||||
$files = File::allFiles($mlpmaPath);
|
||||
$this->info(sizeof($files).' files found!');
|
||||
|
||||
$this->comment('Enumerating artists...');
|
||||
$artists = File::directories($mlpmaPath);
|
||||
$this->info(sizeof($artists).' artists found!');
|
||||
|
||||
$this->comment('Importing tracks...');
|
||||
|
||||
$totalFiles = sizeof($files);
|
||||
|
||||
$fileToStartAt = (int) $this->option('startAt') - 1;
|
||||
$this->comment("Skipping $fileToStartAt files...".PHP_EOL);
|
||||
|
||||
$files = array_slice($files, $fileToStartAt);
|
||||
$this->currentFile = $fileToStartAt;
|
||||
|
||||
foreach($files as $file) {
|
||||
$this->currentFile++;
|
||||
|
||||
pcntl_signal_dispatch();
|
||||
if ($this->isInterrupted) {
|
||||
break;
|
||||
}
|
||||
|
||||
$this->comment('['.$this->currentFile.'/'.$totalFiles.'] Importing track ['. $file->getFilename() .']...');
|
||||
|
||||
if (in_array($file->getExtension(), $this->ignoredExtensions)) {
|
||||
$this->comment('This is not an audio file! Skipping...'.PHP_EOL);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// Has this track already been imported?
|
||||
$importedTrack = DB::table('mlpma_tracks')
|
||||
->where('filename', '=', $file->getFilename())
|
||||
->first();
|
||||
|
||||
if ($importedTrack) {
|
||||
$this->comment('This track has already been imported! Skipping...' . PHP_EOL);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================================================
|
||||
// Extract the original tags.
|
||||
//==========================================================================================================
|
||||
$getId3 = new getID3;
|
||||
|
||||
// all tags read by getID3, including the cover art
|
||||
$allTags = $getId3->analyze($file->getPathname());
|
||||
|
||||
// tags specific to a file format (ID3 or Atom), pre-normalization but with cover art removed
|
||||
$rawTags = [];
|
||||
|
||||
// normalized tags used by Pony.fm
|
||||
$parsedTags = [];
|
||||
|
||||
if (Str::lower($file->getExtension()) === 'mp3') {
|
||||
list($parsedTags, $rawTags) = $this->getId3Tags($allTags);
|
||||
|
||||
} else if (Str::lower($file->getExtension()) === 'm4a') {
|
||||
list($parsedTags, $rawTags) = $this->getAtomTags($allTags);
|
||||
|
||||
} else if (Str::lower($file->getExtension()) === 'ogg') {
|
||||
list($parsedTags, $rawTags) = $this->getVorbisTags($allTags);
|
||||
|
||||
} else if (Str::lower($file->getExtension()) === 'flac') {
|
||||
list($parsedTags, $rawTags) = $this->getVorbisTags($allTags);
|
||||
|
||||
} else if (Str::lower($file->getExtension()) === 'wav') {
|
||||
list($parsedTags, $rawTags) = $this->getAtomTags($allTags);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================================================
|
||||
// Determine the release date.
|
||||
//==========================================================================================================
|
||||
$modifiedDate = Carbon::createFromTimeStampUTC(File::lastModified($file->getPathname()));
|
||||
$taggedYear = $parsedTags['year'];
|
||||
|
||||
$this->info('Modification year: '.$modifiedDate->year);
|
||||
$this->info('Tagged year: '.$taggedYear);
|
||||
|
||||
if ($taggedYear !== null && $modifiedDate->year === $taggedYear) {
|
||||
$releasedAt = $modifiedDate;
|
||||
|
||||
} else if ($taggedYear !== null && Str::length((string) $taggedYear) !== 4) {
|
||||
$this->error('This track\'s tagged year makes no sense! Using the track\'s last modified date...');
|
||||
$releasedAt = $modifiedDate;
|
||||
|
||||
} else if ($taggedYear !== null && $modifiedDate->year !== $taggedYear) {
|
||||
$this->error('Release years don\'t match! Using the tagged year...');
|
||||
$releasedAt = Carbon::create($taggedYear);
|
||||
|
||||
} else {
|
||||
// $taggedYear is null
|
||||
$this->error('This track isn\'t tagged with its release year! Using the track\'s last modified date...');
|
||||
$releasedAt = $modifiedDate;
|
||||
}
|
||||
|
||||
// This is later used by the classification/publishing script to determine the publication date.
|
||||
$parsedTags['released_at'] = $releasedAt->toDateTimeString();
|
||||
|
||||
//==========================================================================================================
|
||||
// Does this track have vocals?
|
||||
//==========================================================================================================
|
||||
$isVocal = $parsedTags['lyrics'] !== null;
|
||||
|
||||
|
||||
//==========================================================================================================
|
||||
// Fill in the title tag if it's missing.
|
||||
//==========================================================================================================
|
||||
if (!$parsedTags['title']) {
|
||||
$parsedTags['title'] = $file->getBasename('.'.$file->getExtension());
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================================================
|
||||
// Determine the genre.
|
||||
//==========================================================================================================
|
||||
$genreName = $parsedTags['genre'];
|
||||
$genreSlug = Str::slug($genreName);
|
||||
$this->info('Genre: '.$genreName);
|
||||
|
||||
if ($genreName && $genreSlug !== '') {
|
||||
$genre = Genre::where('name', '=', $genreName)->first();
|
||||
if ($genre) {
|
||||
$genreId = $genre->id;
|
||||
|
||||
} else {
|
||||
$genre = new Genre();
|
||||
$genre->name = $genreName;
|
||||
$genre->slug = $genreSlug;
|
||||
$genre->save();
|
||||
$genreId = $genre->id;
|
||||
$this->comment('Created a new genre!');
|
||||
}
|
||||
|
||||
} else {
|
||||
$genreId = $UNKNOWN_GENRE->id; // "Unknown" genre ID
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================================================
|
||||
// Determine which artist account this file belongs to using the containing directory.
|
||||
//==========================================================================================================
|
||||
$this->info('Path to file: '.$file->getRelativePath());
|
||||
$path_components = explode(DIRECTORY_SEPARATOR, $file->getRelativePath());
|
||||
$artist_name = $path_components[0];
|
||||
$album_name = array_key_exists(1, $path_components) ? $path_components[1] : null;
|
||||
|
||||
$this->info('Artist: '.$artist_name);
|
||||
$this->info('Album: '.$album_name);
|
||||
|
||||
$artist = User::where('display_name', '=', $artist_name)->first();
|
||||
|
||||
if (!$artist) {
|
||||
$artist = new User;
|
||||
$artist->display_name = $artist_name;
|
||||
$artist->email = null;
|
||||
$artist->is_archived = true;
|
||||
|
||||
$artist->slug = Str::slug($artist_name);
|
||||
|
||||
$slugExists = User::where('slug', '=', $artist->slug)->first();
|
||||
if ($slugExists) {
|
||||
$this->error('Horsefeathers! The slug '.$artist->slug.' is already taken!');
|
||||
$artist->slug = $artist->slug.'-'.Str::random(4);
|
||||
}
|
||||
|
||||
$artist->save();
|
||||
}
|
||||
|
||||
//==========================================================================================================
|
||||
// Extract the cover art, if any exists.
|
||||
//==========================================================================================================
|
||||
$this->comment('Extracting cover art!');
|
||||
$coverId = null;
|
||||
if (array_key_exists('comments', $allTags) && array_key_exists('picture', $allTags['comments'])) {
|
||||
$image = $allTags['comments']['picture'][0];
|
||||
|
||||
if ($image['image_mime'] === 'image/png') {
|
||||
$extension = 'png';
|
||||
|
||||
} else if ($image['image_mime'] === 'image/jpeg') {
|
||||
$extension = 'jpg';
|
||||
|
||||
} else if ($image['image_mime'] === 'image/gif') {
|
||||
$extension = 'gif';
|
||||
|
||||
} else {
|
||||
$this->error('Unknown cover art format!');
|
||||
}
|
||||
|
||||
// write temporary image file
|
||||
$imageFilename = $file->getFilename() . ".cover.$extension";
|
||||
$imageFilePath = "$tmpPath/".$imageFilename;
|
||||
File::put($imageFilePath, $image['data']);
|
||||
|
||||
|
||||
$imageFile = new UploadedFile($imageFilePath, $imageFilename, $image['image_mime']);
|
||||
|
||||
$cover = Image::upload($imageFile, $artist);
|
||||
$coverId = $cover->id;
|
||||
|
||||
} else {
|
||||
$this->comment('No cover art found!');
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================================================
|
||||
// Is this part of an album?
|
||||
//==========================================================================================================
|
||||
$albumId = null;
|
||||
$albumName = $parsedTags['album'];
|
||||
|
||||
if ($albumName !== null) {
|
||||
$album = Album::where('user_id', '=', $artist->id)
|
||||
->where('title', '=', $albumName)
|
||||
->first();
|
||||
|
||||
if (!$album) {
|
||||
$album = new Album;
|
||||
|
||||
$album->title = $albumName;
|
||||
$album->user_id = $artist->id;
|
||||
$album->cover_id = $coverId;
|
||||
|
||||
$album->save();
|
||||
}
|
||||
|
||||
$albumId = $album->id;
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================================================
|
||||
// Save this track.
|
||||
//==========================================================================================================
|
||||
// "Upload" the track to Pony.fm
|
||||
$this->comment('Transcoding the track!');
|
||||
Auth::loginUsingId($artist->id);
|
||||
|
||||
$trackFile = new UploadedFile($file->getPathname(), $file->getFilename(), $allTags['mime_type']);
|
||||
Input::instance()->files->add(['track' => $trackFile]);
|
||||
|
||||
$upload = new UploadTrackCommand(true, true);
|
||||
$result = $upload->execute();
|
||||
|
||||
if ($result->didFail()) {
|
||||
$this->error(json_encode($result->getValidator()->messages()->getMessages(), JSON_PRETTY_PRINT));
|
||||
|
||||
} else {
|
||||
// Save metadata.
|
||||
$track = Track::find($result->getResponse()['id']);
|
||||
|
||||
$track->title = $parsedTags['title'];
|
||||
$track->cover_id = $coverId;
|
||||
$track->album_id = $albumId;
|
||||
$track->genre_id = $genreId;
|
||||
$track->track_number = $parsedTags['track_number'];
|
||||
$track->released_at = $releasedAt;
|
||||
$track->description = $parsedTags['comments'];
|
||||
$track->is_downloadable = true;
|
||||
$track->lyrics = $parsedTags['lyrics'];
|
||||
$track->is_vocal = $isVocal;
|
||||
$track->license_id = 2;
|
||||
$track->save();
|
||||
|
||||
// If we made it to here, the track is intact! Log the import.
|
||||
DB::table('mlpma_tracks')
|
||||
->insert([
|
||||
'track_id' => $result->getResponse()['id'],
|
||||
'path' => $file->getRelativePath(),
|
||||
'filename' => $file->getFilename(),
|
||||
'extension' => $file->getExtension(),
|
||||
'imported_at' => Carbon::now(),
|
||||
'parsed_tags' => json_encode($parsedTags),
|
||||
'raw_tags' => json_encode($rawTags),
|
||||
]);
|
||||
}
|
||||
|
||||
echo PHP_EOL.PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command arguments.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getArguments()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
return array(
|
||||
array('startAt', null, InputOption::VALUE_OPTIONAL, 'Track to start importing from. Useful for resuming an interrupted import.', 1),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $rawTags
|
||||
* @return array
|
||||
*/
|
||||
protected function getId3Tags($rawTags) {
|
||||
if (array_key_exists('tags', $rawTags) &&
|
||||
array_key_exists('id3v2', $rawTags['tags'])) {
|
||||
$tags = $rawTags['tags']['id3v2'];
|
||||
|
||||
} else if (
|
||||
array_key_exists('tags', $rawTags) &&
|
||||
array_key_exists('id3v1', $rawTags['tags'])) {
|
||||
$tags = $rawTags['tags']['id3v1'];
|
||||
|
||||
} else {
|
||||
$tags = [];
|
||||
}
|
||||
|
||||
$comment = null;
|
||||
|
||||
if (isset($tags['comment'])) {
|
||||
// The "comment" tag comes in with a badly encoded string index
|
||||
// so its array key has to be used implicitly.
|
||||
$key = array_keys($tags['comment'])[0];
|
||||
|
||||
// The comment may have a null byte at the end. trim() removes it.
|
||||
$comment = trim($tags['comment'][$key]);
|
||||
|
||||
// Replace the malformed comment with the "fixed" one.
|
||||
unset($tags['comment'][$key]);
|
||||
$tags['comment'][0] = $comment;
|
||||
}
|
||||
|
||||
return [
|
||||
[
|
||||
'title' => isset($tags['title']) ? $tags['title'][0] : null,
|
||||
'artist' => isset($tags['artist']) ? $tags['artist'][0] : null,
|
||||
'band' => isset($tags['band']) ? $tags['band'][0] : null,
|
||||
'genre' => isset($tags['genre']) ? $tags['genre'][0] : null,
|
||||
'track_number' => isset($tags['track_number']) ? $tags['track_number'][0] : null,
|
||||
'album' => isset($tags['album']) ? $tags['album'][0] : null,
|
||||
'year' => isset($tags['year']) ? (int) $tags['year'][0] : null,
|
||||
'comments' => $comment,
|
||||
'lyrics' => isset($tags['unsynchronised_lyric']) ? $tags['unsynchronised_lyric'][0] : null,
|
||||
],
|
||||
$tags];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $rawTags
|
||||
* @return array
|
||||
*/
|
||||
protected function getAtomTags($rawTags) {
|
||||
|
||||
if (array_key_exists('tags', $rawTags) &&
|
||||
array_key_exists('quicktime', $rawTags['tags'])) {
|
||||
$tags = $rawTags['tags']['quicktime'];
|
||||
} else {
|
||||
$tags = [];
|
||||
}
|
||||
|
||||
$trackNumber = null;
|
||||
if (isset($tags['track_number'])) {
|
||||
$trackNumberComponents = explode('/', $tags['track_number'][0]);
|
||||
$trackNumber = $trackNumberComponents[0];
|
||||
}
|
||||
|
||||
return [
|
||||
[
|
||||
'title' => isset($tags['title']) ? $tags['title'][0] : null,
|
||||
'artist' => isset($tags['artist']) ? $tags['artist'][0] : null,
|
||||
'band' => isset($tags['band']) ? $tags['band'][0] : null,
|
||||
'album_artist' => isset($tags['album_artist']) ? $tags['album_artist'][0] : null,
|
||||
'genre' => isset($tags['genre']) ? $tags['genre'][0] : null,
|
||||
'track_number' => $trackNumber,
|
||||
'album' => isset($tags['album']) ? $tags['album'][0] : null,
|
||||
'year' => isset($tags['year']) ? (int) $tags['year'][0] : null,
|
||||
'comments' => isset($tags['comments']) ? $tags['comments'][0] : null,
|
||||
'lyrics' => isset($tags['lyrics']) ? $tags['lyrics'][0] : null,
|
||||
],
|
||||
$tags];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $rawTags
|
||||
* @return array
|
||||
*/
|
||||
protected function getVorbisTags($rawTags) {
|
||||
if (array_key_exists('tags', $rawTags) &&
|
||||
array_key_exists('vorbiscomment', $rawTags['tags'])
|
||||
) {
|
||||
$tags = $rawTags['tags']['vorbiscomment'];
|
||||
} else {
|
||||
$tags = [];
|
||||
}
|
||||
|
||||
$trackNumber = null;
|
||||
if (isset($tags['track_number'])) {
|
||||
$trackNumberComponents = explode('/', $tags['track_number'][0]);
|
||||
$trackNumber = $trackNumberComponents[0];
|
||||
}
|
||||
|
||||
return [
|
||||
[
|
||||
'title' => isset($tags['title']) ? $tags['title'][0] : null,
|
||||
'artist' => isset($tags['artist']) ? $tags['artist'][0] : null,
|
||||
'band' => isset($tags['band']) ? $tags['band'][0] : null,
|
||||
'album_artist' => isset($tags['album_artist']) ? $tags['album_artist'][0] : null,
|
||||
'genre' => isset($tags['genre']) ? $tags['genre'][0] : null,
|
||||
'track_number' => $trackNumber,
|
||||
'album' => isset($tags['album']) ? $tags['album'][0] : null,
|
||||
'year' => isset($tags['year']) ? (int) $tags['year'][0] : null,
|
||||
'comments' => isset($tags['comments']) ? $tags['comments'][0] : null,
|
||||
'lyrics' => isset($tags['lyrics']) ? $tags['lyrics'][0] : null,
|
||||
],
|
||||
$tags];
|
||||
}
|
||||
|
||||
}
|
|
@ -1,76 +0,0 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Entities\Track;
|
||||
|
||||
class RebuildTags extends Command {
|
||||
|
||||
/**
|
||||
* The console command name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'rebuild:tags';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Rewrites tags in track files, ensuring they\'re up to date.';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function fire()
|
||||
{
|
||||
if ($this->argument('trackId')) {
|
||||
$track = Track::findOrFail($this->argument('trackId'));
|
||||
$tracks = [$track];
|
||||
} else {
|
||||
$tracks = Track::whereNotNull('published_at')->get();
|
||||
}
|
||||
|
||||
foreach($tracks as $track) {
|
||||
$this->comment('Rewriting tags for track #'.$track->id.'...');
|
||||
$track->updateTags();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command arguments.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getArguments()
|
||||
{
|
||||
return array(
|
||||
array('trackId', InputArgument::OPTIONAL, 'ID of the track to rebuild tags for.'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue