FFMpegAudioImageProvider.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using MediaBrowser.Common.MediaInfo;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Audio;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.Logging;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Controller.Providers.MediaInfo
  13. {
  14. /// <summary>
  15. /// Uses ffmpeg to create video images
  16. /// </summary>
  17. public class FFMpegAudioImageProvider : BaseFFMpegProvider<Audio>
  18. {
  19. public FFMpegAudioImageProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IMediaEncoder mediaEncoder)
  20. : base(logManager, configurationManager, mediaEncoder)
  21. {
  22. }
  23. /// <summary>
  24. /// The true task result
  25. /// </summary>
  26. protected static readonly Task<bool> TrueTaskResult = Task.FromResult(true);
  27. /// <summary>
  28. /// Gets the priority.
  29. /// </summary>
  30. /// <value>The priority.</value>
  31. public override MetadataProviderPriority Priority
  32. {
  33. get { return MetadataProviderPriority.Last; }
  34. }
  35. /// <summary>
  36. /// The _locks
  37. /// </summary>
  38. private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
  39. /// <summary>
  40. /// Gets the lock.
  41. /// </summary>
  42. /// <param name="filename">The filename.</param>
  43. /// <returns>System.Object.</returns>
  44. private SemaphoreSlim GetLock(string filename)
  45. {
  46. return _locks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
  47. }
  48. /// <summary>
  49. /// Needses the refresh internal.
  50. /// </summary>
  51. /// <param name="item">The item.</param>
  52. /// <param name="providerInfo">The provider info.</param>
  53. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  54. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  55. {
  56. if (!string.IsNullOrEmpty(item.PrimaryImagePath))
  57. {
  58. return false;
  59. }
  60. return base.NeedsRefreshInternal(item, providerInfo);
  61. }
  62. /// <summary>
  63. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  64. /// </summary>
  65. /// <param name="item">The item.</param>
  66. /// <param name="force">if set to <c>true</c> [force].</param>
  67. /// <param name="cancellationToken">The cancellation token.</param>
  68. /// <returns>Task{System.Boolean}.</returns>
  69. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  70. {
  71. var success = ProviderRefreshStatus.Success;
  72. if (force || string.IsNullOrEmpty(item.PrimaryImagePath))
  73. {
  74. var album = item.ResolveArgs.Parent as MusicAlbum;
  75. if (album != null)
  76. {
  77. // First try to use the parent's image
  78. item.PrimaryImagePath = item.ResolveArgs.Parent.PrimaryImagePath;
  79. }
  80. // If it's still empty see if there's an embedded image
  81. if (force || string.IsNullOrEmpty(item.PrimaryImagePath))
  82. {
  83. var audio = (Audio)item;
  84. if (audio.MediaStreams != null && audio.MediaStreams.Any(s => s.Type == MediaStreamType.Video))
  85. {
  86. var filename = album != null && string.IsNullOrEmpty(audio.Album + album.DateModified.Ticks) ? (audio.Id.ToString() + audio.DateModified.Ticks) : audio.Album;
  87. var path = Kernel.Instance.FFMpegManager.AudioImageCache.GetResourcePath(filename + "_primary", ".jpg");
  88. if (!Kernel.Instance.FFMpegManager.AudioImageCache.ContainsFilePath(path))
  89. {
  90. var semaphore = GetLock(path);
  91. // Acquire a lock
  92. await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  93. // Check again
  94. if (!Kernel.Instance.FFMpegManager.AudioImageCache.ContainsFilePath(path))
  95. {
  96. try
  97. {
  98. await MediaEncoder.ExtractImage(new[] { audio.Path }, InputType.AudioFile, null, path, cancellationToken).ConfigureAwait(false);
  99. }
  100. catch
  101. {
  102. success = ProviderRefreshStatus.Failure;
  103. }
  104. finally
  105. {
  106. semaphore.Release();
  107. }
  108. }
  109. else
  110. {
  111. semaphore.Release();
  112. }
  113. }
  114. if (success == ProviderRefreshStatus.Success)
  115. {
  116. // Image is already in the cache
  117. audio.PrimaryImagePath = path;
  118. }
  119. }
  120. }
  121. }
  122. SetLastRefreshed(item, DateTime.UtcNow, success);
  123. return true;
  124. }
  125. }
  126. }