Skip to content

Commit

Permalink
Refactor Audio class methods for raw tags and add support for "music"…
Browse files Browse the repository at this point in the history
… file extension
  • Loading branch information
ewilan-riviere committed Sep 30, 2024
1 parent e83c6b6 commit e150e9f
Show file tree
Hide file tree
Showing 15 changed files with 319 additions and 255 deletions.
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Core metadata:
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');

$audio->getTitle(); // `?string` to get title
$audio->getArtist(); // `?string` to get artist
Expand Down Expand Up @@ -74,7 +74,7 @@ Raw tags:
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');

$audio->getTags(); // `array` with all tags
$title = $audio->getTag('title'); // `?string` to get title same as `$audio->getTitle()`
Expand All @@ -90,7 +90,7 @@ Additional metadata:
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');

$audio->getPath(); // `string` to get path
$audio->hasCover(); // `bool` to know if has cover
Expand All @@ -109,7 +109,7 @@ Raw audio:
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');

$audio->toArray(); // `array` with all metadata
```
Expand All @@ -119,7 +119,7 @@ Advanced properties:
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');

$audio->getReader(); // `?Id3Reader` reader based on `getID3`
$audio->getWriter(); // `?Id3Writer` writer based on `getid3_writetags`
Expand All @@ -139,7 +139,7 @@ You can update audio files metadata with `Audio::class`, but not all formats are
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');
$audio->getTitle(); // `Title`

$tag = $audio->update()
Expand All @@ -163,7 +163,7 @@ $tag = $audio->update()
->cover('path/to/cover.jpg') // you can use file content `file_get_contents('path/to/cover.jpg')`
->save();

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');
$audio->getTitle(); // `New Title`
$audio->getCreationDate(); // `null` because `creationDate` is not supported by `MP3`
```
Expand All @@ -183,7 +183,7 @@ You can set tags manually with `tags` method, but you need to know the format of
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');
$audio->getAlbumArtist(); // `Band`

$tag = $audio->update()
Expand All @@ -194,7 +194,7 @@ $tag = $audio->update()
->tagFormats(['id3v1', 'id3v2.4']) // optional
->save();

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');
$audio->getAlbumArtist(); // `New Band`
```

Expand All @@ -203,15 +203,15 @@ $audio->getAlbumArtist(); // `New Band`
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');
$audio->getAlbumArtist(); // `Band`

$tag = $audio->update()
->title('New Title')
->albumArtist('New Band') // `albumArtist` will set `band` for `id3v2`, exception safe
->save();

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');
$audio->getAlbumArtist(); // `New Band`
```

Expand All @@ -222,7 +222,7 @@ You can use `preventFailOnError` to prevent exception if you use unsupported for
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');

$tag = $audio->update()
->tags([
Expand All @@ -238,7 +238,7 @@ Arrow functions are exception safe for properties but not for unsupported format
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');

$tag = $audio->update()
->encoding('New encoding') // not supported by `id3v2`, BUT will not throw an exception
Expand All @@ -253,7 +253,7 @@ Of course you can add cover with `tags` method.
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');
$cover = 'path/to/cover.jpg';

$image = getimagesize($cover);
Expand Down Expand Up @@ -285,7 +285,7 @@ Merge `tags` with arrow functions.
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get($path);
$audio = Audio::read($path);

$tag = $audio->update()
->title('New Title') // will be merged with `tags` and override `title` key
Expand All @@ -296,7 +296,7 @@ $tag = $audio->update()

$tag->save();

$audio = Audio::get($path);
$audio = Audio::read($path);
expect($audio->getTitle())->toBe('New Title');
expect($audio->getAlbumArtist())->toBe('New Band');
```
Expand All @@ -310,7 +310,7 @@ If you want to extract specific field which can be skipped by `Audio::class`, yo
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');
$extras = $audio->getExtras();

$id3v2 = $extras['id3v2'] ?? [];
Expand All @@ -321,7 +321,7 @@ $id3v2 = $extras['id3v2'] ?? [];
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');

$audio->getAudio()->getFilesize(); // `?int` in bytes
$audio->getAudio()->getExtension(); // `?string` (mp3, m4a, ...)
Expand All @@ -343,7 +343,7 @@ $audio->getAudio()->getCompressionRatio(); // `?float`
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');

$audio->getCover()->getContents(); // `?string` raw file
$audio->getCover()->getMimeType(); // `?string` (image/jpeg, image/png, ...)
Expand Down Expand Up @@ -474,7 +474,7 @@ In `Audio::class`, you have a property `extras` which contains all raw metadata,
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');
$extras = $audio->getExtras();

$custom = null;
Expand All @@ -494,7 +494,7 @@ You can check `extras` property to know if some metadata are available.
```php
use Kiwilan\Audio\Audio;

$audio = Audio::get('path/to/audio.mp3');
$audio = Audio::read('path/to/audio.mp3');

$extras = $audio->getExtras();
var_dump($extras);
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"wma",
"wv",
"wav",
"webm"
"webm",
"music"
],
"homepage": "https://github.com/kiwilan/php-audio",
"license": "MIT",
Expand Down
12 changes: 11 additions & 1 deletion src/Audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function __construct(
protected array $raw_tags_all = [],
) {}

public static function get(string $path): self
public static function read(string $path): self
{
$fileExists = file_exists($path);
if (! $fileExists) {
Expand Down Expand Up @@ -82,6 +82,16 @@ public static function get(string $path): self
return $self;
}

/**
* @deprecated Use `read()` method instead.
*
* Get audio file from path.
*/
public static function get(string $path): self
{
return self::read($path);
}

/**
* Get audio file path, like `/path/to/audio.mp3`.
*/
Expand Down
1 change: 0 additions & 1 deletion src/Core/AudioCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public static function toId3v2(AudioCore $core): Tag\Id3TagAudioV2
track_number: $core->track_number,
year: (string) $core->year,
copyright: $core->copyright,
text: $core->synopsis,
unsynchronised_lyric: $core->lyrics,
language: $core->language,
);
Expand Down
58 changes: 11 additions & 47 deletions src/Core/AudioCoreCover.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,26 @@
class AudioCoreCover
{
public function __construct(
protected ?string $data = null,
protected ?int $picture_type_id = null,
protected ?string $description = null,
protected ?string $mime = null,
public ?string $data = null,
public ?int $picture_type_id = null,
public ?string $description = null,
public ?string $mime = null,
) {}

public static function make(string $pathOrData): self
{
$self = new self;

if (file_exists($pathOrData)) {
$image = getimagesize($pathOrData);
$self->data = base64_encode(file_get_contents($pathOrData));
$self->picture_type_id = $image[2];
$self->description = 'cover';
$self->mime = $image['mime'];

return $self;
}

$image = getimagesizefromstring($pathOrData);
$self->data = base64_encode($pathOrData);
$image = file_exists($pathOrData)
? getimagesize($pathOrData)
: getimagesizefromstring($pathOrData);
$self->data = file_exists($pathOrData)
? base64_encode(file_get_contents($pathOrData))
: base64_encode($pathOrData);
$self->picture_type_id = $image[2];
$self->mime = $image['mime'];
$self->description = 'cover';
$self->mime = $image['mime'];

return $self;
}

public function data(): ?string
{
return $this->data;
}

public function pictureTypeId(): ?int
{
return $this->picture_type_id;
}

public function description(): ?string
{
return $this->description;
}

public function mime(): ?string
{
return $this->mime;
}

public function toArray(): array
{
return [
'data' => $this->data,
'picture_type_id' => $this->picture_type_id,
'description' => $this->description,
'mime' => $this->mime,
];
}
}
Loading

0 comments on commit e150e9f

Please sign in to comment.