Captures the audio container of the music tracks

Adds a Container to the audio metadata, captured with TagLibSharp from the concrete type of the parsed file during the music library scan and stored on the track through a new column and an EF migration. The container is refreshed when a file changes, survives track and artist edits and provider enrichment, and is exposed read-only in the track responses, never settable through a request.

Extracts the container resolution into a testable resolver used by the scan pipeline and the ID3 plugin, refuses to read a file that is itself a reparse point during extraction, and updates the endpoint summaries, the API and domain documentation, and the tests.
This commit is contained in:
BinaryAlley committed 2026-10-14 13:40:28 +03:00
1 parent 4ffa81fffe
commit f29f89f31f
60 files changed
+4483 -68

No files matched your search

@@ -94,6 +94,11 @@ public class MusicLibraryScanItemMetadataEntity : IStorageEntity
/// </summary>
public string? AudioCodec { get; set; }
/// <summary>
/// Gets or sets the container of the audio file, for example MP3, FLAC, OGG, AAC, MP4 or WAV, if applicable.
/// </summary>
public string? Container { get; set; }
/// <summary>
/// Gets or sets the bitrate of the audio in kbps, if applicable.
/// </summary>
@@ -141,6 +141,11 @@ public class TrackEntity : IStorageEntity, IAuditableEntity
/// </summary>
public string? AudioCodec { get; set; }
/// <summary>
/// Gets or sets the container of the audio file, for example MP3, FLAC, OGG, AAC, MP4 or WAV, if applicable.
/// </summary>
public string? Container { get; set; }
/// <summary>
/// Gets or sets the bitrate of the audio in kbps, if applicable.
/// </summary>
@@ -202,6 +202,9 @@ public static class AudioMetadataDtoMapping
Optional<string> audioCodec = Optional<string>.FromNullable(dto.AudioCodec);
if (!audioCodec.HasValue)
audioCodec = track.Metadata.AudioCodec;
// The container is captured from the file itself: a provider that re-reads the file supplies it, and when it is absent the track keeps the stored
// container, which the domain enforces when the metadata is applied. The preserve rule therefore lives in one place, the domain update.
Optional<string> container = Optional<string>.FromNullable(dto.Container);
Optional<int> bitrate = Optional<int>.FromNullable(dto.Bitrate);
if (!bitrate.HasValue)
bitrate = track.Metadata.Bitrate;
@@ -220,6 +223,7 @@ public static class AudioMetadataDtoMapping
originalLanguage,
bitDepth,
audioCodec,
container,
bitrate,
track.Metadata.AcoustId,
track.Metadata.ReplayGainTrackGain,
@@ -63,6 +63,7 @@ public static class MusicTrackMetadataDtoMapping
if (dto.OriginalLanguage is not null && dto.OriginalLanguage.LanguageCode is not null && dto.OriginalLanguage.LanguageName is not null)
originalLanguage = LanguageInfo.Create(dto.OriginalLanguage.LanguageCode, dto.OriginalLanguage.LanguageName, Optional<string>.FromNullable(dto.OriginalLanguage.NativeName));
// The container is captured from the file itself and is never settable through a request; on an edit the track keeps the container it already has.
return AudioMetadata.Create(
dto.Title!,
Optional<string>.FromNullable(dto.OriginalTitle),
@@ -77,6 +78,7 @@ public static class MusicTrackMetadataDtoMapping
originalLanguage,
Optional<int>.FromNullable(dto.BitDepth),
Optional<string>.FromNullable(dto.AudioCodec),
Optional<string>.None(),
Optional<int>.FromNullable(dto.Bitrate),
Optional<string>.FromNullable(dto.AcoustId),
Optional<decimal>.FromNullable(dto.ReplayGainTrackGain),
@@ -84,6 +84,7 @@ public static class TrackEntityMapping
originalLanguage,
Optional<int>.FromNullable(repositoryEntity.BitDepth),
Optional<string>.FromNullable(repositoryEntity.AudioCodec),
Optional<string>.FromNullable(repositoryEntity.Container),
Optional<int>.FromNullable(repositoryEntity.Bitrate),
Optional<string>.FromNullable(repositoryEntity.AcoustId),
Optional<decimal>.FromNullable(repositoryEntity.ReplayGainTrackGain),
@@ -259,7 +260,8 @@ public static class TrackEntityMapping
[.. repositoryEntity.Moods.ToResponses()],
[.. repositoryEntity.Isrcs.ToResponses()],
[.. repositoryEntity.Contributors.Select(contributor => new MediaContributorReferenceDto(contributor.MediaContributorId, contributor.Role))],
[.. repositoryEntity.Ratings.ToResponses()]);
[.. repositoryEntity.Ratings.ToResponses()],
repositoryEntity.Container);
}
/// <summary>
@@ -52,6 +52,7 @@ public static class TrackMapping
Channels = domainEntity.Metadata.Channels,
BitDepth = domainEntity.Metadata.BitDepth.HasValue ? domainEntity.Metadata.BitDepth.Value : null,
AudioCodec = domainEntity.Metadata.AudioCodec.HasValue ? domainEntity.Metadata.AudioCodec.Value : null,
Container = domainEntity.Metadata.Container.HasValue ? domainEntity.Metadata.Container.Value : null,
Bitrate = domainEntity.Metadata.Bitrate.HasValue ? domainEntity.Metadata.Bitrate.Value : null,
AcoustId = domainEntity.Metadata.AcoustId.HasValue ? domainEntity.Metadata.AcoustId.Value : null,
ReplayGainTrackGain = domainEntity.Metadata.ReplayGainTrackGain.HasValue ? domainEntity.Metadata.ReplayGainTrackGain.Value : null,
@@ -34,6 +34,7 @@ namespace Lumina.Contracts.DTO.MediaLibrary.AudioLibrary.MusicLibrary;
/// <param name="Channels">The number of audio channels of the track. Required.</param>
/// <param name="BitDepth">The bit depth of the audio of the track. Optional.</param>
/// <param name="AudioCodec">The audio codec used by the track. Optional.</param>
/// <param name="Container">The container of the audio file of the track, for example MP3, FLAC, OGG, AAC, MP4 or WAV. Optional.</param>
/// <param name="Bitrate">The bitrate of the audio of the track in kbps. Optional.</param>
/// <param name="MusicBrainzRecordingId">The MusicBrainz identifier of the recording. Optional.</param>
/// <param name="MusicBrainzTrackId">The MusicBrainz identifier of the track. Optional.</param>
@@ -62,6 +63,7 @@ public sealed record AudioMetadataDto(
int? Channels,
int? BitDepth,
string? AudioCodec,
string? Container,
int? Bitrate,
Guid? MusicBrainzRecordingId,
Guid? MusicBrainzTrackId,
@@ -33,6 +33,7 @@ namespace Lumina.Contracts.Responses.MediaLibrary.AudioLibrary.MusicLibrary.Trac
/// <param name="Isrcs">The list of ISRC (International Standard Recording Code) of the track.</param>
/// <param name="Contributors">The list of references to the media contributors that performed on the track, each with the role they played.</param>
/// <param name="Ratings">The list of ratings for this track.</param>
/// <param name="Container">The container of the audio file of the track, for example MP3, FLAC, OGG, AAC, MP4 or WAV, captured from the file itself. Read only, and not settable through a request. Optional.</param>
[DebuggerDisplay("Title: {Metadata.Title}")]
public record TrackResponse(
Guid Id,
@@ -53,5 +54,6 @@ public record TrackResponse(
List<MoodDto>? Moods,
List<IsrcDto>? Isrcs,
List<MediaContributorReferenceDto>? Contributors,
List<AudioRatingDto>? Ratings
List<AudioRatingDto>? Ratings,
string? Container = null
);
@@ -98,6 +98,9 @@ public class MusicLibraryScanItemMetadataConfiguration : IEntityTypeConfiguratio
.HasColumnOrder(29);
builder.PrimitiveCollection(musicMetadata => musicMetadata.Moods)
.HasColumnOrder(30);
builder.Property(musicMetadata => musicMetadata.Container)
.HasMaxLength(50)
.HasColumnOrder(31);
builder.HasIndex(musicMetadata => new { musicMetadata.LibraryScanId, musicMetadata.Path })
.IsUnique(); // the metadata of the same physical file cannot be staged twice during the same scan
@@ -152,6 +152,9 @@ public class TrackConfiguration : IEntityTypeConfiguration<TrackEntity>
builder.Property(track => track.ReplayGainAlbumPeak)
.HasColumnType("decimal(8,6)")
.HasColumnOrder(48);
builder.Property(track => track.Container)
.HasMaxLength(50)
.HasColumnOrder(49);
builder.HasOne(track => track.Album)
.WithMany(album => album.Tracks)
@@ -0,0 +1,42 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Lumina.DataAccess.Common.Migrations
{
/// <inheritdoc />
public partial class AddsAudioContainerToMusicMetadata : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "Container",
table: "Tracks",
type: "TEXT",
maxLength: 50,
nullable: true)
.Annotation("Relational:ColumnOrder", 49);
migrationBuilder.AddColumn<string>(
name: "Container",
table: "MusicLibraryScanItemMetadata",
type: "TEXT",
maxLength: 50,
nullable: true)
.Annotation("Relational:ColumnOrder", 31);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Container",
table: "Tracks");
migrationBuilder.DropColumn(
name: "Container",
table: "MusicLibraryScanItemMetadata");
}
}
}
@@ -844,6 +844,11 @@ namespace Lumina.DataAccess.Common.Migrations
.HasColumnType("INTEGER")
.HasColumnOrder(13);
b.Property<string>("Container")
.HasMaxLength(50)
.HasColumnType("TEXT")
.HasColumnOrder(31);
b.Property<int?>("DiscNumber")
.HasColumnType("INTEGER")
.HasColumnOrder(10);
@@ -1037,6 +1042,11 @@ namespace Lumina.DataAccess.Common.Migrations
.HasColumnType("INTEGER")
.HasColumnOrder(22);
b.Property<string>("Container")
.HasMaxLength(50)
.HasColumnType("TEXT")
.HasColumnOrder(49);
b.Property<Guid>("CreatedBy")
.HasColumnType("TEXT")
.HasColumnOrder(38);
@@ -38,6 +38,11 @@ public class AudioMetadata : BaseMetadata
/// </summary>
public Optional<string> AudioCodec { get; }
/// <summary>
/// Gets the container of the audio, as read from the file itself, for example MP3, FLAC, OGG, AAC, MP4 or WAV.
/// </summary>
public Optional<string> Container { get; }
/// <summary>
/// Gets the bitrate of the audio in kbps.
/// </summary>
@@ -82,6 +87,7 @@ public class AudioMetadata : BaseMetadata
/// <param name="originalLanguage">The optional original language of the audio.</param>
/// <param name="bitDepth">The bit depth of the audio.</param>
/// <param name="audioCodec">The audio codec used.</param>
/// <param name="container">The container of the audio.</param>
/// <param name="genres">The genres of the audio.</param>
/// <param name="tags">The tags associated with the audio.</param>
/// <param name="bitrate">The bitrate of the audio in kbps.</param>
@@ -104,6 +110,7 @@ public class AudioMetadata : BaseMetadata
Optional<LanguageInfo> originalLanguage,
Optional<int> bitDepth,
Optional<string> audioCodec,
Optional<string> container,
Optional<int> bitrate,
Optional<string> acoustId,
Optional<decimal> replayGainTrackGain,
@@ -117,6 +124,7 @@ public class AudioMetadata : BaseMetadata
Channels = channels;
BitDepth = bitDepth;
AudioCodec = audioCodec;
Container = container;
Bitrate = bitrate;
AcoustId = acoustId;
ReplayGainTrackGain = replayGainTrackGain;
@@ -139,6 +147,7 @@ public class AudioMetadata : BaseMetadata
/// <param name="originalLanguage">The optional original language of the audio.</param>
/// <param name="bitDepth">The bit depth of the audio.</param>
/// <param name="audioCodec">The audio codec used.</param>
/// <param name="container">The container of the audio.</param>
/// <param name="genres">The genres of the audio.</param>
/// <param name="tags">The tags associated with the audio.</param>
/// <param name="bitrate">The bitrate of the audio in kbps.</param>
@@ -164,6 +173,7 @@ public class AudioMetadata : BaseMetadata
Optional<LanguageInfo> originalLanguage,
Optional<int> bitDepth,
Optional<string> audioCodec,
Optional<string> container,
Optional<int> bitrate,
Optional<string> acoustId,
Optional<decimal> replayGainTrackGain,
@@ -185,6 +195,7 @@ public class AudioMetadata : BaseMetadata
originalLanguage,
bitDepth,
audioCodec,
container,
bitrate,
acoustId,
replayGainTrackGain,
@@ -193,6 +204,38 @@ public class AudioMetadata : BaseMetadata
replayGainAlbumPeak);
}
/// <summary>
/// Creates a copy of this audio metadata with the provided <paramref name="container"/>, keeping every other property unchanged.
/// </summary>
/// <param name="container">The container of the audio.</param>
/// <returns>
/// An <see cref="Result{TValue}"/> containing either a copy of this audio metadata with the provided container, or an error message.
/// </returns>
internal Result<AudioMetadata> WithContainer(Optional<string> container)
{
return Create(
Title,
OriginalTitle,
DurationInSeconds,
SampleRate,
Channels,
ReleaseInfo,
Description,
[.. Genres],
[.. Tags],
Language,
OriginalLanguage,
BitDepth,
AudioCodec,
container,
Bitrate,
AcoustId,
ReplayGainTrackGain,
ReplayGainTrackPeak,
ReplayGainAlbumGain,
ReplayGainAlbumPeak);
}
/// <summary>
/// Gets the list of items that define equality of the object.
/// </summary>
@@ -206,6 +249,7 @@ public class AudioMetadata : BaseMetadata
yield return Channels;
yield return BitDepth;
yield return AudioCodec;
yield return Container;
yield return Bitrate;
yield return AcoustId;
yield return ReplayGainTrackGain;
@@ -347,7 +347,8 @@ public sealed class Track : Entity<TrackId>
/// </summary>
/// <remarks>
/// The file system path is intentionally not editable here: it is set only when the track is created by the scanner, so that a client
/// can never point an existing track at an arbitrary file of the host.
/// can never point an existing track at an arbitrary file of the host. The container is captured from the file itself and is not part of
/// an edit either, so an update that does not carry one keeps the container already stored for the track.
/// </remarks>
/// <param name="metadata">The audio metadata of the track.</param>
/// <param name="disambiguation">The optional disambiguation comment of the track.</param>
@@ -374,7 +375,16 @@ public sealed class Track : Entity<TrackId>
Optional<MusicBrainzId> musicBrainzRecordingId,
Optional<MusicBrainzId> musicBrainzTrackId)
{
Metadata = metadata;
// The container is captured from the file itself and is never part of an edit, so an update that does not carry one keeps the stored container.
if (metadata.Container.HasValue)
Metadata = metadata;
else
{
Result<AudioMetadata> metadataWithStoredContainerResult = metadata.WithContainer(Metadata.Container);
if (metadataWithStoredContainerResult.IsFailure)
return metadataWithStoredContainerResult.Errors;
Metadata = metadataWithStoredContainerResult.Value;
}
Disambiguation = disambiguation;
TrackNumber = trackNumber;
DiscNumber = discNumber;
@@ -0,0 +1,57 @@
#region ========================================================================= USING =====================================================================================
using System;
#endregion
namespace Lumina.Infrastructure.Core.MediaLibrary.AudioLibrary.MusicLibrary.Metadata;
/// <summary>
/// Resolves the canonical name of the container of an audio file from the concrete type the tagging library parsed it as.
/// </summary>
internal static class AudioContainerResolver
{
/// <summary>
/// Resolves the container of the audio file parsed by <paramref name="tagFile"/>.
/// </summary>
/// <param name="tagFile">The parsed audio file whose container is resolved.</param>
/// <returns>The canonical name of the container, for example MP3, FLAC, OGG, AAC, MP4 or WAV, or <see langword="null"/> when the container cannot be recognized.</returns>
public static string? Resolve(TagLib.File tagFile)
{
// The concrete type of the parsed file is the most reliable description of its container, because it is chosen by the parser from the
// actual bytes of the file, while the mime type only describes the extension. Containers that share a parser (for example the Opus and
// Vorbis streams parsed by the Ogg parser) are reported under the name of their container, and the codec column carries the difference.
return tagFile switch
{
TagLib.Mpeg.File => "MP3",
TagLib.Flac.File => "FLAC",
TagLib.Mpeg4.File => "MP4",
TagLib.Aac.File => "AAC",
TagLib.Ogg.File => "OGG",
TagLib.Riff.File => "WAV",
TagLib.Aiff.File => "AIFF",
TagLib.Ape.File => "APE",
TagLib.WavPack.File => "WAVPACK",
TagLib.Asf.File => "WMA",
TagLib.Dsf.File => "DSF",
TagLib.Matroska.File => "MATROSKA",
TagLib.MusePack.File => "MPC",
TagLib.Audible.File => "AA",
_ => ResolveFromMimeType(tagFile.MimeType)
};
}
/// <summary>
/// Resolves the container from the mime type reported by the tagging library, for the containers that have no dedicated parser.
/// </summary>
/// <param name="mimeType">The mime type reported by the tagging library.</param>
/// <returns>The name of the container, or <see langword="null"/> when it cannot be derived.</returns>
public static string? ResolveFromMimeType(string? mimeType)
{
if (string.IsNullOrWhiteSpace(mimeType))
return null;
// The library reports its mime types with the "taglib/" prefix, so only the last segment names the container.
string[] segments = mimeType.Split('/', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
string? container = segments.Length > 0 ? segments[^1] : null;
return string.IsNullOrWhiteSpace(container) ? null : container.ToUpperInvariant();
}
}
@@ -111,6 +111,7 @@ internal static class MusicMetadataAggregator
First(first.Channels, second.Channels),
First(first.BitDepth, second.BitDepth),
First(first.AudioCodec, second.AudioCodec),
First(first.Container, second.Container),
First(first.Bitrate, second.Bitrate),
First(first.MusicBrainzRecordingId, second.MusicBrainzRecordingId),
First(first.MusicBrainzTrackId, second.MusicBrainzTrackId),
@@ -306,6 +306,7 @@ internal sealed class MusicMediaLibraryScanItemMaterializer : IMediaLibraryScanI
Channels = stagedItem.Channels,
BitDepth = stagedItem.BitDepth,
AudioCodec = stagedItem.AudioCodec,
Container = stagedItem.Container,
Bitrate = stagedItem.Bitrate,
AcoustId = stagedItem.AcoustId,
ReplayGainTrackGain = stagedItem.ReplayGainTrackGain,
@@ -17,6 +17,7 @@ using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.Library
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
using Lumina.Domain.SharedKernel.Common.Enums.AudioLibrary;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using Lumina.Infrastructure.Core.MediaLibrary.AudioLibrary.MusicLibrary.Metadata;
using Lumina.Infrastructure.Core.MediaLibrary.Management.Scanning.Jobs.Common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
@@ -232,6 +233,7 @@ internal sealed class MusicMetadataExtractionJob : MediaLibraryScanJob, IMusicMe
int channels = 0;
int? bitDepth = null;
string? audioCodec = null;
string? container = null;
int? bitrate = null;
string? acoustId = null;
decimal? replayGainTrackGain = null;
@@ -242,64 +244,71 @@ internal sealed class MusicMetadataExtractionJob : MediaLibraryScanJob, IMusicMe
try
{
using (TagLib.File tagFile = TagLib.File.Create(path))
// A file that is itself a link is not read, so its target outside the content locations is never opened. A linked directory is a separate
// discovery level concern, so this is a defense in depth for the leaf file rather than a complete containment guarantee.
if (!IsReparsePoint(path))
{
TagLib.Tag tag = tagFile.Tag;
if (!string.IsNullOrWhiteSpace(tag.Title))
trackTitle = tag.Title;
if (!string.IsNullOrWhiteSpace(tag.Album))
releaseName = tag.Album;
// The album artist is preferred over the track artist, because it is the artist the release belongs to.
string? tagArtist = !string.IsNullOrWhiteSpace(tag.FirstAlbumArtist) ? tag.FirstAlbumArtist : tag.FirstPerformer;
if (!string.IsNullOrWhiteSpace(tagArtist))
artistName = tagArtist;
if (tag.Track > 0)
trackNumber = (int)tag.Track;
if (tag.Disc > 0)
discNumber = (int)tag.Disc;
if (tag.Year > 0)
releaseYear = (int)tag.Year;
using (TagLib.File tagFile = TagLib.File.Create(path))
{
TagLib.Tag tag = tagFile.Tag;
if (!string.IsNullOrWhiteSpace(tag.Title))
trackTitle = tag.Title;
if (!string.IsNullOrWhiteSpace(tag.Album))
releaseName = tag.Album;
// The album artist is preferred over the track artist, because it is the artist the release belongs to.
string? tagArtist = !string.IsNullOrWhiteSpace(tag.FirstAlbumArtist) ? tag.FirstAlbumArtist : tag.FirstPerformer;
if (!string.IsNullOrWhiteSpace(tagArtist))
artistName = tagArtist;
if (tag.Track > 0)
trackNumber = (int)tag.Track;
if (tag.Disc > 0)
discNumber = (int)tag.Disc;
if (tag.Year > 0)
releaseYear = (int)tag.Year;
TagLib.Properties audioProperties = tagFile.Properties;
durationInSeconds = (int)audioProperties.Duration.TotalSeconds;
sampleRate = audioProperties.AudioSampleRate;
channels = audioProperties.AudioChannels;
if (audioProperties.BitsPerSample > 0)
bitDepth = audioProperties.BitsPerSample;
if (audioProperties.AudioBitrate > 0)
bitrate = audioProperties.AudioBitrate;
if (!string.IsNullOrWhiteSpace(audioProperties.Description))
audioCodec = audioProperties.Description;
TagLib.Properties audioProperties = tagFile.Properties;
durationInSeconds = (int)audioProperties.Duration.TotalSeconds;
sampleRate = audioProperties.AudioSampleRate;
channels = audioProperties.AudioChannels;
// The container is the format of the file itself, so it is resolved from the concrete type of the parsed file and not from its tags.
container = AudioContainerResolver.Resolve(tagFile);
if (audioProperties.BitsPerSample > 0)
bitDepth = audioProperties.BitsPerSample;
if (audioProperties.AudioBitrate > 0)
bitrate = audioProperties.AudioBitrate;
if (!string.IsNullOrWhiteSpace(audioProperties.Description))
audioCodec = audioProperties.Description;
// The text frames of the ID3v2 tag are read once for the whole file, so that the extended tag lookups below do not walk all the frames
// of the tag again for every key.
IReadOnlyDictionary<string, string[]> id3v2ExtendedFields = GetId3v2ExtendedFields(tagFile);
// The text frames of the ID3v2 tag are read once for the whole file, so that the extended tag lookups below do not walk all the frames
// of the tag again for every key.
IReadOnlyDictionary<string, string[]> id3v2ExtendedFields = GetId3v2ExtendedFields(tagFile);
// The MusicBrainz identifiers written by taggers like Picard are read here, so that the enrichment can look the items up directly,
// instead of guessing them through searches. The work identifier and title are not exposed as properties by the tagging library, so
// they are read from the extended tags of the container, and they only serve as a fallback for recordings that MusicBrainz leaves unlinked to a work.
musicBrainzArtistId = ParseGuid(tag.MusicBrainzArtistId);
musicBrainzReleaseArtistId = ParseGuid(tag.MusicBrainzReleaseArtistId);
musicBrainzReleaseGroupId = ParseGuid(tag.MusicBrainzReleaseGroupId);
musicBrainzReleaseId = ParseGuid(tag.MusicBrainzReleaseId);
musicBrainzRecordingId = ParseGuid(tag.MusicBrainzTrackId);
musicBrainzTrackId = ParseGuid(GetExtendedField(tagFile, id3v2ExtendedFields, "MusicBrainz Release Track Id", "MUSICBRAINZ_RELEASETRACKID"));
musicBrainzWorkId = ParseGuid(GetExtendedField(tagFile, id3v2ExtendedFields, "MusicBrainz Work Id", "MUSICBRAINZ_WORKID"));
string? extendedWorkTitle = GetExtendedField(tagFile, id3v2ExtendedFields, "WORK", "MUSICBRAINZ_WORK");
if (!string.IsNullOrWhiteSpace(extendedWorkTitle))
workTitle = extendedWorkTitle;
// The MusicBrainz identifiers written by taggers like Picard are read here, so that the enrichment can look the items up directly,
// instead of guessing them through searches. The work identifier and title are not exposed as properties by the tagging library, so
// they are read from the extended tags of the container, and they only serve as a fallback for recordings that MusicBrainz leaves unlinked to a work.
musicBrainzArtistId = ParseGuid(tag.MusicBrainzArtistId);
musicBrainzReleaseArtistId = ParseGuid(tag.MusicBrainzReleaseArtistId);
musicBrainzReleaseGroupId = ParseGuid(tag.MusicBrainzReleaseGroupId);
musicBrainzReleaseId = ParseGuid(tag.MusicBrainzReleaseId);
musicBrainzRecordingId = ParseGuid(tag.MusicBrainzTrackId);
musicBrainzTrackId = ParseGuid(GetExtendedField(tagFile, id3v2ExtendedFields, "MusicBrainz Release Track Id", "MUSICBRAINZ_RELEASETRACKID"));
musicBrainzWorkId = ParseGuid(GetExtendedField(tagFile, id3v2ExtendedFields, "MusicBrainz Work Id", "MUSICBRAINZ_WORKID"));
string? extendedWorkTitle = GetExtendedField(tagFile, id3v2ExtendedFields, "WORK", "MUSICBRAINZ_WORK");
if (!string.IsNullOrWhiteSpace(extendedWorkTitle))
workTitle = extendedWorkTitle;
// The AcoustID, the moods and the ReplayGain values written by taggers like Picard are read here as well. The moods can be a single
// slash separated value or several distinct values, depending on the container, so all of them are read and split before being de-duplicated.
acoustId = GetExtendedField(tagFile, id3v2ExtendedFields, "Acoustid Id", "ACOUSTID_ID");
moods = [.. GetExtendedFields(tagFile, id3v2ExtendedFields, "mood", "MOOD", "MOODS")
.SelectMany(mood => mood.Split('/', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
.Where(mood => !string.IsNullOrWhiteSpace(mood))
.Distinct(StringComparer.OrdinalIgnoreCase)];
replayGainTrackGain = ParseDecimal(GetExtendedField(tagFile, id3v2ExtendedFields, "REPLAYGAIN_TRACK_GAIN", "replaygain_track_gain"));
replayGainTrackPeak = ParseDecimal(GetExtendedField(tagFile, id3v2ExtendedFields, "REPLAYGAIN_TRACK_PEAK", "replaygain_track_peak"));
replayGainAlbumGain = ParseDecimal(GetExtendedField(tagFile, id3v2ExtendedFields, "REPLAYGAIN_ALBUM_GAIN", "replaygain_album_gain"));
replayGainAlbumPeak = ParseDecimal(GetExtendedField(tagFile, id3v2ExtendedFields, "REPLAYGAIN_ALBUM_PEAK", "replaygain_album_peak"));
// The AcoustID, the moods and the ReplayGain values written by taggers like Picard are read here as well. The moods can be a single
// slash separated value or several distinct values, depending on the container, so all of them are read and split before being de-duplicated.
acoustId = GetExtendedField(tagFile, id3v2ExtendedFields, "Acoustid Id", "ACOUSTID_ID");
moods = [.. GetExtendedFields(tagFile, id3v2ExtendedFields, "mood", "MOOD", "MOODS")
.SelectMany(mood => mood.Split('/', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
.Where(mood => !string.IsNullOrWhiteSpace(mood))
.Distinct(StringComparer.OrdinalIgnoreCase)];
replayGainTrackGain = ParseDecimal(GetExtendedField(tagFile, id3v2ExtendedFields, "REPLAYGAIN_TRACK_GAIN", "replaygain_track_gain"));
replayGainTrackPeak = ParseDecimal(GetExtendedField(tagFile, id3v2ExtendedFields, "REPLAYGAIN_TRACK_PEAK", "replaygain_track_peak"));
replayGainAlbumGain = ParseDecimal(GetExtendedField(tagFile, id3v2ExtendedFields, "REPLAYGAIN_ALBUM_GAIN", "replaygain_album_gain"));
replayGainAlbumPeak = ParseDecimal(GetExtendedField(tagFile, id3v2ExtendedFields, "REPLAYGAIN_ALBUM_PEAK", "replaygain_album_peak"));
}
}
}
catch (Exception exception) when (exception is TagLib.CorruptFileException or TagLib.UnsupportedFormatException or IOException or UnauthorizedAccessException)
@@ -330,6 +339,7 @@ internal sealed class MusicMetadataExtractionJob : MediaLibraryScanJob, IMusicMe
Channels = channels,
BitDepth = bitDepth,
AudioCodec = audioCodec,
Container = container,
Bitrate = bitrate,
AcoustId = acoustId,
ReplayGainTrackGain = replayGainTrackGain,
@@ -348,6 +358,17 @@ internal sealed class MusicMetadataExtractionJob : MediaLibraryScanJob, IMusicMe
};
}
/// <summary>
/// Determines whether the file stored at <paramref name="path"/> is a reparse point, such as a symbolic link.
/// </summary>
/// <param name="path">The file system path of the file to check.</param>
/// <returns><see langword="true"/> when the file is a reparse point, otherwise <see langword="false"/>.</returns>
private static bool IsReparsePoint(string path)
{
// This only detects a leaf file that is itself a link; a linked directory encountered during discovery is not caught here.
return (File.GetAttributes(path) & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint;
}
/// <summary>
/// Parses the provided tag <paramref name="value"/> as a globally unique identifier.
/// </summary>
@@ -460,6 +460,7 @@ public class AddAlbumEndpointSummary : Summary<AddAlbumEndpoint, AddAlbumRequest
AlbumId: albumId,
LibraryId: libraryId,
Path: "/music/queen/a-night-at-the-opera/01-bohemian-rhapsody.flac",
Container: "FLAC",
Metadata: new(
Title: "Bohemian Rhapsody",
OriginalTitle: "Bohemian Rhapsody",
@@ -552,6 +553,7 @@ public class AddAlbumEndpointSummary : Summary<AddAlbumEndpoint, AddAlbumRequest
AlbumId: albumId,
LibraryId: libraryId,
Path: "/music/queen/a-night-at-the-opera/07-youre-my-best-friend.flac",
Container: "FLAC",
Metadata: new(
Title: "You're My Best Friend",
OriginalTitle: "You're My Best Friend",
@@ -152,6 +152,7 @@ public class GetAlbumEndpointSummary : Summary<GetAlbumEndpoint, EmptyRequest>
AlbumId: Guid.NewGuid(),
LibraryId: Guid.NewGuid(),
Path: "/music/queen/a-night-at-the-opera/01-bohemian-rhapsody.flac",
Container: "FLAC",
Metadata: new(
Title: "Bohemian Rhapsody",
OriginalTitle: "Bohemian Rhapsody",
@@ -244,6 +245,7 @@ public class GetAlbumEndpointSummary : Summary<GetAlbumEndpoint, EmptyRequest>
AlbumId: Guid.NewGuid(),
LibraryId: Guid.NewGuid(),
Path: "/music/queen/a-night-at-the-opera/07-youre-my-best-friend.flac",
Container: "FLAC",
Metadata: new(
Title: "You're My Best Friend",
OriginalTitle: "You're My Best Friend",
@@ -33,6 +33,7 @@ public class GetAlbumTracksEndpointSummary : Summary<GetAlbumTracksEndpoint, Emp
ResponseParam<TrackResponse>(r => r.AlbumId, "The Id of the album the track belongs to.");
ResponseParam<TrackResponse>(r => r.LibraryId, "The Id of the media library the track belongs to.");
ResponseParam<TrackResponse>(r => r.Path, "The file system path of the track.");
ResponseParam<TrackResponse>(r => r.Container, "The container of the audio file of the track, if applicable.");
ResponseParam<TrackResponse>(r => r.Metadata!.Title, "The title of the track.");
ResponseParam<TrackResponse>(r => r.Metadata!.OriginalTitle, "The original title of the track, if applicable.");
ResponseParam<TrackResponse>(r => r.Metadata!.Description, "The description of the track, if applicable.");
@@ -80,6 +81,7 @@ public class GetAlbumTracksEndpointSummary : Summary<GetAlbumTracksEndpoint, Emp
AlbumId: Guid.NewGuid(),
LibraryId: Guid.NewGuid(),
Path: "/music/queen/a-night-at-the-opera/01-bohemian-rhapsody.flac",
Container: "FLAC",
Metadata: new MusicTrackMetadataDto(
Title: "Bohemian Rhapsody",
OriginalTitle: "Bohemian Rhapsody",
@@ -172,6 +174,7 @@ public class GetAlbumTracksEndpointSummary : Summary<GetAlbumTracksEndpoint, Emp
AlbumId: Guid.NewGuid(),
LibraryId: Guid.NewGuid(),
Path: "/music/queen/a-night-at-the-opera/02-youre-my-best-friend.flac",
Container: "FLAC",
Metadata: new MusicTrackMetadataDto(
Title: "You're My Best Friend",
OriginalTitle: "You're My Best Friend",
@@ -264,6 +264,7 @@ public class UpdateAlbumEndpointSummary : Summary<UpdateAlbumEndpoint, UpdateAlb
AlbumId: Guid.NewGuid(),
LibraryId: Guid.NewGuid(),
Path: "/music/queen/a-night-at-the-opera/01-bohemian-rhapsody.flac",
Container: "FLAC",
Metadata: new(
Title: "Bohemian Rhapsody",
OriginalTitle: "Bohemian Rhapsody",
@@ -356,6 +357,7 @@ public class UpdateAlbumEndpointSummary : Summary<UpdateAlbumEndpoint, UpdateAlb
AlbumId: Guid.NewGuid(),
LibraryId: Guid.NewGuid(),
Path: "/music/queen/a-night-at-the-opera/07-youre-my-best-friend.flac",
Container: "FLAC",
Metadata: new(
Title: "You're My Best Friend",
OriginalTitle: "You're My Best Friend",
@@ -633,6 +633,7 @@ public class AddArtistEndpointSummary : Summary<AddArtistEndpoint, AddArtistRequ
AlbumId: albumId,
LibraryId: libraryId,
Path: "/music/queen/a-night-at-the-opera/01-bohemian-rhapsody.flac",
Container: "FLAC",
Metadata: new(
Title: "Bohemian Rhapsody",
OriginalTitle: "Bohemian Rhapsody",
@@ -731,6 +732,7 @@ public class AddArtistEndpointSummary : Summary<AddArtistEndpoint, AddArtistRequ
AlbumId: albumId,
LibraryId: libraryId,
Path: "/music/queen/a-night-at-the-opera/07-youre-my-best-friend.flac",
Container: "FLAC",
Metadata: new(
Title: "You're My Best Friend",
OriginalTitle: "You're My Best Friend",
@@ -123,6 +123,7 @@ public class GetArtistEndpointSummary : Summary<GetArtistEndpoint, EmptyRequest>
AlbumId: Guid.NewGuid(),
LibraryId: Guid.NewGuid(),
Path: "/music/queen/a-night-at-the-opera/01-bohemian-rhapsody.flac",
Container: "FLAC",
Metadata: new(
Title: "Bohemian Rhapsody",
OriginalTitle: "Bohemian Rhapsody",
@@ -154,6 +154,7 @@ public class GetArtistAlbumsEndpointSummary : Summary<GetArtistAlbumsEndpoint, E
AlbumId: Guid.NewGuid(),
LibraryId: Guid.NewGuid(),
Path: "/music/queen/a-night-at-the-opera/01-bohemian-rhapsody.flac",
Container: "FLAC",
Metadata: new(
Title: "Bohemian Rhapsody",
OriginalTitle: "Bohemian Rhapsody",
@@ -328,6 +329,7 @@ public class GetArtistAlbumsEndpointSummary : Summary<GetArtistAlbumsEndpoint, E
AlbumId: Guid.NewGuid(),
LibraryId: Guid.NewGuid(),
Path: "/music/queen/the-works/01-radio-ga-ga.flac",
Container: "FLAC",
Metadata: new(
Title: "Radio Ga Ga",
OriginalTitle: "Radio Ga Ga",
@@ -135,6 +135,7 @@ public class GetArtistsEndpointSummary : Summary<GetArtistsEndpoint, GetArtistsR
AlbumId: Guid.NewGuid(),
LibraryId: Guid.NewGuid(),
Path: "/music/queen/a-night-at-the-opera/01-bohemian-rhapsody.flac",
Container: "FLAC",
Metadata: new(
Title: "Bohemian Rhapsody",
OriginalTitle: "Bohemian Rhapsody",
@@ -259,6 +260,7 @@ public class GetArtistsEndpointSummary : Summary<GetArtistsEndpoint, GetArtistsR
AlbumId: Guid.NewGuid(),
LibraryId: Guid.NewGuid(),
Path: "/music/david-bowie/heroes/01-heroes.flac",
Container: "FLAC",
Metadata: new(
Title: "Heroes",
OriginalTitle: "Heroes",
@@ -630,6 +630,7 @@ public class UpdateArtistEndpointSummary : Summary<UpdateArtistEndpoint, UpdateA
AlbumId: albumId,
LibraryId: libraryId,
Path: "/music/queen/a-night-at-the-opera/01-bohemian-rhapsody.flac",
Container: "FLAC",
Metadata: new MusicTrackMetadataDto(
Title: "Bohemian Rhapsody",
OriginalTitle: "Bohemian Rhapsody",
@@ -728,6 +729,7 @@ public class UpdateArtistEndpointSummary : Summary<UpdateArtistEndpoint, UpdateA
AlbumId: albumId,
LibraryId: libraryId,
Path: "/music/queen/a-night-at-the-opera/07-youre-my-best-friend.flac",
Container: "FLAC",
Metadata: new MusicTrackMetadataDto(
Title: "You're My Best Friend",
OriginalTitle: "You're My Best Friend",
@@ -170,6 +170,7 @@ public class AddTrackEndpointSummary : Summary<AddTrackEndpoint, AddTrackRequest
ResponseParam<TrackResponse>(r => r.AlbumId, "The Id of the album the track belongs to.");
ResponseParam<TrackResponse>(r => r.LibraryId, "The Id of the media library the track belongs to.");
ResponseParam<TrackResponse>(r => r.Path, "The file system path of the track.");
ResponseParam<TrackResponse>(r => r.Container, "The container of the audio file of the track, if applicable.");
ResponseParam<TrackResponse>(r => r.Metadata!.Title, "The title of the track.");
ResponseParam<TrackResponse>(r => r.Metadata!.OriginalTitle, "The original title of the track, if applicable.");
ResponseParam<TrackResponse>(r => r.Metadata!.Description, "The description of the track, if applicable.");
@@ -215,6 +216,7 @@ public class AddTrackEndpointSummary : Summary<AddTrackEndpoint, AddTrackRequest
AlbumId: Guid.NewGuid(),
LibraryId: Guid.NewGuid(),
Path: "/music/queen/a-night-at-the-opera/01-bohemian-rhapsody.flac",
Container: "FLAC",
Metadata: new MusicTrackMetadataDto(
Title: "Bohemian Rhapsody",
OriginalTitle: "Bohemian Rhapsody",
@@ -33,6 +33,7 @@ public class GetTrackEndpointSummary : Summary<GetTrackEndpoint, EmptyRequest>
ResponseParam<TrackResponse>(r => r.AlbumId, "The Id of the album the track belongs to.");
ResponseParam<TrackResponse>(r => r.LibraryId, "The Id of the media library the track belongs to.");
ResponseParam<TrackResponse>(r => r.Path, "The file system path of the track.");
ResponseParam<TrackResponse>(r => r.Container, "The container of the audio file of the track, if applicable.");
ResponseParam<TrackResponse>(r => r.Metadata!.Title, "The title of the track.");
ResponseParam<TrackResponse>(r => r.Metadata!.OriginalTitle, "The original title of the track, if applicable.");
ResponseParam<TrackResponse>(r => r.Metadata!.Description, "The description of the track, if applicable.");
@@ -78,6 +79,7 @@ public class GetTrackEndpointSummary : Summary<GetTrackEndpoint, EmptyRequest>
AlbumId: Guid.NewGuid(),
LibraryId: Guid.NewGuid(),
Path: "/music/queen/a-night-at-the-opera/01-bohemian-rhapsody.flac",
Container: "FLAC",
Metadata: new MusicTrackMetadataDto(
Title: "Bohemian Rhapsody",
OriginalTitle: "Bohemian Rhapsody",
@@ -168,6 +168,7 @@ public class UpdateTrackEndpointSummary : Summary<UpdateTrackEndpoint, UpdateTra
ResponseParam<TrackResponse>(r => r.AlbumId, "The Id of the album the track belongs to.");
ResponseParam<TrackResponse>(r => r.LibraryId, "The Id of the media library the track belongs to.");
ResponseParam<TrackResponse>(r => r.Path, "The file system path of the track.");
ResponseParam<TrackResponse>(r => r.Container, "The container of the audio file of the track, if applicable.");
ResponseParam<TrackResponse>(r => r.Metadata!.Title, "The title of the track.");
ResponseParam<TrackResponse>(r => r.Metadata!.OriginalTitle, "The original title of the track, if applicable.");
ResponseParam<TrackResponse>(r => r.Metadata!.Description, "The description of the track, if applicable.");
@@ -213,6 +214,7 @@ public class UpdateTrackEndpointSummary : Summary<UpdateTrackEndpoint, UpdateTra
AlbumId: Guid.NewGuid(),
LibraryId: Guid.NewGuid(),
Path: "/music/queen/a-night-at-the-opera/01-bohemian-rhapsody.flac",
Container: "FLAC",
Metadata: new MusicTrackMetadataDto(
Title: "Bohemian Rhapsody",
OriginalTitle: "Bohemian Rhapsody",