Migrate to z440.atl instead of TagLib-Sharp

The ATL lib provides a lot of advantages to the TagLib we are currently using.

Notably:

- auto-detect the format of the audio data, even if the file extension has the wrong label, and provides unified API for different file types.

- supports more audio formats than TagLib

- supports lyrics natively

- supports playlists and cuesheets

- srovides relatively simple and controllable way for non-standard fields, enable us to implement compatibility features instead of waiting for lib updates

- is actually maintained

Signed-off-by: gnattu <gnattuoc@me.com>
pull/11879/head
gnattu 8 months ago
parent 095d4d4d15
commit 172feab084

@ -81,6 +81,7 @@
<PackageVersion Include="System.Text.Json" Version="8.0.4" /> <PackageVersion Include="System.Text.Json" Version="8.0.4" />
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="8.0.1" /> <PackageVersion Include="System.Threading.Tasks.Dataflow" Version="8.0.1" />
<PackageVersion Include="TagLibSharp" Version="2.3.0" /> <PackageVersion Include="TagLibSharp" Version="2.3.0" />
<PackageVersion Include="z440.atl.core" Version="5.24.0" />
<PackageVersion Include="TMDbLib" Version="2.2.0" /> <PackageVersion Include="TMDbLib" Version="2.2.0" />
<PackageVersion Include="UTF.Unknown" Version="2.5.1" /> <PackageVersion Include="UTF.Unknown" Version="2.5.1" />
<PackageVersion Include="Xunit.Priority" Version="1.1.6" /> <PackageVersion Include="Xunit.Priority" Version="1.1.6" />

@ -23,7 +23,7 @@
<PackageReference Include="Microsoft.Extensions.Http" /> <PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Newtonsoft.Json" /> <PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="PlaylistsNET" /> <PackageReference Include="PlaylistsNET" />
<PackageReference Include="TagLibSharp" /> <PackageReference Include="z440.atl.core"/>
<PackageReference Include="TMDbLib" /> <PackageReference Include="TMDbLib" />
</ItemGroup> </ItemGroup>

@ -4,6 +4,7 @@ using System.Globalization;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using ATL;
using Jellyfin.Data.Enums; using Jellyfin.Data.Enums;
using Jellyfin.Extensions; using Jellyfin.Extensions;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
@ -18,7 +19,6 @@ using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.MediaInfo; using MediaBrowser.Model.MediaInfo;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using TagLib;
namespace MediaBrowser.Providers.MediaInfo namespace MediaBrowser.Providers.MediaInfo
{ {
@ -127,7 +127,6 @@ namespace MediaBrowser.Providers.MediaInfo
audio.RunTimeTicks = mediaInfo.RunTimeTicks; audio.RunTimeTicks = mediaInfo.RunTimeTicks;
audio.Size = mediaInfo.Size; audio.Size = mediaInfo.Size;
audio.PremiereDate = mediaInfo.PremiereDate;
// Add external lyrics first to prevent the lrc file get overwritten on first scan // Add external lyrics first to prevent the lrc file get overwritten on first scan
var mediaStreams = new List<MediaStream>(mediaInfo.MediaStreams); var mediaStreams = new List<MediaStream>(mediaInfo.MediaStreams);
@ -157,60 +156,19 @@ namespace MediaBrowser.Providers.MediaInfo
/// <param name="tryExtractEmbeddedLyrics">Whether to extract embedded lyrics to lrc file. </param> /// <param name="tryExtractEmbeddedLyrics">Whether to extract embedded lyrics to lrc file. </param>
private async Task FetchDataFromTags(Audio audio, Model.MediaInfo.MediaInfo mediaInfo, MetadataRefreshOptions options, bool tryExtractEmbeddedLyrics) private async Task FetchDataFromTags(Audio audio, Model.MediaInfo.MediaInfo mediaInfo, MetadataRefreshOptions options, bool tryExtractEmbeddedLyrics)
{ {
Tag? tags = null; ATL.Settings.DisplayValueSeparator = '\u001F';
try Track track = new Track(audio.Path);
{
using var file = TagLib.File.Create(audio.Path);
var tagTypes = file.TagTypesOnDisk;
if (tagTypes.HasFlag(TagTypes.Id3v2)) track.Album = string.IsNullOrEmpty(track.Album) ? mediaInfo.Album : track.Album;
{ track.Title = string.IsNullOrEmpty(track.Title) ? mediaInfo.Name : track.Title;
tags = file.GetTag(TagTypes.Id3v2); track.Year ??= mediaInfo.ProductionYear;
} track.TrackNumber ??= mediaInfo.IndexNumber;
else if (tagTypes.HasFlag(TagTypes.Ape)) track.DiscNumber ??= mediaInfo.ParentIndexNumber;
{
tags = file.GetTag(TagTypes.Ape);
}
else if (tagTypes.HasFlag(TagTypes.FlacMetadata))
{
tags = file.GetTag(TagTypes.FlacMetadata);
}
else if (tagTypes.HasFlag(TagTypes.Apple))
{
tags = file.GetTag(TagTypes.Apple);
}
else if (tagTypes.HasFlag(TagTypes.Xiph))
{
tags = file.GetTag(TagTypes.Xiph);
}
else if (tagTypes.HasFlag(TagTypes.AudibleMetadata))
{
tags = file.GetTag(TagTypes.AudibleMetadata);
}
else if (tagTypes.HasFlag(TagTypes.Id3v1))
{
tags = file.GetTag(TagTypes.Id3v1);
}
}
catch (Exception e)
{
_logger.LogWarning(e, "TagLib-Sharp does not support this audio");
}
tags ??= new TagLib.Id3v2.Tag();
tags.AlbumArtists ??= mediaInfo.AlbumArtists;
tags.Album ??= mediaInfo.Album;
tags.Title ??= mediaInfo.Name;
tags.Year = tags.Year == 0U ? Convert.ToUInt32(mediaInfo.ProductionYear, CultureInfo.InvariantCulture) : tags.Year;
tags.Performers ??= mediaInfo.Artists;
tags.Genres ??= mediaInfo.Genres;
tags.Track = tags.Track == 0U ? Convert.ToUInt32(mediaInfo.IndexNumber, CultureInfo.InvariantCulture) : tags.Track;
tags.Disc = tags.Disc == 0U ? Convert.ToUInt32(mediaInfo.ParentIndexNumber, CultureInfo.InvariantCulture) : tags.Disc;
if (audio.SupportsPeople && !audio.LockedFields.Contains(MetadataField.Cast)) if (audio.SupportsPeople && !audio.LockedFields.Contains(MetadataField.Cast))
{ {
var people = new List<PersonInfo>(); var people = new List<PersonInfo>();
var albumArtists = tags.AlbumArtists; var albumArtists = string.IsNullOrEmpty(track.AlbumArtist) ? mediaInfo.AlbumArtists : track.AlbumArtist.Split('\u001F');
foreach (var albumArtist in albumArtists) foreach (var albumArtist in albumArtists)
{ {
if (!string.IsNullOrEmpty(albumArtist)) if (!string.IsNullOrEmpty(albumArtist))
@ -223,7 +181,7 @@ namespace MediaBrowser.Providers.MediaInfo
} }
} }
var performers = tags.Performers; var performers = string.IsNullOrEmpty(track.Artist) ? mediaInfo.Artists : track.Artist.Split('\u001F');
foreach (var performer in performers) foreach (var performer in performers)
{ {
if (!string.IsNullOrEmpty(performer)) if (!string.IsNullOrEmpty(performer))
@ -236,7 +194,7 @@ namespace MediaBrowser.Providers.MediaInfo
} }
} }
foreach (var composer in tags.Composers) foreach (var composer in track.Composer.Split('\u001F'))
{ {
if (!string.IsNullOrEmpty(composer)) if (!string.IsNullOrEmpty(composer))
{ {
@ -277,27 +235,32 @@ namespace MediaBrowser.Providers.MediaInfo
} }
} }
if (!audio.LockedFields.Contains(MetadataField.Name) && !string.IsNullOrEmpty(tags.Title)) if (!audio.LockedFields.Contains(MetadataField.Name) && !string.IsNullOrEmpty(track.Title))
{ {
audio.Name = tags.Title; audio.Name = track.Title;
} }
if (options.ReplaceAllMetadata) if (options.ReplaceAllMetadata)
{ {
audio.Album = tags.Album; audio.Album = track.Album;
audio.IndexNumber = Convert.ToInt32(tags.Track); audio.IndexNumber = track.TrackNumber;
audio.ParentIndexNumber = Convert.ToInt32(tags.Disc); audio.ParentIndexNumber = track.DiscNumber;
} }
else else
{ {
audio.Album ??= tags.Album; audio.Album ??= track.Album;
audio.IndexNumber ??= Convert.ToInt32(tags.Track); audio.IndexNumber ??= track.TrackNumber;
audio.ParentIndexNumber ??= Convert.ToInt32(tags.Disc); audio.ParentIndexNumber ??= track.DiscNumber;
}
if (track.Date.HasValue)
{
audio.PremiereDate = track.Date;
} }
if (tags.Year != 0) if (track.Year.HasValue)
{ {
var year = Convert.ToInt32(tags.Year); var year = track.Year.Value;
audio.ProductionYear = year; audio.ProductionYear = year;
if (!audio.PremiereDate.HasValue) if (!audio.PremiereDate.HasValue)
@ -308,57 +271,121 @@ namespace MediaBrowser.Providers.MediaInfo
} }
catch (ArgumentOutOfRangeException ex) catch (ArgumentOutOfRangeException ex)
{ {
_logger.LogError(ex, "Error parsing YEAR tag in {File}. '{TagValue}' is an invalid year", audio.Path, tags.Year); _logger.LogError(ex, "Error parsing YEAR tag in {File}. '{TagValue}' is an invalid year", audio.Path, track.Year);
} }
} }
} }
if (!audio.LockedFields.Contains(MetadataField.Genres)) if (!audio.LockedFields.Contains(MetadataField.Genres))
{ {
var genres = string.IsNullOrEmpty(track.Genre) ? mediaInfo.Genres : track.Genre.Split('\u001F').Distinct(StringComparer.OrdinalIgnoreCase).ToArray();
audio.Genres = options.ReplaceAllMetadata || audio.Genres == null || audio.Genres.Length == 0 audio.Genres = options.ReplaceAllMetadata || audio.Genres == null || audio.Genres.Length == 0
? tags.Genres.Distinct(StringComparer.OrdinalIgnoreCase).ToArray() ? genres
: audio.Genres; : audio.Genres;
} }
if (!double.IsNaN(tags.ReplayGainTrackGain)) track.AdditionalFields.TryGetValue("REPLAYGAIN_TRACK_GAIN", out var trackGainTag);
float trackGain = float.NaN;
if (trackGainTag is not null)
{ {
audio.NormalizationGain = (float)tags.ReplayGainTrackGain; if (trackGainTag.ToLower(CultureInfo.InvariantCulture).EndsWith("db", StringComparison.OrdinalIgnoreCase))
{
trackGainTag = trackGainTag[..^2].Trim();
} }
if (options.ReplaceAllMetadata || !audio.HasProviderId(MetadataProvider.MusicBrainzArtist)) if (float.TryParse(trackGainTag, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
{ {
audio.TrySetProviderId(MetadataProvider.MusicBrainzArtist, tags.MusicBrainzArtistId); trackGain = value;
}
} }
if (options.ReplaceAllMetadata || !audio.HasProviderId(MetadataProvider.MusicBrainzAlbumArtist)) if (!float.IsNaN(trackGain))
{ {
audio.TrySetProviderId(MetadataProvider.MusicBrainzAlbumArtist, tags.MusicBrainzReleaseArtistId); audio.NormalizationGain = trackGain;
} }
if (options.ReplaceAllMetadata || !audio.HasProviderId(MetadataProvider.MusicBrainzAlbum)) if ((options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzArtist, out _))
&& !string.IsNullOrEmpty(tags.MusicBrainzArtistId))
{ {
audio.TrySetProviderId(MetadataProvider.MusicBrainzAlbum, tags.MusicBrainzReleaseId); track.AdditionalFields.TryGetValue("MUSICBRAINZ_ARTISTID", out var musicBrainzArtistTag);
if (musicBrainzArtistTag is null)
{
track.AdditionalFields.TryGetValue("MusicBrainz Artist Id", out musicBrainzArtistTag);
} }
if (options.ReplaceAllMetadata || !audio.HasProviderId(MetadataProvider.MusicBrainzReleaseGroup)) if (musicBrainzArtistTag is not null)
{ {
audio.TrySetProviderId(MetadataProvider.MusicBrainzReleaseGroup, tags.MusicBrainzReleaseGroupId); audio.TrySetProviderId(MetadataProvider.MusicBrainzArtist, musicBrainzArtistTag);
}
} }
if (options.ReplaceAllMetadata || !audio.HasProviderId(MetadataProvider.MusicBrainzTrack)) if ((options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzAlbumArtist, out _))
&& !string.IsNullOrEmpty(tags.MusicBrainzReleaseArtistId))
{
track.AdditionalFields.TryGetValue("MUSICBRAINZ_ALBUMARTISTID", out var musicBrainzReleaseArtistIdTag);
if (musicBrainzReleaseArtistIdTag is null)
{
track.AdditionalFields.TryGetValue("MusicBrainz Album Artist Id", out musicBrainzReleaseArtistIdTag);
}
if (musicBrainzReleaseArtistIdTag is not null)
{
audio.TrySetProviderId(MetadataProvider.MusicBrainzAlbumArtist, musicBrainzReleaseArtistIdTag);
}
}
if ((options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzAlbum, out _))
&& !string.IsNullOrEmpty(tags.MusicBrainzReleaseId))
{
track.AdditionalFields.TryGetValue("MUSICBRAINZ_ALBUMID", out var musicBrainzReleaseIdTag);
if (musicBrainzReleaseIdTag is null)
{
track.AdditionalFields.TryGetValue("MusicBrainz Album Id", out musicBrainzReleaseIdTag);
}
if (musicBrainzReleaseIdTag is not null)
{
audio.TrySetProviderId(MetadataProvider.MusicBrainzAlbum, musicBrainzReleaseIdTag);
}
}
if ((options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzReleaseGroup, out _))
&& !string.IsNullOrEmpty(tags.MusicBrainzReleaseGroupId))
{
track.AdditionalFields.TryGetValue("MUSICBRAINZ_RELEASEGROUPID", out var musicBrainzReleaseGroupIdTag);
if (musicBrainzReleaseGroupIdTag is null)
{
track.AdditionalFields.TryGetValue("MusicBrainz Release Group Id", out musicBrainzReleaseGroupIdTag);
}
if (musicBrainzReleaseGroupIdTag is not null)
{
audio.TrySetProviderId(MetadataProvider.MusicBrainzReleaseGroup, musicBrainzReleaseGroupIdTag);
}
}
if (options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzTrack, out _))
{
track.AdditionalFields.TryGetValue("MUSICBRAINZ_RELEASETRACKID", out var trackMbId);
if (trackMbId is null)
{
track.AdditionalFields.TryGetValue("MusicBrainz Release Track Id", out trackMbId);
}
if (trackMbId is not null)
{ {
// Fallback to ffprobe as TagLib incorrectly provides recording MBID in `tags.MusicBrainzTrackId`.
// See https://github.com/mono/taglib-sharp/issues/304
var trackMbId = mediaInfo.GetProviderId(MetadataProvider.MusicBrainzTrack);
audio.TrySetProviderId(MetadataProvider.MusicBrainzTrack, trackMbId); audio.TrySetProviderId(MetadataProvider.MusicBrainzTrack, trackMbId);
} }
}
// Save extracted lyrics if they exist, // Save extracted lyrics if they exist,
// and if the audio doesn't yet have lyrics. // and if the audio doesn't yet have lyrics.
if (!string.IsNullOrWhiteSpace(tags.Lyrics) var lyrics = track.Lyrics.SynchronizedLyrics.Count > 0 ? track.Lyrics.FormatSynchToLRC() : track.Lyrics.UnsynchronizedLyrics;
if (!string.IsNullOrWhiteSpace(lyrics)
&& tryExtractEmbeddedLyrics) && tryExtractEmbeddedLyrics)
{ {
await _lyricManager.SaveLyricAsync(audio, "lrc", tags.Lyrics).ConfigureAwait(false); await _lyricManager.SaveLyricAsync(audio, "lrc", lyrics).ConfigureAwait(false);
} }
} }

Loading…
Cancel
Save