From 172feab084c062355677f58c2da3f35f348b0d04 Mon Sep 17 00:00:00 2001 From: gnattu Date: Thu, 30 May 2024 16:09:50 +0800 Subject: [PATCH 1/8] 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 --- Directory.Packages.props | 1 + .../MediaBrowser.Providers.csproj | 2 +- .../MediaInfo/AudioFileProber.cs | 191 ++++++++++-------- 3 files changed, 111 insertions(+), 83 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 5242126a3..b2848c538 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -81,6 +81,7 @@ + diff --git a/MediaBrowser.Providers/MediaBrowser.Providers.csproj b/MediaBrowser.Providers/MediaBrowser.Providers.csproj index dfb6319ac..9a65852f0 100644 --- a/MediaBrowser.Providers/MediaBrowser.Providers.csproj +++ b/MediaBrowser.Providers/MediaBrowser.Providers.csproj @@ -23,7 +23,7 @@ - + diff --git a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs index fbafd5518..1e7ec36e5 100644 --- a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs +++ b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs @@ -4,6 +4,7 @@ using System.Globalization; using System.Linq; using System.Threading; using System.Threading.Tasks; +using ATL; using Jellyfin.Data.Enums; using Jellyfin.Extensions; using MediaBrowser.Controller.Entities; @@ -18,7 +19,6 @@ using MediaBrowser.Model.Dto; using MediaBrowser.Model.Entities; using MediaBrowser.Model.MediaInfo; using Microsoft.Extensions.Logging; -using TagLib; namespace MediaBrowser.Providers.MediaInfo { @@ -127,7 +127,6 @@ namespace MediaBrowser.Providers.MediaInfo audio.RunTimeTicks = mediaInfo.RunTimeTicks; audio.Size = mediaInfo.Size; - audio.PremiereDate = mediaInfo.PremiereDate; // Add external lyrics first to prevent the lrc file get overwritten on first scan var mediaStreams = new List(mediaInfo.MediaStreams); @@ -157,60 +156,19 @@ namespace MediaBrowser.Providers.MediaInfo /// Whether to extract embedded lyrics to lrc file. private async Task FetchDataFromTags(Audio audio, Model.MediaInfo.MediaInfo mediaInfo, MetadataRefreshOptions options, bool tryExtractEmbeddedLyrics) { - Tag? tags = null; - try - { - using var file = TagLib.File.Create(audio.Path); - var tagTypes = file.TagTypesOnDisk; + ATL.Settings.DisplayValueSeparator = '\u001F'; + Track track = new Track(audio.Path); - if (tagTypes.HasFlag(TagTypes.Id3v2)) - { - tags = file.GetTag(TagTypes.Id3v2); - } - else if (tagTypes.HasFlag(TagTypes.Ape)) - { - 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; + track.Album = string.IsNullOrEmpty(track.Album) ? mediaInfo.Album : track.Album; + track.Title = string.IsNullOrEmpty(track.Title) ? mediaInfo.Name : track.Title; + track.Year ??= mediaInfo.ProductionYear; + track.TrackNumber ??= mediaInfo.IndexNumber; + track.DiscNumber ??= mediaInfo.ParentIndexNumber; if (audio.SupportsPeople && !audio.LockedFields.Contains(MetadataField.Cast)) { var people = new List(); - var albumArtists = tags.AlbumArtists; + var albumArtists = string.IsNullOrEmpty(track.AlbumArtist) ? mediaInfo.AlbumArtists : track.AlbumArtist.Split('\u001F'); foreach (var albumArtist in albumArtists) { 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) { 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)) { @@ -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) { - audio.Album = tags.Album; - audio.IndexNumber = Convert.ToInt32(tags.Track); - audio.ParentIndexNumber = Convert.ToInt32(tags.Disc); + audio.Album = track.Album; + audio.IndexNumber = track.TrackNumber; + audio.ParentIndexNumber = track.DiscNumber; } else { - audio.Album ??= tags.Album; - audio.IndexNumber ??= Convert.ToInt32(tags.Track); - audio.ParentIndexNumber ??= Convert.ToInt32(tags.Disc); + audio.Album ??= track.Album; + audio.IndexNumber ??= track.TrackNumber; + audio.ParentIndexNumber ??= track.DiscNumber; } - if (tags.Year != 0) + if (track.Date.HasValue) { - var year = Convert.ToInt32(tags.Year); + audio.PremiereDate = track.Date; + } + + if (track.Year.HasValue) + { + var year = track.Year.Value; audio.ProductionYear = year; if (!audio.PremiereDate.HasValue) @@ -308,57 +271,121 @@ namespace MediaBrowser.Providers.MediaInfo } 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)) { + 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 - ? tags.Genres.Distinct(StringComparer.OrdinalIgnoreCase).ToArray() + ? 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 (float.TryParse(trackGainTag, NumberStyles.Float, CultureInfo.InvariantCulture, out var value)) + { + trackGain = value; + } } - if (options.ReplaceAllMetadata || !audio.HasProviderId(MetadataProvider.MusicBrainzArtist)) + if (!float.IsNaN(trackGain)) { - audio.TrySetProviderId(MetadataProvider.MusicBrainzArtist, tags.MusicBrainzArtistId); + audio.NormalizationGain = trackGain; } - if (options.ReplaceAllMetadata || !audio.HasProviderId(MetadataProvider.MusicBrainzAlbumArtist)) + if ((options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzArtist, out _)) + && !string.IsNullOrEmpty(tags.MusicBrainzArtistId)) { - audio.TrySetProviderId(MetadataProvider.MusicBrainzAlbumArtist, tags.MusicBrainzReleaseArtistId); + track.AdditionalFields.TryGetValue("MUSICBRAINZ_ARTISTID", out var musicBrainzArtistTag); + if (musicBrainzArtistTag is null) + { + track.AdditionalFields.TryGetValue("MusicBrainz Artist Id", out musicBrainzArtistTag); + } + + if (musicBrainzArtistTag is not null) + { + audio.TrySetProviderId(MetadataProvider.MusicBrainzArtist, musicBrainzArtistTag); + } } - if (options.ReplaceAllMetadata || !audio.HasProviderId(MetadataProvider.MusicBrainzAlbum)) + if ((options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzAlbumArtist, out _)) + && !string.IsNullOrEmpty(tags.MusicBrainzReleaseArtistId)) { - audio.TrySetProviderId(MetadataProvider.MusicBrainzAlbum, tags.MusicBrainzReleaseId); + 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.HasProviderId(MetadataProvider.MusicBrainzReleaseGroup)) + if ((options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzAlbum, out _)) + && !string.IsNullOrEmpty(tags.MusicBrainzReleaseId)) { - audio.TrySetProviderId(MetadataProvider.MusicBrainzReleaseGroup, tags.MusicBrainzReleaseGroupId); + 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.HasProviderId(MetadataProvider.MusicBrainzTrack)) + if ((options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzReleaseGroup, out _)) + && !string.IsNullOrEmpty(tags.MusicBrainzReleaseGroupId)) { - // 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); + 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) + { + audio.TrySetProviderId(MetadataProvider.MusicBrainzTrack, trackMbId); + } } // Save extracted lyrics if they exist, // 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) { - await _lyricManager.SaveLyricAsync(audio, "lrc", tags.Lyrics).ConfigureAwait(false); + await _lyricManager.SaveLyricAsync(audio, "lrc", lyrics).ConfigureAwait(false); } } From ac9322370b441d26fa040e91d1774df6628fce88 Mon Sep 17 00:00:00 2001 From: gnattu Date: Thu, 30 May 2024 18:59:26 +0800 Subject: [PATCH 2/8] Check if the metadata is supported for title fallback Signed-off-by: gnattu --- MediaBrowser.Providers/MediaInfo/AudioFileProber.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs index 1e7ec36e5..7a1d04b3a 100644 --- a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs +++ b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs @@ -159,8 +159,13 @@ namespace MediaBrowser.Providers.MediaInfo ATL.Settings.DisplayValueSeparator = '\u001F'; Track track = new Track(audio.Path); + // ATL will fall back to filename as title when it does not understand the metadata + if (track.MetadataFormats.All(mf => mf.Equals(ATL.Factory.UNKNOWN_FORMAT))) + { + track.Title = mediaInfo.Name; + } + track.Album = string.IsNullOrEmpty(track.Album) ? mediaInfo.Album : track.Album; - track.Title = string.IsNullOrEmpty(track.Title) ? mediaInfo.Name : track.Title; track.Year ??= mediaInfo.ProductionYear; track.TrackNumber ??= mediaInfo.IndexNumber; track.DiscNumber ??= mediaInfo.ParentIndexNumber; From 507f89b8ed23449006c66a2338fe767d178ee13c Mon Sep 17 00:00:00 2001 From: gnattu Date: Sun, 2 Jun 2024 15:18:32 +0800 Subject: [PATCH 3/8] Bump to v5.25.0 Signed-off-by: gnattu --- Directory.Packages.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index b2848c538..f9887dc79 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -81,7 +81,7 @@ - + From 939e02cceef9ccbd2b362a39ab10f026d452eb1b Mon Sep 17 00:00:00 2001 From: gnattu Date: Wed, 17 Jul 2024 03:29:16 +0800 Subject: [PATCH 4/8] Apply suggestions from code review Co-authored-by: Cody Robibero --- .../MediaInfo/AudioFileProber.cs | 50 +++++++------------ 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs index 7a1d04b3a..250717205 100644 --- a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs +++ b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs @@ -294,7 +294,7 @@ namespace MediaBrowser.Providers.MediaInfo if (trackGainTag is not null) { - if (trackGainTag.ToLower(CultureInfo.InvariantCulture).EndsWith("db", StringComparison.OrdinalIgnoreCase)) + if (trackGainTag.EndsWith("db", StringComparison.OrdinalIgnoreCase)) { trackGainTag = trackGainTag[..^2].Trim(); } @@ -313,13 +313,9 @@ namespace MediaBrowser.Providers.MediaInfo if ((options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzArtist, out _)) && !string.IsNullOrEmpty(tags.MusicBrainzArtistId)) { - track.AdditionalFields.TryGetValue("MUSICBRAINZ_ARTISTID", out var musicBrainzArtistTag); - if (musicBrainzArtistTag is null) - { - track.AdditionalFields.TryGetValue("MusicBrainz Artist Id", out musicBrainzArtistTag); - } - - if (musicBrainzArtistTag is not null) + if ((track.AdditionalFields.TryGetValue("MUSICBRAINZ_ARTISTID", out var musicBrainzArtistTag) + || track.AdditionalFields.TryGetValue("MusicBrainz Artist Id", out musicBrainzArtistTag)) + && !string.IsNullOrEmpty(musicBrainzArtistTag)) { audio.TrySetProviderId(MetadataProvider.MusicBrainzArtist, musicBrainzArtistTag); } @@ -328,13 +324,9 @@ namespace MediaBrowser.Providers.MediaInfo 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) + if ((track.AdditionalFields.TryGetValue("MUSICBRAINZ_ALBUMARTISTID", out var musicBrainzReleaseArtistIdTag) + || track.AdditionalFields.TryGetValue("MusicBrainz Album Artist Id", out musicBrainzReleaseArtistIdTag)) + && !string.IsNullOrEmpty(musicBrainzReleaseArtistIdTag)) { audio.TrySetProviderId(MetadataProvider.MusicBrainzAlbumArtist, musicBrainzReleaseArtistIdTag); } @@ -343,13 +335,9 @@ namespace MediaBrowser.Providers.MediaInfo 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) + if ((track.AdditionalFields.TryGetValue("MUSICBRAINZ_ALBUMID", out var musicBrainzReleaseIdTag) + || track.AdditionalFields.TryGetValue("MusicBrainz Album Id", out musicBrainzReleaseIdTag)) + && !string.IsNullOrEmpty(musicBrainzReleaseIdTag)) { audio.TrySetProviderId(MetadataProvider.MusicBrainzAlbum, musicBrainzReleaseIdTag); } @@ -358,13 +346,9 @@ namespace MediaBrowser.Providers.MediaInfo 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) + if ((track.AdditionalFields.TryGetValue("MUSICBRAINZ_RELEASEGROUPID", out var musicBrainzReleaseGroupIdTag) + || track.AdditionalFields.TryGetValue("MusicBrainz Release Group Id", out musicBrainzReleaseGroupIdTag)) + && !string.IsNullOrEmpty(musicBrainzReleaseGroupIdTag)) { audio.TrySetProviderId(MetadataProvider.MusicBrainzReleaseGroup, musicBrainzReleaseGroupIdTag); } @@ -372,12 +356,12 @@ namespace MediaBrowser.Providers.MediaInfo if (options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzTrack, out _)) { - track.AdditionalFields.TryGetValue("MUSICBRAINZ_RELEASETRACKID", out var trackMbId); - if (trackMbId is null) + if ((track.AdditionalFields.TryGetValue("MUSICBRAINZ_RELEASETRACKID", out var trackMbId) + || track.AdditionalFields.TryGetValue("MusicBrainz Release Track Id", out trackMbId)) + && !string.IsNullOrEmpty(trackMbId)) { - track.AdditionalFields.TryGetValue("MusicBrainz Release Track Id", out trackMbId); + audio.SetProviderId(MetadataProvider.MusicBrainzTrack, trackMbId); } - if (trackMbId is not null) { audio.TrySetProviderId(MetadataProvider.MusicBrainzTrack, trackMbId); From 1d658a5a4de0332987c468dc9eb13fe90a08b469 Mon Sep 17 00:00:00 2001 From: gnattu Date: Wed, 17 Jul 2024 03:31:03 +0800 Subject: [PATCH 5/8] Remove redundant check Signed-off-by: gnattu --- MediaBrowser.Providers/MediaInfo/AudioFileProber.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs index 250717205..40ff5551d 100644 --- a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs +++ b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs @@ -359,10 +359,6 @@ namespace MediaBrowser.Providers.MediaInfo if ((track.AdditionalFields.TryGetValue("MUSICBRAINZ_RELEASETRACKID", out var trackMbId) || track.AdditionalFields.TryGetValue("MusicBrainz Release Track Id", out trackMbId)) && !string.IsNullOrEmpty(trackMbId)) - { - audio.SetProviderId(MetadataProvider.MusicBrainzTrack, trackMbId); - } - if (trackMbId is not null) { audio.TrySetProviderId(MetadataProvider.MusicBrainzTrack, trackMbId); } From 56a98a3bb0cc6efcc0d533164aeb8879045d646f Mon Sep 17 00:00:00 2001 From: gnattu Date: Wed, 17 Jul 2024 03:40:07 +0800 Subject: [PATCH 6/8] Make internal value separator a constant Signed-off-by: gnattu --- MediaBrowser.Providers/MediaInfo/AudioFileProber.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs index 40ff5551d..13e5310de 100644 --- a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs +++ b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs @@ -27,6 +27,7 @@ namespace MediaBrowser.Providers.MediaInfo /// public class AudioFileProber { + private const char InternalValueSeparator = '\u001F'; private readonly IMediaEncoder _mediaEncoder; private readonly IItemRepository _itemRepo; private readonly ILibraryManager _libraryManager; @@ -61,6 +62,7 @@ namespace MediaBrowser.Providers.MediaInfo _mediaSourceManager = mediaSourceManager; _lyricResolver = lyricResolver; _lyricManager = lyricManager; + ATL.Settings.DisplayValueSeparator = InternalValueSeparator; } /// @@ -156,7 +158,7 @@ namespace MediaBrowser.Providers.MediaInfo /// Whether to extract embedded lyrics to lrc file. private async Task FetchDataFromTags(Audio audio, Model.MediaInfo.MediaInfo mediaInfo, MetadataRefreshOptions options, bool tryExtractEmbeddedLyrics) { - ATL.Settings.DisplayValueSeparator = '\u001F'; + var test = ATL.Settings.DisplayValueSeparator; Track track = new Track(audio.Path); // ATL will fall back to filename as title when it does not understand the metadata @@ -173,7 +175,7 @@ namespace MediaBrowser.Providers.MediaInfo if (audio.SupportsPeople && !audio.LockedFields.Contains(MetadataField.Cast)) { var people = new List(); - var albumArtists = string.IsNullOrEmpty(track.AlbumArtist) ? mediaInfo.AlbumArtists : track.AlbumArtist.Split('\u001F'); + var albumArtists = string.IsNullOrEmpty(track.AlbumArtist) ? mediaInfo.AlbumArtists : track.AlbumArtist.Split(InternalValueSeparator); foreach (var albumArtist in albumArtists) { if (!string.IsNullOrEmpty(albumArtist)) @@ -186,7 +188,7 @@ namespace MediaBrowser.Providers.MediaInfo } } - var performers = string.IsNullOrEmpty(track.Artist) ? mediaInfo.Artists : track.Artist.Split('\u001F'); + var performers = string.IsNullOrEmpty(track.Artist) ? mediaInfo.Artists : track.Artist.Split(InternalValueSeparator); foreach (var performer in performers) { if (!string.IsNullOrEmpty(performer)) @@ -199,7 +201,7 @@ namespace MediaBrowser.Providers.MediaInfo } } - foreach (var composer in track.Composer.Split('\u001F')) + foreach (var composer in track.Composer.Split(InternalValueSeparator)) { if (!string.IsNullOrEmpty(composer)) { @@ -283,7 +285,7 @@ namespace MediaBrowser.Providers.MediaInfo if (!audio.LockedFields.Contains(MetadataField.Genres)) { - var genres = string.IsNullOrEmpty(track.Genre) ? mediaInfo.Genres : track.Genre.Split('\u001F').Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); + var genres = string.IsNullOrEmpty(track.Genre) ? mediaInfo.Genres : track.Genre.Split(InternalValueSeparator).Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); audio.Genres = options.ReplaceAllMetadata || audio.Genres == null || audio.Genres.Length == 0 ? genres : audio.Genres; From 5c5b326b1aa794e3956cc81c08dd380ac9fd9ae0 Mon Sep 17 00:00:00 2001 From: gnattu Date: Wed, 17 Jul 2024 20:33:49 +0800 Subject: [PATCH 7/8] Remove test var Co-authored-by: Cody Robibero --- MediaBrowser.Providers/MediaInfo/AudioFileProber.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs index 13e5310de..5270b1c3e 100644 --- a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs +++ b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs @@ -158,7 +158,6 @@ namespace MediaBrowser.Providers.MediaInfo /// Whether to extract embedded lyrics to lrc file. private async Task FetchDataFromTags(Audio audio, Model.MediaInfo.MediaInfo mediaInfo, MetadataRefreshOptions options, bool tryExtractEmbeddedLyrics) { - var test = ATL.Settings.DisplayValueSeparator; Track track = new Track(audio.Path); // ATL will fall back to filename as title when it does not understand the metadata From 79c4469ac7ac936ded42e0d855df10a0a7d609e2 Mon Sep 17 00:00:00 2001 From: gnattu Date: Thu, 18 Jul 2024 01:59:16 +0800 Subject: [PATCH 8/8] Remove redundant NaN check Signed-off-by: gnattu --- .../MediaInfo/AudioFileProber.cs | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs index 5270b1c3e..7e0773b6d 100644 --- a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs +++ b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs @@ -291,7 +291,6 @@ namespace MediaBrowser.Providers.MediaInfo } track.AdditionalFields.TryGetValue("REPLAYGAIN_TRACK_GAIN", out var trackGainTag); - float trackGain = float.NaN; if (trackGainTag is not null) { @@ -302,17 +301,11 @@ namespace MediaBrowser.Providers.MediaInfo if (float.TryParse(trackGainTag, NumberStyles.Float, CultureInfo.InvariantCulture, out var value)) { - trackGain = value; + audio.NormalizationGain = value; } } - if (!float.IsNaN(trackGain)) - { - audio.NormalizationGain = trackGain; - } - - if ((options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzArtist, out _)) - && !string.IsNullOrEmpty(tags.MusicBrainzArtistId)) + if (options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzArtist, out _)) { if ((track.AdditionalFields.TryGetValue("MUSICBRAINZ_ARTISTID", out var musicBrainzArtistTag) || track.AdditionalFields.TryGetValue("MusicBrainz Artist Id", out musicBrainzArtistTag)) @@ -322,8 +315,7 @@ namespace MediaBrowser.Providers.MediaInfo } } - if ((options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzAlbumArtist, out _)) - && !string.IsNullOrEmpty(tags.MusicBrainzReleaseArtistId)) + if (options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzAlbumArtist, out _)) { if ((track.AdditionalFields.TryGetValue("MUSICBRAINZ_ALBUMARTISTID", out var musicBrainzReleaseArtistIdTag) || track.AdditionalFields.TryGetValue("MusicBrainz Album Artist Id", out musicBrainzReleaseArtistIdTag)) @@ -333,8 +325,7 @@ namespace MediaBrowser.Providers.MediaInfo } } - if ((options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzAlbum, out _)) - && !string.IsNullOrEmpty(tags.MusicBrainzReleaseId)) + if (options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzAlbum, out _)) { if ((track.AdditionalFields.TryGetValue("MUSICBRAINZ_ALBUMID", out var musicBrainzReleaseIdTag) || track.AdditionalFields.TryGetValue("MusicBrainz Album Id", out musicBrainzReleaseIdTag)) @@ -344,8 +335,7 @@ namespace MediaBrowser.Providers.MediaInfo } } - if ((options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzReleaseGroup, out _)) - && !string.IsNullOrEmpty(tags.MusicBrainzReleaseGroupId)) + if (options.ReplaceAllMetadata || !audio.TryGetProviderId(MetadataProvider.MusicBrainzReleaseGroup, out _)) { if ((track.AdditionalFields.TryGetValue("MUSICBRAINZ_RELEASEGROUPID", out var musicBrainzReleaseGroupIdTag) || track.AdditionalFields.TryGetValue("MusicBrainz Release Group Id", out musicBrainzReleaseGroupIdTag))