AudioFileProber.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Jellyfin.Data.Enums;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.Entities.Audio;
  12. using MediaBrowser.Controller.Library;
  13. using MediaBrowser.Controller.MediaEncoding;
  14. using MediaBrowser.Controller.Persistence;
  15. using MediaBrowser.Controller.Providers;
  16. using MediaBrowser.Model.Dlna;
  17. using MediaBrowser.Model.Dto;
  18. using MediaBrowser.Model.Entities;
  19. using MediaBrowser.Model.MediaInfo;
  20. using Microsoft.Extensions.Logging;
  21. using TagLib;
  22. namespace MediaBrowser.Providers.MediaInfo
  23. {
  24. /// <summary>
  25. /// Probes audio files for metadata.
  26. /// </summary>
  27. public partial class AudioFileProber
  28. {
  29. // Default LUFS value for use with the web interface, at -18db gain will be 1(no db gain).
  30. private const float DefaultLUFSValue = -18;
  31. private readonly ILogger<AudioFileProber> _logger;
  32. private readonly IMediaEncoder _mediaEncoder;
  33. private readonly IItemRepository _itemRepo;
  34. private readonly ILibraryManager _libraryManager;
  35. private readonly IMediaSourceManager _mediaSourceManager;
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="AudioFileProber"/> class.
  38. /// </summary>
  39. /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
  40. /// <param name="mediaSourceManager">Instance of the <see cref="IMediaSourceManager"/> interface.</param>
  41. /// <param name="mediaEncoder">Instance of the <see cref="IMediaEncoder"/> interface.</param>
  42. /// <param name="itemRepo">Instance of the <see cref="IItemRepository"/> interface.</param>
  43. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  44. public AudioFileProber(
  45. ILogger<AudioFileProber> logger,
  46. IMediaSourceManager mediaSourceManager,
  47. IMediaEncoder mediaEncoder,
  48. IItemRepository itemRepo,
  49. ILibraryManager libraryManager)
  50. {
  51. _logger = logger;
  52. _mediaEncoder = mediaEncoder;
  53. _itemRepo = itemRepo;
  54. _libraryManager = libraryManager;
  55. _mediaSourceManager = mediaSourceManager;
  56. }
  57. [GeneratedRegex(@"I:\s+(.*?)\s+LUFS")]
  58. private static partial Regex LUFSRegex();
  59. [GeneratedRegex(@"REPLAYGAIN_TRACK_GAIN:\s+-?([0-9.]+)\s+dB")]
  60. private static partial Regex ReplayGainTagRegex();
  61. /// <summary>
  62. /// Probes the specified item for metadata.
  63. /// </summary>
  64. /// <param name="item">The item to probe.</param>
  65. /// <param name="options">The <see cref="MetadataRefreshOptions"/>.</param>
  66. /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
  67. /// <typeparam name="T">The type of item to resolve.</typeparam>
  68. /// <returns>A <see cref="Task"/> probing the item for metadata.</returns>
  69. public async Task<ItemUpdateType> Probe<T>(
  70. T item,
  71. MetadataRefreshOptions options,
  72. CancellationToken cancellationToken)
  73. where T : Audio
  74. {
  75. var path = item.Path;
  76. var protocol = item.PathProtocol ?? MediaProtocol.File;
  77. if (!item.IsShortcut || options.EnableRemoteContentProbe)
  78. {
  79. if (item.IsShortcut)
  80. {
  81. path = item.ShortcutPath;
  82. protocol = _mediaSourceManager.GetPathProtocol(path);
  83. }
  84. var result = await _mediaEncoder.GetMediaInfo(
  85. new MediaInfoRequest
  86. {
  87. MediaType = DlnaProfileType.Audio,
  88. MediaSource = new MediaSourceInfo
  89. {
  90. Path = path,
  91. Protocol = protocol
  92. }
  93. },
  94. cancellationToken).ConfigureAwait(false);
  95. cancellationToken.ThrowIfCancellationRequested();
  96. Fetch(item, result, cancellationToken);
  97. }
  98. var libraryOptions = _libraryManager.GetLibraryOptions(item);
  99. bool foundLUFSValue = false;
  100. if (libraryOptions.UseReplayGainTags)
  101. {
  102. using (var process = new Process()
  103. {
  104. StartInfo = new ProcessStartInfo
  105. {
  106. FileName = _mediaEncoder.ProbePath,
  107. Arguments = $"-hide_banner -i \"{path}\"",
  108. RedirectStandardOutput = false,
  109. RedirectStandardError = true
  110. },
  111. })
  112. {
  113. try
  114. {
  115. process.Start();
  116. }
  117. catch (Exception ex)
  118. {
  119. _logger.LogError(ex, "Error starting ffmpeg");
  120. throw;
  121. }
  122. using var reader = process.StandardError;
  123. var output = await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
  124. cancellationToken.ThrowIfCancellationRequested();
  125. Match split = ReplayGainTagRegex().Match(output);
  126. if (split.Success)
  127. {
  128. item.LUFS = DefaultLUFSValue - float.Parse(split.Groups[1].ValueSpan, CultureInfo.InvariantCulture.NumberFormat);
  129. foundLUFSValue = true;
  130. }
  131. else
  132. {
  133. item.LUFS = DefaultLUFSValue;
  134. }
  135. }
  136. }
  137. if (libraryOptions.EnableLUFSScan && !foundLUFSValue)
  138. {
  139. using (var process = new Process()
  140. {
  141. StartInfo = new ProcessStartInfo
  142. {
  143. FileName = _mediaEncoder.EncoderPath,
  144. Arguments = $"-hide_banner -i \"{path}\" -af ebur128=framelog=verbose -f null -",
  145. RedirectStandardOutput = false,
  146. RedirectStandardError = true
  147. },
  148. })
  149. {
  150. try
  151. {
  152. process.Start();
  153. }
  154. catch (Exception ex)
  155. {
  156. _logger.LogError(ex, "Error starting ffmpeg");
  157. throw;
  158. }
  159. using var reader = process.StandardError;
  160. var output = await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
  161. cancellationToken.ThrowIfCancellationRequested();
  162. MatchCollection split = LUFSRegex().Matches(output);
  163. if (split.Count != 0)
  164. {
  165. item.LUFS = float.Parse(split[0].Groups[1].ValueSpan, CultureInfo.InvariantCulture.NumberFormat);
  166. }
  167. else
  168. {
  169. item.LUFS = DefaultLUFSValue;
  170. }
  171. }
  172. }
  173. if (!libraryOptions.EnableLUFSScan && !libraryOptions.UseReplayGainTags)
  174. {
  175. item.LUFS = DefaultLUFSValue;
  176. }
  177. _logger.LogDebug("LUFS for {ItemName} is {LUFS}.", item.Name, item.LUFS);
  178. return ItemUpdateType.MetadataImport;
  179. }
  180. /// <summary>
  181. /// Fetches the specified audio.
  182. /// </summary>
  183. /// <param name="audio">The <see cref="Audio"/>.</param>
  184. /// <param name="mediaInfo">The <see cref="Model.MediaInfo.MediaInfo"/>.</param>
  185. /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
  186. protected void Fetch(Audio audio, Model.MediaInfo.MediaInfo mediaInfo, CancellationToken cancellationToken)
  187. {
  188. audio.Container = mediaInfo.Container;
  189. audio.TotalBitrate = mediaInfo.Bitrate;
  190. audio.RunTimeTicks = mediaInfo.RunTimeTicks;
  191. audio.Size = mediaInfo.Size;
  192. if (!audio.IsLocked)
  193. {
  194. FetchDataFromTags(audio);
  195. }
  196. _itemRepo.SaveMediaStreams(audio.Id, mediaInfo.MediaStreams, cancellationToken);
  197. }
  198. /// <summary>
  199. /// Fetches data from the tags.
  200. /// </summary>
  201. /// <param name="audio">The <see cref="Audio"/>.</param>
  202. private void FetchDataFromTags(Audio audio)
  203. {
  204. var file = TagLib.File.Create(audio.Path);
  205. var tagTypes = file.TagTypesOnDisk;
  206. Tag? tags = null;
  207. if (tagTypes.HasFlag(TagTypes.Id3v2))
  208. {
  209. tags = file.GetTag(TagTypes.Id3v2);
  210. }
  211. else if (tagTypes.HasFlag(TagTypes.Ape))
  212. {
  213. tags = file.GetTag(TagTypes.Ape);
  214. }
  215. else if (tagTypes.HasFlag(TagTypes.FlacMetadata))
  216. {
  217. tags = file.GetTag(TagTypes.FlacMetadata);
  218. }
  219. else if (tagTypes.HasFlag(TagTypes.Apple))
  220. {
  221. tags = file.GetTag(TagTypes.Apple);
  222. }
  223. else if (tagTypes.HasFlag(TagTypes.Xiph))
  224. {
  225. tags = file.GetTag(TagTypes.Xiph);
  226. }
  227. else if (tagTypes.HasFlag(TagTypes.AudibleMetadata))
  228. {
  229. tags = file.GetTag(TagTypes.AudibleMetadata);
  230. }
  231. else if (tagTypes.HasFlag(TagTypes.Id3v1))
  232. {
  233. tags = file.GetTag(TagTypes.Id3v1);
  234. }
  235. if (tags is not null)
  236. {
  237. if (audio.SupportsPeople && !audio.LockedFields.Contains(MetadataField.Cast))
  238. {
  239. var people = new List<PersonInfo>();
  240. var albumArtists = tags.AlbumArtists;
  241. foreach (var albumArtist in albumArtists)
  242. {
  243. if (!string.IsNullOrEmpty(albumArtist))
  244. {
  245. PeopleHelper.AddPerson(people, new PersonInfo
  246. {
  247. Name = albumArtist,
  248. Type = PersonKind.AlbumArtist
  249. });
  250. }
  251. }
  252. var performers = tags.Performers;
  253. foreach (var performer in performers)
  254. {
  255. if (!string.IsNullOrEmpty(performer))
  256. {
  257. PeopleHelper.AddPerson(people, new PersonInfo
  258. {
  259. Name = performer,
  260. Type = PersonKind.Artist
  261. });
  262. }
  263. }
  264. foreach (var composer in tags.Composers)
  265. {
  266. if (!string.IsNullOrEmpty(composer))
  267. {
  268. PeopleHelper.AddPerson(people, new PersonInfo
  269. {
  270. Name = composer,
  271. Type = PersonKind.Composer
  272. });
  273. }
  274. }
  275. _libraryManager.UpdatePeople(audio, people);
  276. audio.Artists = performers;
  277. audio.AlbumArtists = albumArtists;
  278. }
  279. audio.Name = tags.Title;
  280. audio.Album = tags.Album;
  281. audio.IndexNumber = Convert.ToInt32(tags.Track);
  282. audio.ParentIndexNumber = Convert.ToInt32(tags.Disc);
  283. if (tags.Year != 0)
  284. {
  285. var year = Convert.ToInt32(tags.Year);
  286. audio.ProductionYear = year;
  287. audio.PremiereDate = new DateTime(year, 01, 01);
  288. }
  289. if (!audio.LockedFields.Contains(MetadataField.Genres))
  290. {
  291. audio.Genres = tags.Genres.Distinct(StringComparer.OrdinalIgnoreCase).ToArray();
  292. }
  293. audio.SetProviderId(MetadataProvider.MusicBrainzArtist, tags.MusicBrainzArtistId);
  294. audio.SetProviderId(MetadataProvider.MusicBrainzAlbumArtist, tags.MusicBrainzReleaseArtistId);
  295. audio.SetProviderId(MetadataProvider.MusicBrainzAlbum, tags.MusicBrainzReleaseId);
  296. audio.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, tags.MusicBrainzReleaseGroupId);
  297. audio.SetProviderId(MetadataProvider.MusicBrainzTrack, tags.MusicBrainzTrackId);
  298. }
  299. }
  300. }
  301. }