Use music metadata from ffprobe when TagLib fails

TagLib has its own limitations, which cause it to fail on certain audio files or extract incomplete information from the tags. Use the information from ffprobe when TagLib fails to extract data.

Signed-off-by: gnattu <gnattuoc@me.com>
This commit is contained in:
gnattu 2024-05-28 14:24:36 +08:00
parent 730b01fb14
commit e8d1ee0934

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -150,10 +151,12 @@ namespace MediaBrowser.Providers.MediaInfo
/// <param name="options">The <see cref="MetadataRefreshOptions"/>.</param> /// <param name="options">The <see cref="MetadataRefreshOptions"/>.</param>
/// <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;
try
{ {
using var file = TagLib.File.Create(audio.Path); using var file = TagLib.File.Create(audio.Path);
var tagTypes = file.TagTypesOnDisk; var tagTypes = file.TagTypesOnDisk;
Tag? tags = null;
if (tagTypes.HasFlag(TagTypes.Id3v2)) if (tagTypes.HasFlag(TagTypes.Id3v2))
{ {
@ -183,9 +186,24 @@ namespace MediaBrowser.Providers.MediaInfo
{ {
tags = file.GetTag(TagTypes.Id3v1); tags = file.GetTag(TagTypes.Id3v1);
} }
}
if (tags is not null) catch (Exception e)
{ {
_logger.LogWarning("TagLib-Sharp does not support this audio: {Exception}", e);
}
finally
{
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>();
@ -287,7 +305,7 @@ 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, tags.Year);
} }
} }
} }
@ -343,7 +361,6 @@ namespace MediaBrowser.Providers.MediaInfo
await _lyricManager.SaveLyricAsync(audio, "lrc", tags.Lyrics).ConfigureAwait(false); await _lyricManager.SaveLyricAsync(audio, "lrc", tags.Lyrics).ConfigureAwait(false);
} }
} }
}
private void AddExternalLyrics( private void AddExternalLyrics(
Audio audio, Audio audio,