FFMpegAudioImageProvider.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  50. /// </summary>
  51. /// <param name="item">The item.</param>
  52. /// <param name="force">if set to <c>true</c> [force].</param>
  53. /// <param name="cancellationToken">The cancellation token.</param>
  54. /// <returns>Task{System.Boolean}.</returns>
  55. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  56. {
  57. var success = ProviderRefreshStatus.Success;
  58. if (force || string.IsNullOrEmpty(item.PrimaryImagePath))
  59. {
  60. var album = item.ResolveArgs.Parent as MusicAlbum;
  61. if (album != null)
  62. {
  63. // First try to use the parent's image
  64. item.PrimaryImagePath = item.ResolveArgs.Parent.PrimaryImagePath;
  65. }
  66. // If it's still empty see if there's an embedded image
  67. if (force || string.IsNullOrEmpty(item.PrimaryImagePath))
  68. {
  69. var audio = (Audio)item;
  70. if (audio.MediaStreams != null && audio.MediaStreams.Any(s => s.Type == MediaStreamType.Video))
  71. {
  72. var filename = album != null && string.IsNullOrEmpty(audio.Album + album.DateModified.Ticks) ? (audio.Id.ToString() + audio.DateModified.Ticks) : audio.Album;
  73. var path = Kernel.Instance.FFMpegManager.AudioImageCache.GetResourcePath(filename + "_primary", ".jpg");
  74. if (!Kernel.Instance.FFMpegManager.AudioImageCache.ContainsFilePath(path))
  75. {
  76. var semaphore = GetLock(path);
  77. // Acquire a lock
  78. await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  79. // Check again
  80. if (!Kernel.Instance.FFMpegManager.AudioImageCache.ContainsFilePath(path))
  81. {
  82. try
  83. {
  84. await MediaEncoder.ExtractImage(new[] { audio.Path }, InputType.AudioFile, null, path, cancellationToken).ConfigureAwait(false);
  85. }
  86. catch
  87. {
  88. success = ProviderRefreshStatus.Failure;
  89. }
  90. finally
  91. {
  92. semaphore.Release();
  93. }
  94. }
  95. else
  96. {
  97. semaphore.Release();
  98. }
  99. }
  100. if (success == ProviderRefreshStatus.Success)
  101. {
  102. // Image is already in the cache
  103. audio.PrimaryImagePath = path;
  104. }
  105. }
  106. }
  107. }
  108. SetLastRefreshed(item, DateTime.UtcNow, success);
  109. return true;
  110. }
  111. }
  112. }