2019-01-13 15:03:10 -05:00
|
|
|
using System;
|
2019-01-13 14:26:31 -05:00
|
|
|
using System.Collections.Generic;
|
2023-05-15 07:12:24 -04:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Globalization;
|
2019-01-13 14:26:31 -05:00
|
|
|
using System.Linq;
|
2023-05-15 07:12:24 -04:00
|
|
|
using System.Text.RegularExpressions;
|
2019-01-13 14:26:31 -05:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2023-03-25 13:52:02 -04:00
|
|
|
using Jellyfin.Data.Enums;
|
2013-02-20 20:33:05 -05:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2014-02-05 23:39:16 -05:00
|
|
|
using MediaBrowser.Controller.Library;
|
2014-02-20 11:37:41 -05:00
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2013-12-05 22:39:44 -05:00
|
|
|
using MediaBrowser.Controller.Persistence;
|
2019-01-13 14:26:31 -05:00
|
|
|
using MediaBrowser.Controller.Providers;
|
2015-04-05 11:01:57 -04:00
|
|
|
using MediaBrowser.Model.Dlna;
|
2019-01-13 14:26:31 -05:00
|
|
|
using MediaBrowser.Model.Dto;
|
2013-02-20 20:33:05 -05:00
|
|
|
using MediaBrowser.Model.Entities;
|
2014-06-16 21:56:23 -04:00
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2023-05-15 07:12:24 -04:00
|
|
|
using Microsoft.Extensions.Logging;
|
2022-03-28 17:11:21 -04:00
|
|
|
using TagLib;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
2013-06-09 12:47:28 -04:00
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2022-03-31 10:17:37 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Probes audio files for metadata.
|
|
|
|
/// </summary>
|
2023-05-22 16:48:09 -04:00
|
|
|
public partial class AudioFileProber
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2023-05-15 07:12:24 -04:00
|
|
|
// Default LUFS value for use with the web interface, at -18db gain will be 1(no db gain).
|
|
|
|
private const float DefaultLUFSValue = -18;
|
|
|
|
|
|
|
|
private readonly ILogger<AudioFileProber> _logger;
|
2014-02-05 23:39:16 -05:00
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
2013-12-05 22:39:44 -05:00
|
|
|
private readonly IItemRepository _itemRepo;
|
2015-06-28 13:00:36 -04:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
2018-09-12 13:26:21 -04:00
|
|
|
private readonly IMediaSourceManager _mediaSourceManager;
|
2013-12-05 22:39:44 -05:00
|
|
|
|
2022-03-31 10:17:37 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="AudioFileProber"/> class.
|
|
|
|
/// </summary>
|
2023-05-15 07:12:24 -04:00
|
|
|
/// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
|
2022-03-31 10:17:37 -04:00
|
|
|
/// <param name="mediaSourceManager">Instance of the <see cref="IMediaSourceManager"/> interface.</param>
|
|
|
|
/// <param name="mediaEncoder">Instance of the <see cref="IMediaEncoder"/> interface.</param>
|
|
|
|
/// <param name="itemRepo">Instance of the <see cref="IItemRepository"/> interface.</param>
|
|
|
|
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
|
2022-03-29 11:06:30 -04:00
|
|
|
public AudioFileProber(
|
2023-05-15 07:12:24 -04:00
|
|
|
ILogger<AudioFileProber> logger,
|
2020-08-07 13:26:28 -04:00
|
|
|
IMediaSourceManager mediaSourceManager,
|
|
|
|
IMediaEncoder mediaEncoder,
|
|
|
|
IItemRepository itemRepo,
|
|
|
|
ILibraryManager libraryManager)
|
2013-03-02 12:59:15 -05:00
|
|
|
{
|
2023-05-15 07:12:24 -04:00
|
|
|
_logger = logger;
|
2014-02-05 23:39:16 -05:00
|
|
|
_mediaEncoder = mediaEncoder;
|
2013-12-05 22:39:44 -05:00
|
|
|
_itemRepo = itemRepo;
|
2015-06-28 13:00:36 -04:00
|
|
|
_libraryManager = libraryManager;
|
2018-09-12 13:26:21 -04:00
|
|
|
_mediaSourceManager = mediaSourceManager;
|
2013-03-02 12:59:15 -05:00
|
|
|
}
|
|
|
|
|
2023-05-22 16:48:09 -04:00
|
|
|
[GeneratedRegex("I:\\s+(.*?)\\s+LUFS")]
|
|
|
|
private static partial Regex LUFSRegex();
|
|
|
|
|
2022-03-31 10:17:37 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Probes the specified item for metadata.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item to probe.</param>
|
|
|
|
/// <param name="options">The <see cref="MetadataRefreshOptions"/>.</param>
|
|
|
|
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
|
|
|
|
/// <typeparam name="T">The type of item to resolve.</typeparam>
|
|
|
|
/// <returns>A <see cref="Task"/> probing the item for metadata.</returns>
|
2020-09-07 07:20:39 -04:00
|
|
|
public async Task<ItemUpdateType> Probe<T>(
|
|
|
|
T item,
|
|
|
|
MetadataRefreshOptions options,
|
2018-09-12 13:26:21 -04:00
|
|
|
CancellationToken cancellationToken)
|
2014-02-05 23:39:16 -05:00
|
|
|
where T : Audio
|
2013-06-18 15:16:27 -04:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
var path = item.Path;
|
|
|
|
var protocol = item.PathProtocol ?? MediaProtocol.File;
|
2013-06-18 15:16:27 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
if (!item.IsShortcut || options.EnableRemoteContentProbe)
|
|
|
|
{
|
|
|
|
if (item.IsShortcut)
|
|
|
|
{
|
|
|
|
path = item.ShortcutPath;
|
|
|
|
protocol = _mediaSourceManager.GetPathProtocol(path);
|
|
|
|
}
|
2013-06-18 15:16:27 -04:00
|
|
|
|
2020-09-07 07:20:39 -04:00
|
|
|
var result = await _mediaEncoder.GetMediaInfo(
|
|
|
|
new MediaInfoRequest
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
2020-09-07 07:20:39 -04:00
|
|
|
MediaType = DlnaProfileType.Audio,
|
|
|
|
MediaSource = new MediaSourceInfo
|
|
|
|
{
|
|
|
|
Path = path,
|
|
|
|
Protocol = protocol
|
|
|
|
}
|
|
|
|
},
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
2014-02-05 23:39:16 -05:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2014-02-09 16:11:11 -05:00
|
|
|
|
2020-09-07 07:20:39 -04:00
|
|
|
Fetch(item, result, cancellationToken);
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
2014-02-09 16:11:11 -05:00
|
|
|
|
2023-05-15 07:12:24 -04:00
|
|
|
var libraryOptions = _libraryManager.GetLibraryOptions(item);
|
|
|
|
|
|
|
|
if (libraryOptions.EnableLUFSScan)
|
|
|
|
{
|
|
|
|
string output;
|
|
|
|
using (var process = new Process()
|
|
|
|
{
|
|
|
|
StartInfo = new ProcessStartInfo
|
|
|
|
{
|
|
|
|
FileName = _mediaEncoder.EncoderPath,
|
|
|
|
Arguments = $"-hide_banner -i \"{path}\" -af ebur128=framelog=verbose -f null -",
|
|
|
|
RedirectStandardOutput = false,
|
|
|
|
RedirectStandardError = true
|
|
|
|
},
|
|
|
|
})
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
process.Start();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.LogError(ex, "Error starting ffmpeg");
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2023-08-20 14:06:57 -04:00
|
|
|
using var reader = process.StandardError;
|
|
|
|
output = await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
|
2023-05-15 07:12:24 -04:00
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2023-05-22 16:48:09 -04:00
|
|
|
MatchCollection split = LUFSRegex().Matches(output);
|
2023-05-15 07:12:24 -04:00
|
|
|
|
|
|
|
if (split.Count != 0)
|
|
|
|
{
|
|
|
|
item.LUFS = float.Parse(split[0].Groups[1].ValueSpan, CultureInfo.InvariantCulture.NumberFormat);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item.LUFS = DefaultLUFSValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item.LUFS = DefaultLUFSValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.LogDebug("LUFS for {ItemName} is {LUFS}.", item.Name, item.LUFS);
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
return ItemUpdateType.MetadataImport;
|
2013-06-18 15:16:27 -04:00
|
|
|
}
|
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Fetches the specified audio.
|
|
|
|
/// </summary>
|
2022-03-31 10:17:37 -04:00
|
|
|
/// <param name="audio">The <see cref="Audio"/>.</param>
|
|
|
|
/// <param name="mediaInfo">The <see cref="Model.MediaInfo.MediaInfo"/>.</param>
|
|
|
|
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
|
2020-09-07 07:20:39 -04:00
|
|
|
protected void Fetch(Audio audio, Model.MediaInfo.MediaInfo mediaInfo, CancellationToken cancellationToken)
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2017-08-04 16:29:34 -04:00
|
|
|
audio.Container = mediaInfo.Container;
|
2015-04-04 15:35:29 -04:00
|
|
|
audio.TotalBitrate = mediaInfo.Bitrate;
|
2013-12-05 22:39:44 -05:00
|
|
|
|
2015-04-04 15:35:29 -04:00
|
|
|
audio.RunTimeTicks = mediaInfo.RunTimeTicks;
|
|
|
|
audio.Size = mediaInfo.Size;
|
2013-05-15 00:05:52 -04:00
|
|
|
|
2023-02-12 21:59:58 -05:00
|
|
|
if (!audio.IsLocked)
|
|
|
|
{
|
|
|
|
FetchDataFromTags(audio);
|
|
|
|
}
|
2013-12-05 22:39:44 -05:00
|
|
|
|
2022-01-22 09:40:05 -05:00
|
|
|
_itemRepo.SaveMediaStreams(audio.Id, mediaInfo.MediaStreams, cancellationToken);
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2022-03-31 10:17:37 -04:00
|
|
|
/// Fetches data from the tags.
|
2013-02-20 20:33:05 -05:00
|
|
|
/// </summary>
|
2022-03-31 10:17:37 -04:00
|
|
|
/// <param name="audio">The <see cref="Audio"/>.</param>
|
2022-03-28 17:11:21 -04:00
|
|
|
private void FetchDataFromTags(Audio audio)
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2022-03-28 17:11:21 -04:00
|
|
|
var file = TagLib.File.Create(audio.Path);
|
|
|
|
var tagTypes = file.TagTypesOnDisk;
|
2022-10-07 08:40:10 -04:00
|
|
|
Tag? tags = null;
|
2022-03-28 17:11:21 -04:00
|
|
|
|
|
|
|
if (tagTypes.HasFlag(TagTypes.Id3v2))
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2022-03-28 17:11:21 -04:00
|
|
|
tags = file.GetTag(TagTypes.Id3v2);
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
2022-03-28 17:11:21 -04:00
|
|
|
else if (tagTypes.HasFlag(TagTypes.Ape))
|
2021-04-04 17:34:29 -04:00
|
|
|
{
|
2022-03-28 17:11:21 -04:00
|
|
|
tags = file.GetTag(TagTypes.Ape);
|
2021-04-04 17:34:29 -04:00
|
|
|
}
|
2022-03-28 17:11:21 -04:00
|
|
|
else if (tagTypes.HasFlag(TagTypes.FlacMetadata))
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2022-03-28 17:11:21 -04:00
|
|
|
tags = file.GetTag(TagTypes.FlacMetadata);
|
|
|
|
}
|
2022-10-03 07:05:57 -04:00
|
|
|
else if (tagTypes.HasFlag(TagTypes.Apple))
|
|
|
|
{
|
|
|
|
tags = file.GetTag(TagTypes.Apple);
|
|
|
|
}
|
2022-10-03 07:16:13 -04:00
|
|
|
else if (tagTypes.HasFlag(TagTypes.Xiph))
|
|
|
|
{
|
|
|
|
tags = file.GetTag(TagTypes.Xiph);
|
|
|
|
}
|
2022-10-01 14:18:14 -04:00
|
|
|
else if (tagTypes.HasFlag(TagTypes.AudibleMetadata))
|
|
|
|
{
|
|
|
|
tags = file.GetTag(TagTypes.AudibleMetadata);
|
|
|
|
}
|
2022-03-28 17:11:21 -04:00
|
|
|
else if (tagTypes.HasFlag(TagTypes.Id3v1))
|
|
|
|
{
|
|
|
|
tags = file.GetTag(TagTypes.Id3v1);
|
|
|
|
}
|
2013-08-29 17:00:27 -04:00
|
|
|
|
2022-12-05 09:01:13 -05:00
|
|
|
if (tags is not null)
|
2022-03-28 17:11:21 -04:00
|
|
|
{
|
|
|
|
if (audio.SupportsPeople && !audio.LockedFields.Contains(MetadataField.Cast))
|
2013-08-03 20:59:23 -04:00
|
|
|
{
|
2022-03-28 17:11:21 -04:00
|
|
|
var people = new List<PersonInfo>();
|
|
|
|
var albumArtists = tags.AlbumArtists;
|
|
|
|
foreach (var albumArtist in albumArtists)
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2023-07-31 16:19:06 -04:00
|
|
|
if (!string.IsNullOrEmpty(albumArtist))
|
2022-03-28 17:11:21 -04:00
|
|
|
{
|
2023-07-31 16:19:06 -04:00
|
|
|
PeopleHelper.AddPerson(people, new PersonInfo
|
|
|
|
{
|
|
|
|
Name = albumArtist,
|
|
|
|
Type = PersonKind.AlbumArtist
|
|
|
|
});
|
|
|
|
}
|
2022-03-28 17:11:21 -04:00
|
|
|
}
|
2014-06-23 12:05:19 -04:00
|
|
|
|
2022-03-28 17:11:21 -04:00
|
|
|
var performers = tags.Performers;
|
|
|
|
foreach (var performer in performers)
|
|
|
|
{
|
2023-07-31 16:19:06 -04:00
|
|
|
if (!string.IsNullOrEmpty(performer))
|
2022-03-28 17:11:21 -04:00
|
|
|
{
|
2023-07-31 16:19:06 -04:00
|
|
|
PeopleHelper.AddPerson(people, new PersonInfo
|
|
|
|
{
|
|
|
|
Name = performer,
|
|
|
|
Type = PersonKind.Artist
|
|
|
|
});
|
|
|
|
}
|
2022-03-28 17:11:21 -04:00
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
|
2022-03-28 17:11:21 -04:00
|
|
|
foreach (var composer in tags.Composers)
|
|
|
|
{
|
2023-07-31 16:19:06 -04:00
|
|
|
if (!string.IsNullOrEmpty(composer))
|
2022-03-28 17:11:21 -04:00
|
|
|
{
|
2023-07-31 16:19:06 -04:00
|
|
|
PeopleHelper.AddPerson(people, new PersonInfo
|
|
|
|
{
|
|
|
|
Name = composer,
|
|
|
|
Type = PersonKind.Composer
|
|
|
|
});
|
|
|
|
}
|
2022-03-28 17:11:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_libraryManager.UpdatePeople(audio, people);
|
|
|
|
audio.Artists = performers;
|
|
|
|
audio.AlbumArtists = albumArtists;
|
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
|
2022-03-28 17:11:21 -04:00
|
|
|
audio.Name = tags.Title;
|
|
|
|
audio.Album = tags.Album;
|
|
|
|
audio.IndexNumber = Convert.ToInt32(tags.Track);
|
|
|
|
audio.ParentIndexNumber = Convert.ToInt32(tags.Disc);
|
2023-05-15 07:12:24 -04:00
|
|
|
|
2022-03-29 11:06:30 -04:00
|
|
|
if (tags.Year != 0)
|
2022-03-28 17:11:21 -04:00
|
|
|
{
|
|
|
|
var year = Convert.ToInt32(tags.Year);
|
|
|
|
audio.ProductionYear = year;
|
|
|
|
audio.PremiereDate = new DateTime(year, 01, 01);
|
|
|
|
}
|
2013-04-28 01:44:45 -04:00
|
|
|
|
2022-03-28 17:11:21 -04:00
|
|
|
if (!audio.LockedFields.Contains(MetadataField.Genres))
|
2013-04-28 01:44:45 -04:00
|
|
|
{
|
2022-03-28 17:11:21 -04:00
|
|
|
audio.Genres = tags.Genres.Distinct(StringComparer.OrdinalIgnoreCase).ToArray();
|
2013-04-28 01:44:45 -04:00
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
|
2022-03-28 17:11:21 -04:00
|
|
|
audio.SetProviderId(MetadataProvider.MusicBrainzArtist, tags.MusicBrainzArtistId);
|
|
|
|
audio.SetProviderId(MetadataProvider.MusicBrainzAlbumArtist, tags.MusicBrainzReleaseArtistId);
|
|
|
|
audio.SetProviderId(MetadataProvider.MusicBrainzAlbum, tags.MusicBrainzReleaseId);
|
|
|
|
audio.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, tags.MusicBrainzReleaseGroupId);
|
|
|
|
audio.SetProviderId(MetadataProvider.MusicBrainzTrack, tags.MusicBrainzTrackId);
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-05 23:39:16 -05:00
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|