Skip to content

Commit

Permalink
fix(metadata): add support for vorbis tags/comments in FileSynchronizer
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoxd authored and phanan committed Sep 15, 2022
1 parent b6465c6 commit 4c7e264
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 19 deletions.
8 changes: 5 additions & 3 deletions app/Services/FileSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ public function setFile(string|SplFileInfo $path): self

public function getFileScanInformation(): ?SongScanInformation
{
$info = $this->getID3->analyze($this->filePath);
$this->syncError = Arr::get($info, 'error.0') ?: (Arr::get($info, 'playtime_seconds') ? null : 'Empty file');
$raw = $this->getID3->analyze($this->filePath);
$this->syncError = Arr::get($raw, 'error.0') ?: (Arr::get($raw, 'playtime_seconds') ? null : 'Empty file');

if ($this->syncError) {
return null;
}

$info = SongScanInformation::fromGetId3Info($info);
$this->getID3->CopyTagsToComments($raw);
$info = SongScanInformation::fromGetId3Info($raw);

$info->lyrics = $info->lyrics ?: $this->lrcReader->tryReadForMediaFile($this->filePath);

return $info;
Expand Down
25 changes: 21 additions & 4 deletions app/Values/SongScanInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ private function __construct(
public static function fromGetId3Info(array $info): self
{
// We prefer ID3v2 tags over ID3v1 tags.
$tags = array_merge(Arr::get($info, 'tags.id3v1', []), Arr::get($info, 'tags.id3v2', []));
$tags = array_merge(
Arr::get($info, 'tags.id3v1', []),
Arr::get($info, 'tags.id3v2', []),
Arr::get($info, 'comments', []),
);

$comments = Arr::get($info, 'comments', []);

$albumArtistName = self::getTag($tags, ['albumartist', 'album_artist', 'band']);
Expand All @@ -41,16 +46,28 @@ public static function fromGetId3Info(array $info): self

$path = Arr::get($info, 'filenamepath');

$cover = [self::getTag($comments, 'cover', null)];

if ($cover[0] === null) {
$cover = self::getTag($comments, 'picture', []);
}

$lyrics = html_entity_decode(self::getTag($tags, [
'unsynchronised_lyric',
'unsychronised_lyric',
'unsyncedlyrics',
]));

return new self(
title: html_entity_decode(self::getTag($tags, 'title', pathinfo($path, PATHINFO_FILENAME))),
albumName: html_entity_decode(self::getTag($tags, 'album', Album::UNKNOWN_NAME)),
artistName: html_entity_decode(self::getTag($tags, 'artist', Artist::UNKNOWN_NAME)),
albumArtistName: html_entity_decode($albumArtistName),
track: (int) self::getTag($tags, ['track', 'tracknumber', 'track_number']),
disc: (int) self::getTag($tags, 'part_of_a_set', 1),
lyrics: html_entity_decode(self::getTag($tags, ['unsynchronised_lyric', 'unsychronised_lyric'])),
disc: (int) self::getTag($tags, ['discnumber', 'part_of_a_set'], 1),
lyrics: $lyrics,
length: (float) Arr::get($info, 'playtime_seconds'),
cover: self::getTag($comments, 'picture', []),
cover: $cover,
path: $path,
mTime: Helper::getModifiedTime($path),
);
Expand Down
31 changes: 31 additions & 0 deletions tests/Integration/Services/FileSynchronizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,37 @@ public function testGetFileInfo(): void
self::assertEqualsWithDelta(10, $info->length, 0.1);
}

/** @test */
public function testGetFileInfoVorbisCommentsFlac(): void
{
$flacPath = __DIR__ . '/../../songs/full-vorbis-comments.flac';
$info = $this->fileSynchronizer->setFile($flacPath)->getFileScanInformation();

$expectedData = [
'artist' => 'Koel',
'album' => 'Koel Testing Vol. 1',
'albumartist' => 'Koel',
'title' => 'Amet',
'track' => 5,
'disc' => 3,
'lyrics' => "Foo\r\nbar",
'cover' => [
'data' => file_get_contents(__DIR__ . '/../../blobs/cover.png'),
'image_mime' => 'image/png',
'image_width' => 512,
'image_height' => 512,
'picturetype' => 'Other',
'datalength' => 7627,
],
'path' => realpath($flacPath),
'mtime' => filemtime($flacPath),
];

self::assertArraySubset($expectedData, $info->toArray());
self::assertEqualsWithDelta(10, $info->length, 0.1);
}

/** @test */
public function testSongWithoutTitleHasFileNameAsTitle(): void
{
$this->fileSynchronizer->setFile(__DIR__ . '/../../songs/blank.mp3');
Expand Down
26 changes: 14 additions & 12 deletions tests/Integration/Services/MediaSyncServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,22 +219,24 @@ public function testSyncDeletedDirectoryViaWatch(): void
public function testHtmlEntities(): void
{
$path = $this->path('/songs/blank.mp3');
$analyzed = [
'filenamepath' => $path,
'tags' => [
'id3v2' => [
'title' => ['水谷広実'],
'album' => ['小岩井こ Random'],
'artist' => ['佐倉綾音 Unknown'],
],
],
'encoding' => 'UTF-8',
'playtime_seconds' => 100,
];

$this->swap(
getID3::class,
Mockery::mock(getID3::class, [
'analyze' => [
'filenamepath' => $path,
'tags' => [
'id3v2' => [
'title' => ['水谷広実'],
'album' => ['小岩井こ Random'],
'artist' => ['佐倉綾音 Unknown'],
],
],
'encoding' => 'UTF-8',
'playtime_seconds' => 100,
],
'CopyTagsToComments' => $analyzed,
'analyze' => $analyzed,
])
);

Expand Down
Binary file added tests/songs/full-vorbis-comments.flac
Binary file not shown.

0 comments on commit 4c7e264

Please sign in to comment.