Skip to content

Commit

Permalink
Merge pull request #23 from kiwilan/develop
Browse files Browse the repository at this point in the history
v3.0.05
  • Loading branch information
ewilan-riviere authored Feb 3, 2024
2 parents 8de55cb + eeffe50 commit db9f825
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 7 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ $audio->getStik(); // `?string` (audiobook)
$audio->getDuration(); // `?float` to get duration in seconds
```

Raw tags:

```php
use Kiwilan\Audio\Audio;

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

$audio->getTags(); // `array` with all tags
$title = $audio->getTag('title'); // `?string` to get title same as `$audio->getTitle()`
```

Additional metadata:

```php
Expand All @@ -82,6 +93,20 @@ $audio->getType(); // `?AudioTypeEnum` ID3 type (id3, riff, asf, quicktime, matr
$audio->getExtras(); // `array` with raw metadata (could contains some metadata not parsed)
```

Raw audio:

> [!NOTE]
>
> Cover is removed from `toArray()` method, you can use `getCover()` method to get cover metadata.
```php
use Kiwilan\Audio\Audio;

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

$audio->toArray(); // `array` with all metadata
```

Advanced properties:

```php
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kiwilan/php-audio",
"description": "PHP package to parse and update audio files metadata, with `JamesHeinrich/getID3`.",
"version": "3.0.04",
"version": "3.0.05",
"keywords": [
"audio",
"php",
Expand Down
27 changes: 27 additions & 0 deletions src/Audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,31 @@ private function coreToProperties(?AudioCore $core): self

return $this;
}

public function getTag(string $tag): ?string
{
$tags = $this->reader->toTagsArray();

return $tags[$tag] ?? null;
}

/**
* Get all tags as array.
*
* @return array<string, string>
*/
public function getTags(): array
{
return $this->reader->toTagsArray();
}

/**
* Get all raw metadata as array.
*
* @return array<string, mixed>
*/
public function toArray(): array
{
return $this->reader->toArray();
}
}
23 changes: 23 additions & 0 deletions src/Models/Id3Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,29 @@ public function getRaw(): array
{
return $this->raw;
}

public function toTagsArray(): array
{
$tags = $this->raw['tags'] ?? [];
$first = reset($tags);

$items = [];
foreach ($first as $key => $value) {
$items[$key] = $value[0] ?? null;
}

return $items;
}

public function toArray(): array
{
$raw = $this->raw;
if (array_key_exists('comments', $raw) && array_key_exists('picture', $raw['comments'])) {
$raw['comments']['picture'] = 'cover string (removed for array)';
}

return $raw;
}
}

class Id3Audio
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Id3Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public function stik(?string $stik): self
}

/**
* @param string $pathOrData Path to cover image or binary data
* @param string $pathOrData Path to cover image or binary data
*/
public function cover(string $pathOrData): self
{
Expand Down Expand Up @@ -288,7 +288,7 @@ public function tags(array $tags): self
/**
* Set tag format.
*
* @param string[] $tags Options are `id3v1`, `id3v2.2`, `id2v2.3`, `id3v2.4`, `ape`, `vorbiscomment`, `metaflac`, `real`
* @param string[] $tags Options are `id3v1`, `id3v2.2`, `id2v2.3`, `id3v2.4`, `ape`, `vorbiscomment`, `metaflac`, `real`
*/
public function tagFormats(array $tags): self
{
Expand Down
2 changes: 2 additions & 0 deletions tests/AudioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
expect($audio->getFormat())->toBe($format);
expect($audio->getDuration())->toBeFloat();
expect($audio->getExtras())->toBeArray();
expect($audio->getTags())->toBeArray();
expect($audio->toArray())->toBeArray();

$metadata = $audio->getAudio();
expect($metadata->getPath())->toBeString();
Expand Down
17 changes: 13 additions & 4 deletions tests/AudiobookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,27 @@
expect($audio->getTrackNumber())->toBe('1/1');
expect($audio->getComment())->toBe('P1PDD team');
expect($audio->getAlbumArtist())->toBe('Mr Piouf and P1PDD');
expect($audio->getComposer())->toBeNull();
expect($audio->getDiscNumber())->toBeNull();
expect($audio->getComposer())->toBe('Composer');
expect($audio->getDiscNumber())->toBe('1');
expect($audio->isCompilation())->toBe(false);
expect($audio->getPath())->toBe(AUDIOBOOK);
expect($audio->getFormat())->toBe(AudioFormatEnum::m4b);
expect($audio->getCreationDate())->toBe('2023-06-04T12:00:00Z');
expect($audio->getEncodingBy())->toBe('Mr Piouf');
expect($audio->getEncoding())->toBe('Audiobook Builder 2.2.6 (www.splasm.com), macOS 13.4');
expect($audio->getCopyright())->toBeString();
expect($audio->getDescription())->toBe('Première campagne de P1PDD');
expect($audio->getLyrics())->toBe('P1PDD');
expect($audio->getDescription())->toBe('Description');
expect($audio->getLyrics())->toBe('Lyrics');
expect($audio->getStik())->toBe('Audiobook');
expect($audio->getDuration())->toBe(11.00);
expect($audio->getExtras())->toBeArray();
expect($audio->toArray())->toBeArray();

expect($audio->getTags())->toBeArray();
expect($audio->getTag('title'))->toBe('P1PDD Saison 1');
expect($audio->getTag('artist'))->toBe('Mr Piouf');
expect($audio->getTag('album'))->toBe('P1PDD Saison 1');
expect($audio->getTag('genre'))->toBe('Audiobooks');
expect($audio->getTag('track_number'))->toBe('1/1');
expect($audio->getTag('comment'))->toBe('P1PDD team');
});
Binary file modified tests/media/audiobook.m4b
Binary file not shown.

0 comments on commit db9f825

Please sign in to comment.